Skip to content

Commit d275892

Browse files
authored
Check that layers array items are objects (#1026)
* Fix unhandled error when layers contains primitive Fixes an unhandled error occurring when a non-object type value is used as a layer item, e.g. `layers: [123]`. For primitive types, this caused an error such as: > TypeError: Cannot use 'in' operator to search for 'ref' in 123 For `null` values, this caused an error such as: > TypeError: Cannot read properties of null (reading 'type') * Update CHANGELOG
1 parent 18d63a6 commit d275892

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

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

66
### 🐞 Bug fixes
77
- Fix RuntimeError class, make it inherited from Error ([#983](https://github.com/maplibre/maplibre-style-spec/issues/983))
8+
- Validate that `layers` array items are objects instead of throwing an error if not ([!1026](https://github.com/maplibre/maplibre-style-spec/pull/1026))
89

910
## 23.1.0
1011

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {validate} from './validate';
2+
import {validateLayer} from './validate_layer';
3+
import v8 from '../reference/v8.json' with {type: 'json'};
4+
5+
test.each([
6+
['number', 1],
7+
['string', '__proto__'],
8+
['boolean', true],
9+
['array', [{}]],
10+
['null', null],
11+
])('Should return error if layer value is %s instead of an object', (valueType, value) => {
12+
const errors = validateLayer({
13+
validateSpec: validate,
14+
value: value,
15+
styleSpec: v8,
16+
style: {} as any,
17+
});
18+
expect(errors).toHaveLength(1);
19+
expect(errors[0].message).toBe(`object expected, ${valueType} found`);
20+
});

src/validate/validate_layer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {validateFilter} from './validate_filter';
66
import {validatePaintProperty} from './validate_paint_property';
77
import {validateLayoutProperty} from './validate_layout_property';
88
import {extendBy as extend} from '../util/extend';
9+
import {getType} from '../util/get_type';
910

1011
export function validateLayer(options) {
1112
let errors = [];
@@ -15,6 +16,10 @@ export function validateLayer(options) {
1516
const style = options.style;
1617
const styleSpec = options.styleSpec;
1718

19+
if (getType(layer) !== 'object') {
20+
return [new ValidationError(key, layer, `object expected, ${getType(layer)} found`)]
21+
}
22+
1823
if (!layer.type && !layer.ref) {
1924
errors.push(new ValidationError(key, layer, 'either "type" or "ref" is required'));
2025
}

0 commit comments

Comments
 (0)