Skip to content

Commit 82012cf

Browse files
Add length=2 for center (#1372)
* add center length * add changelog * add a testcase * fix typo * add an integration test * remove dead code
1 parent ed815ad commit 82012cf

File tree

7 files changed

+174
-6
lines changed

7 files changed

+174
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
- _...Add new stuff here..._
55

66
### 🐞 Bug fixes
7-
- _...Add new stuff here..._
7+
8+
- The optional `center` property is now validated to be a `[lon,lat]` array ([#1372](https://github.com/maplibre/maplibre-gl-js/issues/1372))
89

910
## 24.3.1
1011

src/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ function validSchema(k, v, obj, ref, version, kind) {
8888
'expression',
8989
'property-type',
9090
'length',
91-
'min-length',
9291
'required',
9392
'transition',
9493
'type',

src/reference/v8.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"center": {
2929
"type": "array",
3030
"value": "number",
31+
"length": 2,
3132
"doc": "Default map center in longitude and latitude. The style center will be used only if the map has not been positioned by other means (e.g. map options or user interaction).",
3233
"example": [
3334
-73.9749,
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import {validate} from './validate';
2+
import {validateArray} from './validate_array';
3+
import v8 from '../reference/v8.json' with {type: 'json'};
4+
import {describe, test, expect} from 'vitest';
5+
6+
describe('Validate Array', () => {
7+
test('Should return error if type is not array', () => {
8+
let errors = validateArray({
9+
validateSpec: validate,
10+
key: 'testArray',
11+
value: '3',
12+
valueSpec: {type: 'array', value: 'number'},
13+
styleSpec: v8
14+
});
15+
expect(errors).toHaveLength(1);
16+
expect(errors[0].message).toBe('testArray: array expected, string found');
17+
18+
errors = validateArray({
19+
validateSpec: validate,
20+
key: 'testArray',
21+
value: true,
22+
valueSpec: {type: 'array', value: 'number'},
23+
styleSpec: v8
24+
});
25+
expect(errors).toHaveLength(1);
26+
expect(errors[0].message).toBe('testArray: array expected, boolean found');
27+
28+
errors = validateArray({
29+
validateSpec: validate,
30+
key: 'testArray',
31+
value: null,
32+
valueSpec: {type: 'array', value: 'number'},
33+
styleSpec: v8
34+
});
35+
expect(errors).toHaveLength(1);
36+
expect(errors[0].message).toBe('testArray: array expected, null found');
37+
38+
errors = validateArray({
39+
validateSpec: validate,
40+
key: 'testArray',
41+
value: {x: 1, y: 1},
42+
valueSpec: {type: 'array', value: 'number'},
43+
styleSpec: v8
44+
});
45+
expect(errors).toHaveLength(1);
46+
expect(errors[0].message).toBe('testArray: array expected, object found');
47+
48+
errors = validateArray({
49+
validateSpec: validate,
50+
key: 'testArray',
51+
value: 123,
52+
valueSpec: {type: 'array', value: 'number'},
53+
styleSpec: v8
54+
});
55+
expect(errors).toHaveLength(1);
56+
expect(errors[0].message).toBe('testArray: array expected, number found');
57+
});
58+
59+
test('Should return error if array length does not match spec', () => {
60+
const errors = validateArray({
61+
validateSpec: validate,
62+
key: 'testArray',
63+
value: [1, 2, 3],
64+
valueSpec: {type: 'array', value: 'number', length: 2},
65+
styleSpec: v8
66+
});
67+
expect(errors).toHaveLength(1);
68+
expect(errors[0].message).toBe('testArray: array length 2 expected, length 3 found');
69+
});
70+
71+
test('Should pass if array length matches spec', () => {
72+
const errors = validateArray({
73+
validateSpec: validate,
74+
key: 'testArray',
75+
value: [1, 2],
76+
valueSpec: {type: 'array', value: 'number', length: 2},
77+
styleSpec: v8
78+
});
79+
expect(errors).toHaveLength(0);
80+
});
81+
82+
test('Should return error if array contains invalid element types', () => {
83+
let errors = validateArray({
84+
validateSpec: validate,
85+
key: 'testArray',
86+
value: ['1', '2'],
87+
valueSpec: {type: 'array', value: 'number'},
88+
styleSpec: v8
89+
});
90+
expect(errors).toHaveLength(2);
91+
expect(errors[0].message).toBe('testArray[0]: number expected, string found');
92+
expect(errors[1].message).toBe('testArray[1]: number expected, string found');
93+
94+
errors = validateArray({
95+
validateSpec: validate,
96+
key: 'testArray',
97+
value: [1, true, 3],
98+
valueSpec: {type: 'array', value: 'number'},
99+
styleSpec: v8
100+
});
101+
expect(errors).toHaveLength(1);
102+
expect(errors[0].message).toBe('testArray[1]: number expected, boolean found');
103+
104+
errors = validateArray({
105+
validateSpec: validate,
106+
key: 'testArray',
107+
value: [1, 2, null],
108+
valueSpec: {type: 'array', value: 'number'},
109+
styleSpec: v8
110+
});
111+
expect(errors).toHaveLength(1);
112+
expect(errors[0].message).toBe('testArray[2]: number expected, null found');
113+
});
114+
115+
test('Should pass if array contains valid element types', () => {
116+
let errors = validateArray({
117+
validateSpec: validate,
118+
key: 'testArray',
119+
value: [1, 2, 3],
120+
valueSpec: {type: 'array', value: 'number'},
121+
styleSpec: v8
122+
});
123+
expect(errors).toHaveLength(0);
124+
125+
errors = validateArray({
126+
validateSpec: validate,
127+
key: 'testArray',
128+
value: ['a', 'b', 'c'],
129+
valueSpec: {type: 'array', value: 'string'},
130+
styleSpec: v8
131+
});
132+
expect(errors).toHaveLength(0);
133+
134+
errors = validateArray({
135+
validateSpec: validate,
136+
key: 'testArray',
137+
value: [true, false, true],
138+
valueSpec: {type: 'array', value: 'boolean'},
139+
styleSpec: v8
140+
});
141+
expect(errors).toHaveLength(0);
142+
});
143+
144+
test('Should validate arrays with complex element specs', () => {
145+
const errors = validateArray({
146+
validateSpec: validate,
147+
key: 'testArray',
148+
value: [1, 2, 3],
149+
valueSpec: {
150+
type: 'array',
151+
value: {
152+
type: 'number'
153+
}
154+
},
155+
styleSpec: v8
156+
});
157+
expect(errors).toHaveLength(0);
158+
});
159+
});

src/validate/validate_array.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ export function validateArray(options) {
1818
return [new ValidationError(key, array, `array length ${arraySpec.length} expected, length ${array.length} found`)];
1919
}
2020

21-
if (arraySpec['min-length'] && array.length < arraySpec['min-length']) {
22-
return [new ValidationError(key, array, `array length at least ${arraySpec['min-length']} expected, length ${array.length} found`)];
23-
}
24-
2521
let arrayElementSpec = {
2622
'type': arraySpec.value,
2723
'values': arraySpec.values
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 8,
3+
"center": [12.3, 45.6, 0],
4+
"sources": {},
5+
"layers": []
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"message": "center: array length 2 expected, length 3 found",
4+
"line": 3
5+
}
6+
]

0 commit comments

Comments
 (0)