Skip to content

Commit

Permalink
Validation cannot be disabled for API methods queryRenderedFeatures, …
Browse files Browse the repository at this point in the history
…querySourceFeatures (#8211)

* #8189 disable validation for queryRenderedFeatures and querySourceFeatures

* #8189 disable validation for queryRenderedFeatures and querySourceFeatures

* #8189 disable validation for queryRenderedFeatures and querySourceFeatures. Unit tests, fix flow compilation.
  • Loading branch information
gorshkov-leonid authored and mourner committed May 1, 2019
1 parent 6d7688e commit 6487d62
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ class Style extends Evented {

queryRenderedFeatures(queryGeometry: any, params: any, transform: Transform) {
if (params && params.filter) {
this._validate(validateStyle.filter, 'queryRenderedFeatures.filter', params.filter);
this._validate(validateStyle.filter, 'queryRenderedFeatures.filter', params.filter, null, params);
}

const includedSources = {};
Expand Down Expand Up @@ -1067,9 +1067,9 @@ class Style extends Evented {
return this._flattenAndSortRenderedFeatures(sourceResults);
}

querySourceFeatures(sourceID: string, params: ?{sourceLayer: ?string, filter: ?Array<any>}) {
querySourceFeatures(sourceID: string, params: ?{sourceLayer: ?string, filter: ?Array<any>, validate?: boolean}) {
if (params && params.filter) {
this._validate(validateStyle.filter, 'querySourceFeatures.filter', params.filter);
this._validate(validateStyle.filter, 'querySourceFeatures.filter', params.filter, null, params);
}
const sourceCache = this.sourceCaches[sourceID];
return sourceCache ? querySourceFeatures(sourceCache, params) : [];
Expand Down Expand Up @@ -1121,7 +1121,7 @@ class Style extends Evented {
this.light.updateTransitions(parameters);
}

_validate(validate: ({}) => void, key: string, value: any, props: any, options: StyleSetterOptions = {}) {
_validate(validate: ({}) => void, key: string, value: any, props: any, options: { validate?: boolean } = {}) {
if (options && options.validate === false) {
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ class Map extends Camera {
* Only features within these layers will be returned. If this parameter is undefined, all layers will be checked.
* @param {Array} [options.filter] A [filter](https://www.mapbox.com/mapbox-gl-js/style-spec/#other-filter)
* to limit query results.
* @param {boolean} [options.validate=true] Whether to check if the [options.filter] conforms to the Mapbox GL Style Specification. Disabling validation is a performance optimization that should only be used if you have previously validated the values you will be passing to this function.
*
* @returns {Array<Object>} An array of [GeoJSON](http://geojson.org/)
* [feature objects](https://tools.ietf.org/html/rfc7946#section-3.2).
Expand Down Expand Up @@ -921,6 +922,7 @@ class Map extends Camera {
* sources, this parameter is required.* For GeoJSON sources, it is ignored.
* @param {Array} [parameters.filter] A [filter](https://www.mapbox.com/mapbox-gl-js/style-spec/#other-filter)
* to limit query results.
* @param {boolean} [parameters.validate=true] Whether to check if the [parameters.filter] conforms to the Mapbox GL Style Specification. Disabling validation is a performance optimization that should only be used if you have previously validated the values you will be passing to this function.
*
* @returns {Array<Object>} An array of [GeoJSON](http://geojson.org/)
* [Feature objects](https://tools.ietf.org/html/rfc7946#section-3.2).
Expand All @@ -941,7 +943,7 @@ class Map extends Camera {
* @see [Filter features within map view](https://www.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/)
* @see [Highlight features containing similar data](https://www.mapbox.com/mapbox-gl-js/example/query-similar-features/)
*/
querySourceFeatures(sourceId: string, parameters: ?{sourceLayer: ?string, filter: ?Array<any>}) {
querySourceFeatures(sourceId: string, parameters: ?{sourceLayer: ?string, filter: ?Array<any>, validate?: boolean}) {
return this.style.querySourceFeatures(sourceId, parameters);
}

Expand Down
24 changes: 24 additions & 0 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,30 @@ test('Style#query*Features', (t) => {
t.end();
});

t.test('querySourceFeatures not raise validation errors if validation was disabled', (t) => {
let errors = 0;
t.stub(style, 'fire').callsFake((event) => {
if (event.error) {
console.log(event.error.message);
errors++;
}
});
style.queryRenderedFeatures([{x: 0, y: 0}], {filter: "invalidFilter", validate: false}, transform);
t.equals(errors, 0);
t.end();
});


t.test('querySourceFeatures not raise validation errors if validation was disabled', (t) => {
let errors = 0;
t.stub(style, 'fire').callsFake((event) => {
if (event.error) errors++;
});
style.querySourceFeatures([{x: 0, y: 0}], {filter: "invalidFilter", validate: false}, transform);
t.equals(errors, 0);
t.end();
});

t.end();
});

Expand Down

0 comments on commit 6487d62

Please sign in to comment.