Skip to content

Commit 2ee4c84

Browse files
authored
validate that colorArrays and numberArrays are non-empty. (#1095)
1 parent e8ba3a1 commit 2ee4c84

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### 🐞 Bug fixes
88

9+
- Validate that `numberArray` and `colorArray` values are non-empty ([#1094](https://github.com/maplibre/maplibre-style-spec/pull/1094))
910
- _...Add new stuff here..._
1011

1112
## 23.2.0

src/validate/validate_color_array.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ describe('Validate ColorArray', () => {
5555

5656
test('Should pass if type is array of colors', () => {
5757
let errors = validateColorArray({validateSpec: validate, key: 'colorArray', value: []});
58-
expect(errors).toHaveLength(0);
58+
expect(errors).toHaveLength(1);
59+
expect(errors[0].message).toBe('colorArray: array length at least 1 expected, length 0 found');
5960

6061
errors = validateColorArray({validateSpec: validate, key: 'colorArray', value: ['red']});
6162
expect(errors).toHaveLength(0);

src/validate/validate_color_array.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {ValidationError} from '../error/validation_error';
12
import {getType} from '../util/get_type';
23
import {validateColor} from './validate_color';
34

@@ -7,6 +8,11 @@ export function validateColorArray(options) {
78
const type = getType(value);
89

910
if (type === 'array') {
11+
12+
if (value.length < 1) {
13+
return [new ValidationError(key, value, 'array length at least 1 expected, length 0 found')];
14+
}
15+
1016
let errors = [];
1117
for (let i = 0; i < value.length; i++) {
1218
errors = errors.concat(validateColor({

src/validate/validate_number_array.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ describe('Validate NumberArray', () => {
5757

5858
test('Should pass if type is numeric array', () => {
5959
let errors = validateNumberArray({validateSpec: validate, key: 'numberArray', value: []});
60-
expect(errors).toHaveLength(0);
60+
expect(errors).toHaveLength(1);
61+
expect(errors[0].message).toBe('numberArray: array length at least 1 expected, length 0 found');
6162
errors = validateNumberArray({validateSpec: validate, key: 'numberArray', value: [1]});
6263
expect(errors).toHaveLength(0);
6364
errors = validateNumberArray({validateSpec: validate, key: 'numberArray', value: [1, 1, 1]});

src/validate/validate_number_array.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {ValidationError} from '../error/validation_error';
12
import {getType} from '../util/get_type';
23
import {validateNumber} from './validate_number';
34

@@ -12,6 +13,10 @@ export function validateNumberArray(options) {
1213
type: 'number'
1314
};
1415

16+
if (value.length < 1) {
17+
return [new ValidationError(key, value, 'array length at least 1 expected, length 0 found')];
18+
}
19+
1520
let errors = [];
1621
for (let i = 0; i < value.length; i++) {
1722
errors = errors.concat(options.validateSpec({

test/integration/style-spec/tests/layers.input.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@
178178
"type": "custom",
179179
"source": "vector",
180180
"source-layer": "layer"
181+
},
182+
{
183+
"id": "hillshade-empty-numberArray",
184+
"type": "hillshade",
185+
"source": "raster-dem",
186+
"source-layer": "source-layer",
187+
"paint": {
188+
"hillshade-illumination-direction": [],
189+
"hillshade-illumination-altitude": [],
190+
"hillshade-highlight-color": [],
191+
"hillshade-shadow-color": []
192+
}
181193
}
182194
]
183195
}

test/integration/style-spec/tests/layers.output.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,21 @@
8686
{
8787
"message": "layers[21].type: expected one of [fill, line, symbol, circle, heatmap, fill-extrusion, raster, hillshade, background], \"custom\" found",
8888
"line": 178
89+
},
90+
{
91+
"message": "layers[22].paint.hillshade-illumination-direction: array length at least 1 expected, length 0 found",
92+
"line": 188
93+
},
94+
{
95+
"message": "layers[22].paint.hillshade-illumination-altitude: array length at least 1 expected, length 0 found",
96+
"line": 189
97+
},
98+
{
99+
"message": "layers[22].paint.hillshade-highlight-color: array length at least 1 expected, length 0 found",
100+
"line": 190
101+
},
102+
{
103+
"message": "layers[22].paint.hillshade-shadow-color: array length at least 1 expected, length 0 found",
104+
"line": 191
89105
}
90106
]

0 commit comments

Comments
 (0)