Skip to content

Commit

Permalink
Number validation should fail on NaN (#8615)
Browse files Browse the repository at this point in the history
* Number validation should fail on NaN

* Avoid Number.isNaN

* Add runtime checks

* Add render and unit test

* Remove not useful unit test

* Revert debug page change
  • Loading branch information
Ryan Hamley authored and mourner committed Nov 25, 2019
1 parent efc0caa commit ce520ff
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 2 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/integration/render-tests/text-size/nan/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64
}
},
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [
0,
0
]
}
}
},
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [
{
"id": "symbol",
"type": "symbol",
"source": "geojson",
"layout": {
"text-size": ["sqrt", -1],
"text-field": "ABC",
"text-font": [
"Open Sans Semibold",
"Arial Unicode MS Bold"
]
}
}
]
}
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
}
]

0 comments on commit ce520ff

Please sign in to comment.