Skip to content

Commit

Permalink
Increase strictness of style API validation. (#10779)
Browse files Browse the repository at this point in the history
* Increase strictness of style API validation.
  • Loading branch information
samanpwbb authored Jun 17, 2021
1 parent 67de00f commit 8ffa21f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 49 deletions.
12 changes: 10 additions & 2 deletions src/style-spec/validate_mapbox_api_supported.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function getAllowedKeyErrors(obj: Object, keys: Array<*>, path: ?string): Array<
return errors;
}

const acceptedSourceTypes = new Set(["vector", "raster", "raster-dem"]);
function getSourceErrors(source: Object, i: number): Array<?ValidationError> {
const errors = [];

Expand All @@ -44,15 +45,22 @@ function getSourceErrors(source: Object, i: number): Array<?ValidationError> {
const sourceKeys = ['type', 'url', 'tileSize'];
errors.push(...getAllowedKeyErrors(source, sourceKeys, 'source'));

/*
* "type" is required and must be one of "vector", "raster", "raster-dem"
*/
if (!acceptedSourceTypes.has(String(source.type))) {
errors.push(new ValidationError(`sources[${i}].type`, source.type, `Expected one of [${Array.from(acceptedSourceTypes).join(", ")}]`));
}

/*
* "source" is required. Valid examples:
* mapbox://mapbox.abcd1234
* mapbox://penny.abcd1234
* mapbox://mapbox.abcd1234,penny.abcd1234
*/
const sourceUrlPattern = /^mapbox:\/\/([^/]*)$/;
if (!isValid(source.url, sourceUrlPattern)) {
errors.push(new ValidationError(`sources[${i}]`, source.url, 'Source url must be a valid Mapbox tileset url'));
if (!source.url || !isValid(source.url, sourceUrlPattern)) {
errors.push(new ValidationError(`sources[${i}].url`, source.url, 'Expected a valid Mapbox tileset url'));
}

return errors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"line": 16,
"message": "layers[0].paint.line-dasharray[1]: -2 is less than the minimum value 0"
"message": "layers[0].paint.line-dasharray[1]: -2 is less than the minimum value 0",
"line": 16
},
{
"line": 16,
"message": "layers[0].paint.line-dasharray[2]: -1 is less than the minimum value 0"
"message": "layers[0].paint.line-dasharray[2]: -1 is less than the minimum value 0",
"line": 16
}
]
16 changes: 8 additions & 8 deletions test/unit/style-spec/fixture/bad-sky.output-api-supported.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[
{
"line": 23,
"message": "layers[1].paint.sky-atmosphere-sun[0]: -1 is less than the minimum value 0"
"message": "layers[1].paint.sky-atmosphere-sun[0]: -1 is less than the minimum value 0",
"line": 23
},
{
"line": 23,
"message": "layers[1].paint.sky-atmosphere-sun[1]: 181 is greater than the maximum value 180"
"message": "layers[1].paint.sky-atmosphere-sun[1]: 181 is greater than the maximum value 180",
"line": 23
},
{
"line": 39,
"message": "layers[3].paint.sky-gradient-center[0]: 361 is greater than the maximum value 360"
"message": "layers[3].paint.sky-gradient-center[0]: 361 is greater than the maximum value 360",
"line": 39
},
{
"line": 39,
"message": "layers[3].paint.sky-gradient-center[1]: -1 is less than the minimum value 0"
"message": "layers[3].paint.sky-gradient-center[1]: -1 is less than the minimum value 0",
"line": 39
}
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
{
"line": 11,
"message": "range[0]: -100 is less than the minimum value -20"
"message": "range[0]: -100 is less than the minimum value -20",
"line": 11
},
{
"line": 11,
"message": "range[1]: -80 is less than the minimum value -20"
"message": "range[1]: -80 is less than the minimum value -20",
"line": 11
},
{
"line": 12,
"message": "horizon-blend: -4 is less than the minimum value 0"
"message": "horizon-blend: -4 is less than the minimum value 0",
"line": 12
}
]
]
14 changes: 14 additions & 0 deletions test/unit/style-spec/fixture/layers.output-api-supported.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,26 @@
"message": "source.data: Unsupported property \"data\"",
"line": 14
},
{
"message": "sources[2].type: Expected one of [vector, raster, raster-dem]",
"line": 13
},
{
"message": "sources[2].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.data: Unsupported property \"data\"",
"line": 18
},
{
"message": "source.lineMetrics: Unsupported property \"lineMetrics\"",
"line": 19
},
{
"message": "sources[3].type: Expected one of [vector, raster, raster-dem]",
"line": 17
},
{
"message": "sources[3].url: Expected a valid Mapbox tileset url"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"line": 4
},
{
"message": "sources[0]: Source url must be a valid Mapbox tileset url",
"message": "sources[0].url: Expected a valid Mapbox tileset url",
"line": 8
},
{
"message": "sources[1]: Source url must be a valid Mapbox tileset url",
"message": "sources[1].url: Expected a valid Mapbox tileset url",
"line": 12
}
]
55 changes: 31 additions & 24 deletions test/unit/style-spec/fixture/numbers.output-api-supported.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
[
{
"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\""
}
]
{
"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
},
{
"message": "source.data: Unsupported property \"data\"",
"line": 6
},
{
"message": "sources[0].type: Expected one of [vector, raster, raster-dem]",
"line": 5
},
{
"message": "sources[0].url: Expected a valid Mapbox tileset url"
}
]
3 changes: 3 additions & 0 deletions test/unit/style-spec/fixture/sources.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"zoom": ["+", ["zoom"]],
"state": ["+", ["feature-state", "foo"]]
}
},
"missing-url": {
"type": "vector"
}
},
"layers": []
Expand Down
55 changes: 53 additions & 2 deletions test/unit/style-spec/fixture/sources.output-api-supported.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,33 @@
"message": "sources.cluster-properties.state.map: \"zoom\" and \"feature-state\" expressions are not supported with cluster properties.",
"line": 50
},
{
"message": "sources[0].type: Expected one of [vector, raster, raster-dem]"
},
{
"message": "sources[0].url: Expected a valid Mapbox tileset url"
},
{
"message": "sources[1].type: Expected one of [vector, raster, raster-dem]",
"line": 7
},
{
"message": "sources[1].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.tiles: Unsupported property \"tiles\"",
"line": 12
},
{
"message": "sources[2]: Source url must be a valid Mapbox tileset url",
"message": "sources[2].url: Expected a valid Mapbox tileset url",
"line": 11
},
{
"message": "source.foo: Unsupported property \"foo\"",
"line": 17
},
{
"message": "sources[3]: Source url must be a valid Mapbox tileset url",
"message": "sources[3].url: Expected a valid Mapbox tileset url",
"line": 16
},
{
Expand All @@ -63,10 +76,24 @@
"message": "source.coordinates: Unsupported property \"coordinates\"",
"line": 22
},
{
"message": "sources[4].type: Expected one of [vector, raster, raster-dem]",
"line": 20
},
{
"message": "sources[4].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.urls: Unsupported property \"urls\"",
"line": 28
},
{
"message": "sources[5].type: Expected one of [vector, raster, raster-dem]",
"line": 27
},
{
"message": "sources[5].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.urls: Unsupported property \"urls\"",
"line": 32
Expand All @@ -75,6 +102,13 @@
"message": "source.coordinates: Unsupported property \"coordinates\"",
"line": 33
},
{
"message": "sources[6].type: Expected one of [vector, raster, raster-dem]",
"line": 31
},
{
"message": "sources[6].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.canvas: Unsupported property \"canvas\"",
"line": 39
Expand All @@ -83,6 +117,13 @@
"message": "source.coordinates: Unsupported property \"coordinates\"",
"line": 40
},
{
"message": "sources[7].type: Expected one of [vector, raster, raster-dem]",
"line": 38
},
{
"message": "sources[7].url: Expected a valid Mapbox tileset url"
},
{
"message": "source.data: Unsupported property \"data\"",
"line": 46
Expand All @@ -94,5 +135,15 @@
{
"message": "source.clusterProperties: Unsupported property \"clusterProperties\"",
"line": 48
},
{
"message": "sources[8].type: Expected one of [vector, raster, raster-dem]",
"line": 45
},
{
"message": "sources[8].url: Expected a valid Mapbox tileset url"
},
{
"message": "sources[9].url: Expected a valid Mapbox tileset url"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@
{
"message": "source.data: Unsupported property \"data\"",
"line": 6
},
{
"message": "sources[0].type: Expected one of [vector, raster, raster-dem]",
"line": 5
},
{
"message": "sources[0].url: Expected a valid Mapbox tileset url"
}
]

0 comments on commit 8ffa21f

Please sign in to comment.