-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
validate_expression.js
74 lines (60 loc) · 3.2 KB
/
validate_expression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// @flow
import ValidationError from '../error/validation_error.js';
import {createExpression, createPropertyExpression} from '../expression/index.js';
import {deepUnbundle} from '../util/unbundle_jsonlint.js';
import {isStateConstant, isGlobalPropertyConstant, isFeatureConstant} from '../expression/is_constant.js';
import CompoundExpression from '../expression/compound_expression.js';
import type {Expression} from '../expression/expression.js';
export default function validateExpression(options: any): Array<ValidationError> {
const expression = (options.expressionContext === 'property' ? createPropertyExpression : createExpression)(deepUnbundle(options.value), options.valueSpec);
if (expression.result === 'error') {
return expression.value.map((error) => {
return new ValidationError(`${options.key}${error.key}`, options.value, error.message);
});
}
const expressionObj = (expression.value: any).expression || (expression.value: any)._styleExpression.expression;
if (options.expressionContext === 'property' && (options.propertyKey === 'text-font') &&
!expressionObj.outputDefined()) {
return [new ValidationError(options.key, options.value, `Invalid data expression for "${options.propertyKey}". Output values must be contained as literals within the expression.`)];
}
if (options.expressionContext === 'property' && options.propertyType === 'layout' &&
(!isStateConstant(expressionObj))) {
return [new ValidationError(options.key, options.value, '"feature-state" data expressions are not supported with layout properties.')];
}
if (options.expressionContext === 'filter') {
return disallowedFilterParameters(expressionObj, options);
}
if (options.expressionContext && options.expressionContext.indexOf('cluster') === 0) {
if (!isGlobalPropertyConstant(expressionObj, ['zoom', 'feature-state'])) {
return [new ValidationError(options.key, options.value, '"zoom" and "feature-state" expressions are not supported with cluster properties.')];
}
if (options.expressionContext === 'cluster-initial' && !isFeatureConstant(expressionObj)) {
return [new ValidationError(options.key, options.value, 'Feature data expressions are not supported with initial expression part of cluster properties.')];
}
}
return [];
}
export function disallowedFilterParameters(e: Expression, options: any): Array<ValidationError> {
const disallowedParameters = new Set([
'zoom',
'feature-state',
'pitch',
'distance-from-center'
]);
for (const param of options.valueSpec.expression.parameters) {
disallowedParameters.delete(param);
}
if (disallowedParameters.size === 0) {
return [];
}
const errors = [];
if (e instanceof CompoundExpression) {
if (disallowedParameters.has(e.name)) {
return [new ValidationError(options.key, options.value, `["${e.name}"] expression is not supported in a filter for a ${options.object.type} layer with id: ${options.object.id}`)];
}
}
e.eachChild((arg) => {
errors.push(...disallowedFilterParameters(arg, options));
});
return errors;
}