Skip to content

Commit

Permalink
Silence canvas source validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick committed Apr 4, 2018
1 parent 5a25bad commit 1ffe9ee
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/source/canvas_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class CanvasSource extends ImageSource {
this.options.canvas :
window.document.getElementById(this.options.canvas);
}

this.width = this.canvas.width;
this.height = this.canvas.height;

Expand Down Expand Up @@ -220,7 +219,6 @@ class CanvasSource extends ImageSource {
serialize(): Object {
return {
type: 'canvas',
canvas: this.canvas,
coordinates: this.coordinates
};
}
Expand Down
10 changes: 2 additions & 8 deletions src/style-spec/error/validation_error.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@

export default class ValidationError {
constructor(key, value, message) {
constructor(key, value, message, identifier) {
this.message = (key ? `${key}: ` : '') + message;
if (identifier) this.identifier = identifier;

if (value !== null && value !== undefined && value.__line__) {
this.line = value.__line__;
}
}
}

export class ValidationWarning extends ValidationError {
constructor(key, value, message) {
super(key, value, message);
this.type = 'warning';
}
}
6 changes: 3 additions & 3 deletions src/style-spec/validate/validate_source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import ValidationError, { ValidationWarning } from '../error/validation_error';
import ValidationError from '../error/validation_error';
import { unbundle } from '../util/unbundle_jsonlint';
import validateObject from './validate_object';
import validateEnum from './validate_enum';
Expand Down Expand Up @@ -65,14 +65,14 @@ export default function validateSource(options) {
});

case 'canvas':
errors.push(new ValidationWarning(key, null, `Please use runtime APIs to add canvas sources, rather than including them in stylesheets.`));
errors.push(new ValidationError(key, null, `Please use runtime APIs to add canvas sources, rather than including them in stylesheets.`, 'source.canvas'));
return errors;

default:
return validateEnum({
key: `${key}.type`,
value: value.type,
valueSpec: {values: ['vector', 'raster', 'raster-dem', 'geojson', 'video', 'image', 'canvas']},
valueSpec: {values: ['vector', 'raster', 'raster-dem', 'geojson', 'video', 'image']},
style: style,
styleSpec: styleSpec
});
Expand Down
7 changes: 6 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getJSON, ResourceType } from '../util/ajax';
import { isMapboxURL, normalizeStyleURL } from '../util/mapbox';
import browser from '../util/browser';
import Dispatcher from '../util/dispatcher';
import { validateStyle, emitValidationErrors } from './validate_style';
import { validateStyle, emitValidationErrors as _emitValidationErrors } from './validate_style';
import {
getType as getSourceType,
setType as setSourceType
Expand All @@ -35,6 +35,11 @@ import PauseablePlacement from './pauseable_placement';
import ZoomHistory from './zoom_history';
import CrossTileSymbolIndex from '../symbol/cross_tile_symbol_index';

// We're skipping validation errors with the `source.canvas` identifier in order
// to continue to allow canvas sources to be added at runtime/updated in
// smart setStyle (see https://github.com/mapbox/mapbox-gl-js/pull/6424):
const emitValidationErrors = (evented, errors) => _emitValidationErrors(evented, errors, { 'source.canvas': true});

import type Map from '../ui/map';
import type Transform from '../geo/transform';
import type {Source} from '../source/source';
Expand Down
16 changes: 6 additions & 10 deletions src/style/validate_style.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import validateStyleMin from '../style-spec/validate_style.min';
import { ErrorEvent, WarningEvent } from '../util/evented';
import { ValidationWarning } from '../style-spec/error/validation_error';
import { ErrorEvent } from '../util/evented';

import type {Evented} from '../util/evented';

Expand All @@ -20,17 +19,14 @@ export const validateFilter = (validateStyleMin.filter: Validator);
export const validatePaintProperty = (validateStyleMin.paintProperty: Validator);
export const validateLayoutProperty = (validateStyleMin.layoutProperty: Validator);

export function emitValidationErrors(emitter: Evented, errors: ?$ReadOnlyArray<{message: string}>): boolean {
export function emitValidationErrors(emitter: Evented, errors: ?$ReadOnlyArray<{message: string}>, omit: ?{[string]: boolean}): boolean {
let hasErrors = false;
if (errors && errors.length) {
for (const error of errors) {
const {message} = error;
if (error instanceof ValidationWarning) {
emitter.fire(new WarningEvent({warning: message}));
} else {
emitter.fire(new ErrorEvent(new Error(message)));
hasErrors = true;
}
if (error.identifier && omit && omit[error.identifier]) continue;

emitter.fire(new ErrorEvent(new Error(error.message)));
hasErrors = true;
}
}
return hasErrors;
Expand Down
6 changes: 0 additions & 6 deletions src/util/evented.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ export class ErrorEvent extends Event {
}
}

export class WarningEvent extends Event {
constructor(data: Object = {}) {
super('warning', data);
}
}

/**
* Methods mixed in to other classes for event capabilities.
*
Expand Down
1 change: 0 additions & 1 deletion test/unit/source/canvas_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ test('CanvasSource#serialize', (t) => {

const serialized = source.serialize();
t.equal(serialized.type, 'canvas');
t.ok(serialized.canvas);
t.deepEqual(serialized.coordinates, [[0, 0], [1, 0], [1, 1], [0, 1]]);

window.restore();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/style-spec/fixture/layers.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"message": "layers[13]: layer \"missing-source-layer\" must specify a \"source-layer\"",
"line": 91
}
]
]
2 changes: 1 addition & 1 deletion test/unit/style-spec/fixture/properties.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"message": "layers[4].paint.fill-opacity: \"fill-opacity\" does not support interpolation syntax\nUse an identity property function instead: `{ \"type\": \"identity\", \"property\": \"opacity\" }`.",
"line": 74
}
]
]
7 changes: 7 additions & 0 deletions test/unit/style-spec/fixture/sources.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
"coordinates": [
1, "2", [3, "4"], []
]
},
"canvas": {
"type": "canvas",
"canvas": "canvas",
"coordinates": [
[1, 2], [3, 4], [5, 6], [7, 8]
]
}
},
"layers": []
Expand Down
8 changes: 6 additions & 2 deletions test/unit/style-spec/fixture/sources.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"line": 4
},
{
"message": "sources.invalid-type.type: expected one of [vector, raster, raster-dem, geojson, video, image, canvas], \"invalid\" found",
"message": "sources.invalid-type.type: expected one of [vector, raster, raster-dem, geojson, video, image], \"invalid\" found",
"line": 7
},
{
Expand Down Expand Up @@ -34,5 +34,9 @@
{
"message": "sources.video-wrong-coordinates.coordinates[3]: array length 2 expected, length 0 found",
"line": 34
},
{
"message": "sources.canvas: Please use runtime APIs to add canvas sources, rather than including them in stylesheets.",
"identifier": "source.canvas"
}
]
]

0 comments on commit 1ffe9ee

Please sign in to comment.