You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.3 KiB
115 lines
3.3 KiB
// @flow
|
|
|
|
import ValidationError from '../error/validation_error.js';
|
|
import {unbundle} from '../util/unbundle_jsonlint.js';
|
|
import validateObject from './validate_object.js';
|
|
import validateEnum from './validate_enum.js';
|
|
import validateExpression from './validate_expression.js';
|
|
import validateString from './validate_string.js';
|
|
import getType from '../util/get_type.js';
|
|
|
|
import type {ValidationOptions} from './validate.js';
|
|
|
|
const objectElementValidators = {
|
|
promoteId: validatePromoteId
|
|
};
|
|
|
|
export default function validateSource(options: ValidationOptions): Array<ValidationError> {
|
|
const value = options.value;
|
|
const key = options.key;
|
|
const styleSpec = options.styleSpec;
|
|
const style = options.style;
|
|
|
|
if (!value.type) {
|
|
return [new ValidationError(key, value, '"type" is required')];
|
|
}
|
|
|
|
const type = unbundle(value.type);
|
|
let errors;
|
|
|
|
switch (type) {
|
|
case 'vector':
|
|
case 'raster':
|
|
case 'raster-dem':
|
|
errors = validateObject({
|
|
key,
|
|
value,
|
|
valueSpec: styleSpec[`source_${type.replace('-', '_')}`],
|
|
style: options.style,
|
|
styleSpec,
|
|
objectElementValidators
|
|
});
|
|
return errors;
|
|
|
|
case 'geojson':
|
|
errors = validateObject({
|
|
key,
|
|
value,
|
|
valueSpec: styleSpec.source_geojson,
|
|
style,
|
|
styleSpec,
|
|
objectElementValidators
|
|
});
|
|
if (value.cluster) {
|
|
for (const prop in value.clusterProperties) {
|
|
const [operator, mapExpr] = value.clusterProperties[prop];
|
|
const reduceExpr = typeof operator === 'string' ? [operator, ['accumulated'], ['get', prop]] : operator;
|
|
|
|
errors.push(...validateExpression({
|
|
key: `${key}.${prop}.map`,
|
|
value: mapExpr,
|
|
expressionContext: 'cluster-map'
|
|
}));
|
|
errors.push(...validateExpression({
|
|
key: `${key}.${prop}.reduce`,
|
|
value: reduceExpr,
|
|
expressionContext: 'cluster-reduce'
|
|
}));
|
|
}
|
|
}
|
|
return errors;
|
|
|
|
case 'video':
|
|
return validateObject({
|
|
key,
|
|
value,
|
|
valueSpec: styleSpec.source_video,
|
|
style,
|
|
styleSpec
|
|
});
|
|
|
|
case 'image':
|
|
return validateObject({
|
|
key,
|
|
value,
|
|
valueSpec: styleSpec.source_image,
|
|
style,
|
|
styleSpec
|
|
});
|
|
|
|
case 'canvas':
|
|
return [new ValidationError(key, null, `Please use runtime APIs to add canvas sources, rather than including them in stylesheets.`, 'source.canvas')];
|
|
|
|
default:
|
|
return validateEnum({
|
|
key: `${key}.type`,
|
|
value: value.type,
|
|
valueSpec: {values: ['vector', 'raster', 'raster-dem', 'geojson', 'video', 'image']},
|
|
style,
|
|
styleSpec
|
|
});
|
|
}
|
|
}
|
|
|
|
function validatePromoteId({key, value}) {
|
|
if (getType(value) === 'string') {
|
|
return validateString({key, value});
|
|
} else {
|
|
const errors = [];
|
|
for (const prop in value) {
|
|
errors.push(...validateString({key: `${key}.${prop}`, value: value[prop]}));
|
|
}
|
|
return errors;
|
|
}
|
|
}
|