Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number validation should fail on NaN #8615

Merged
merged 6 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/style-spec/expression/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export class StyleExpression {

try {
const val = this.expression.evaluate(this._evaluator);
if (val === null || val === undefined) {
// eslint-disable-next-line no-self-compare
if (val === null || val === undefined || (typeof val === 'number' && val !== val)) {
return this._defaultValue;
}
if (this._enumValues && !(val in this._enumValues)) {
Expand Down
7 changes: 6 additions & 1 deletion src/style-spec/validate/validate_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export default function validateNumber(options) {
const key = options.key;
const value = options.value;
const valueSpec = options.valueSpec;
const type = getType(value);
let type = getType(value);

// eslint-disable-next-line no-self-compare
if (type === 'number' && value !== value) {
type = 'NaN';
}

if (type !== 'number') {
return [new ValidationError(key, value, `number expected, ${type} found`)];
Expand Down
94 changes: 94 additions & 0 deletions test/unit/style-spec/fixture/numbers.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"version": 8,
"sources": {
"point": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
}
]
}
}
},
"layers": [
{
"id": "valid",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": 5
}
},
{
"id": "zero",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": 0
}
},
{
"id": "less-than-zero",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": -1
}
},
{
"id": "null-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": null
}
},
{
"id": "object-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": {}
}
},
{
"id": "array-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": []
}
},
{
"id": "boolean-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": true
}
},
{
"id": "expression",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": ["sqrt", 16]
}
},
{
"id": "expression-invalid",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": ["/", 0, 0]
}
}
]
}
25 changes: 25 additions & 0 deletions test/unit/style-spec/fixture/numbers.output-api-supported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"line": 42,
"message": "layers[2].paint.circle-radius: -1 is less than the minimum value 0"
},
{
"message": "layers[3].paint.circle-radius: number expected, null found"
},
{
"line": 58,
"message": "layers[4].paint.circle-radius: missing required property \"stops\""
},
{
"line": 66,
"message": "layers[5].paint.circle-radius: number expected, array found"
},
{
"line": 74,
"message": "layers[6].paint.circle-radius: number expected, boolean found"
},
{
"line": 6,
"message": "source.data: Unsupported property \"data\""
}
]
21 changes: 21 additions & 0 deletions test/unit/style-spec/fixture/numbers.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"message": "layers[2].paint.circle-radius: -1 is less than the minimum value 0",
"line": 42
},
{
"message": "layers[3].paint.circle-radius: number expected, null found"
},
{
"message": "layers[4].paint.circle-radius: missing required property \"stops\"",
"line": 58
},
{
"message": "layers[5].paint.circle-radius: number expected, array found",
"line": 66
},
{
"message": "layers[6].paint.circle-radius: number expected, boolean found",
"line": 74
}
]