Skip to content

Commit

Permalink
Accept {expression: [...]} in style spec validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Jul 3, 2017
1 parent efa6f29 commit 6ba399f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/style-spec/function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function createFunction(parameters, propertySpec) {

module.exports = createFunction;
module.exports.isFunctionDefinition = isFunctionDefinition;
module.exports.getExpectedType = getExpectedType;

function isFunctionDefinition(value) {
return typeof value === 'object' &&
Expand Down
10 changes: 10 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,10 @@
"doc": "The geometry type for the filter to select."
},
"function": {
"expression": {
"type": "expression",
"doc": "An expression."
},
"stops": {
"type": "array",
"doc": "An array of stops.",
Expand Down Expand Up @@ -1734,6 +1738,12 @@
"length": 2,
"doc": "Zoom level and value pair."
},
"expression": {
"type": "array",
"value": "*",
"minimum": 1,
"doc": "An expression defines a function that can be used for data-driven style properties or feature filters."
},
"light": {
"anchor": {
"type": "enum",
Expand Down
23 changes: 23 additions & 0 deletions src/style-spec/validate/validate_expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

const ValidationError = require('../error/validation_error');
const {getExpectedType} = require('../function');
const compile = require('../function/compile');
const unbundle = require('../util/unbundle_jsonlint');

module.exports = function validateExpression(options) {
const expression = deepUnbundle(options.value.expression);
const compiled = compile(expression, getExpectedType(options.valueSpec));
if (compiled.result === 'success') return [];

const key = `${options.key}.expression`;
return compiled.errors.map((error) => {
return new ValidationError(`${key}${error.key}`, options.value, error.error);
});
};

function deepUnbundle (value) {
if (Array.isArray(value)) {
return value.map(deepUnbundle);
}
return unbundle(value);
}
5 changes: 5 additions & 0 deletions src/style-spec/validate/validate_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const validate = require('./validate');
const validateObject = require('./validate_object');
const validateArray = require('./validate_array');
const validateNumber = require('./validate_number');
const validateExpression = require('./validate_expression');
const unbundle = require('../util/unbundle_jsonlint');

module.exports = function validateFunction(options) {
Expand All @@ -22,6 +23,10 @@ module.exports = function validateFunction(options) {
getType(options.value.stops[0]) === 'array' &&
getType(options.value.stops[0][0]) === 'object';

if (options.value.expression) {
return validateExpression(options);
}

const errors = validateObject({
key: options.key,
value: options.value,
Expand Down
33 changes: 33 additions & 0 deletions test/unit/style-spec/fixture/functions.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,39 @@
"default": "invalid"
}
}
},
{
"id": "valid expression",
"type": "fill",
"source": "source",
"source-layer": "layer",
"paint": {
"fill-color": {
"expression": ["rgba", 10, ["number", ["get", "x"]], 30, 1]
}
}
},
{
"id": "invalid expression type - color",
"type": "fill",
"source": "source",
"source-layer": "layer",
"paint": {
"fill-color": {
"expression": ["pi"]
}
}
},
{
"id": "invalid expression - fails type checking",
"type": "fill",
"source": "source",
"source-layer": "layer",
"paint": {
"fill-color": {
"expression": ["rgba", 1, "should be a number", 0, 1]
}
}
}
]
}
14 changes: 11 additions & 3 deletions test/unit/style-spec/fixture/functions.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@
"message": "layers[22].paint.fill-color: missing required property \"property\"",
"line": 401
},
{
"message": "layers[32].paint.fill-color.stops[0][0]: stop domain value must be a number, string, or boolean"
},
{
"message": "layers[23].paint.fill-color.stops: identity function may not have a \"stops\" property",
"line": 415
},
{
"message": "layers[32].paint.fill-color.stops[0][0]: stop domain value must be a number, string, or boolean"
},
{
"message": "layers[24].paint.fill-color.stops[1][0]: number stop domain type must match previous stop domain type string",
"line": 434
Expand Down Expand Up @@ -157,5 +157,13 @@
{
"message": "layers[43].paint.fill-color.default: color expected, \"invalid\" found",
"line": 839
},
{
"message": "layers[45].paint.fill-color.expression: Expected Color but found Number instead.",
"line": 860
},
{
"message": "layers[46].paint.fill-color.expression[2]: Expected Number but found String instead.",
"line": 871
}
]

0 comments on commit 6ba399f

Please sign in to comment.