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

Fix format expression options validation #8339

Merged
merged 7 commits into from
Jun 13, 2019
Merged
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
12 changes: 11 additions & 1 deletion src/style-spec/util/unbundle_jsonlint.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
function isPrimitive(value) {
return value instanceof Number || value instanceof String || value instanceof Boolean;
}

// Turn jsonlint-lines-primitives objects into primitive objects
export function unbundle(value) {
if (value instanceof Number || value instanceof String || value instanceof Boolean) {
if (isPrimitive(value)) {
return value.valueOf();
} else {
return value;
Expand All @@ -11,7 +14,14 @@ export function unbundle(value) {
export function deepUnbundle(value) {
if (Array.isArray(value)) {
return value.map(deepUnbundle);
} else if (value instanceof Object && !isPrimitive(value)) {
const unbundledValue = {};
for (const key in value) {
unbundledValue[key] = deepUnbundle(value[key]);
}
return unbundledValue;
}

return unbundle(value);
}

161 changes: 161 additions & 0 deletions test/unit/style-spec/fixture/text-field-format.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
"version": 8,
"sources": {
"point": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"font-scale-override": 2.0,
"text-font-override": ["Arial"]
},
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
}
]
}
}
},
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [
{
"id": "no options - invalid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text"
]
}
},
{
"id": "empty options - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{}
]
}
},
{
"id": "font-scale option float literal - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "font-scale": 1.5 }
]
}
},
{
"id": "font-scale option expression - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "font-scale": ["get", "font-scale-override"] }
]
}
},
{
"id": "font-scale option invalid expression type - invalid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "font-scale": ["string", 3.0] }
]
}
},
{
"id": "text-font option array - invalid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "text-font": ["Helvetica"] }
]
}
},
{
"id": "text-font option literal coercion - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "text-font": ["literal", ["Helvetica"]] }
]
}
},
{
"id": "text-font option expression - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "text-font": ["get", "text-font-override"] }
]
}
},
{
"id": "text-font option invalid expression type - invalid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "text-font": ["number", ["get", "font-scale-override"]] }
]
}
},
{
"id": "text-font option array expression - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{ "text-font": ["array", "string", ["literal", ["Helvetica"]]] }
]
}
},
{
"id": "text-font option and font-scale option combined - valid",
"type": "symbol",
"source": "point",
"layout": {
"text-field": [
"format",
"some text",
{
"text-font": ["literal", ["Helvetica"]],
"font-scale": 1.5
}
]
}
}
]
}
18 changes: 18 additions & 0 deletions test/unit/style-spec/fixture/text-field-format.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"message": "layers[0].layout.text-field: Expected at least two arguments.",
"line": 31
},
{
"message": "layers[4].layout.text-field[1]: Expected number but found string instead.",
"line": 78
},
{
"message": "layers[5].layout.text-field[1][0]: Unknown expression \"Helvetica\". If you wanted a literal array, use [\"literal\", [...]].",
"line": 90
},
{
"message": "layers[8].layout.text-field[1]: Expected array<string> but found number instead.",
"line": 126
}
]
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
integrity sha512-OfC2uemaknXr87bdLUkWog7nYuliM9Ij5HUcajsVcMCpQrcLmtxRbVFTIqmcSkSeYRBFBRxs2FiUqFJDLdiebA==
dependencies:
"@babel/highlight" "^7.0.0"
"@babel/highlight" "~7.0.0"

"@babel/core@>=7.1.0", "@babel/core@^7.1.2":
version "7.4.3"
Expand Down