Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate duplicate stops in stylesheets. #4498

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/style-spec/validate/validate_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,22 @@ module.exports = function validateFunction(options) {
arrayElementValidator: validateFunctionStop
}));

if (getType(value) === 'array' && value.length === 0) {
errors.push(new ValidationError(options.key, value, 'array must have at least one stop'));
if (getType(value) === 'array') {
if (value.length === 0) {
errors.push(new ValidationError(options.key, value, 'array must have at least one stop'));
} else {
const seenStops = Object.create(null);
value.every(stop => {
if (getType(stop) !== 'array' || getType(stop[0]) !== 'number') {
return false;
}
if (seenStops[stop[0]]) {
errors.push(new ValidationError(options.key, value, 'stops must not have duplicate pairs.'));
}
seenStops[stop[0]] = stop[1];
return true;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't actually use the returned value of every, it makes more sense to use a for ... of loop here.

}
}

return errors;
Expand Down
14 changes: 14 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,20 @@
"default": "invalid"
}
}
},
{
"id": "invalid duplicate stop pairs",
"type": "symbol",
"source": "source",
"source-layer": "layer",
"layout": {
"text-size": {
"stops": [
[0, 1],
[0, 1]
]
}
}
}
]
}
4 changes: 4 additions & 0 deletions test/unit/style-spec/fixture/functions.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,9 @@
{
"message": "layers[43].paint.fill-color.default: color expected, \"invalid\" found",
"line": 839
},
{
"message": "layers[44].layout.text-size.stops: stops must not have duplicate pairs.",
"line": 850
}
]