strict-qs 8.0.3 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/errors.d.ts +82 -0
- package/dist/errors.js +2 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +8 -11
- package/dist/index.js +29 -13
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +102 -48
- package/dist/index.test.js.map +1 -1
- package/package.json +45 -60
- package/src/errors.ts +94 -0
- package/src/index.test.ts +160 -109
- package/src/index.ts +43 -41
package/src/index.test.ts
CHANGED
|
@@ -1,37 +1,45 @@
|
|
|
1
|
-
import { describe, test } from '@jest/globals';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import type { QSParameter } from './index.js';
|
|
1
|
+
import { describe, test, expect } from '@jest/globals';
|
|
2
|
+
import { qsStrict, type QSParameter } from './index.js';
|
|
3
|
+
import { YError } from 'yerror';
|
|
5
4
|
|
|
6
5
|
describe('strict-qs', () => {
|
|
7
6
|
describe('with no search', () => {
|
|
8
|
-
const qsDefinition = [];
|
|
7
|
+
const qsDefinition: QSParameter[] = [];
|
|
9
8
|
|
|
10
9
|
test('should work', () => {
|
|
11
|
-
|
|
10
|
+
expect(qsStrict({}, qsDefinition, '')).toEqual({});
|
|
12
11
|
});
|
|
13
12
|
});
|
|
14
13
|
|
|
15
14
|
describe('with bad search', () => {
|
|
16
|
-
const qsDefinition = [];
|
|
15
|
+
const qsDefinition: QSParameter[] = [];
|
|
17
16
|
|
|
18
17
|
test('should fail', () => {
|
|
19
|
-
|
|
18
|
+
try {
|
|
20
19
|
qsStrict({}, qsDefinition, '?');
|
|
21
|
-
|
|
20
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
21
|
+
} catch (err) {
|
|
22
|
+
expect(err).toMatchInlineSnapshot(
|
|
23
|
+
`[YError: E_EMPTY_SEARCH (["?"]): E_EMPTY_SEARCH]`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
22
26
|
});
|
|
23
27
|
|
|
24
28
|
test('should work when allowed', () => {
|
|
25
|
-
|
|
26
|
-
qsStrict({ allowEmptySearch: true }, qsDefinition, '?'),
|
|
29
|
+
expect(qsStrict({ allowEmptySearch: true }, qsDefinition, '?')).toEqual(
|
|
27
30
|
{},
|
|
28
31
|
);
|
|
29
32
|
});
|
|
30
33
|
|
|
31
34
|
test('should fail', () => {
|
|
32
|
-
|
|
35
|
+
try {
|
|
33
36
|
qsStrict({}, qsDefinition, 'lol');
|
|
34
|
-
|
|
37
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
38
|
+
} catch (err) {
|
|
39
|
+
expect(err).toMatchInlineSnapshot(
|
|
40
|
+
`[YError: E_MALFORMED_SEARCH (["lol"]): E_MALFORMED_SEARCH]`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
35
43
|
});
|
|
36
44
|
});
|
|
37
45
|
|
|
@@ -47,9 +55,14 @@ describe('strict-qs', () => {
|
|
|
47
55
|
];
|
|
48
56
|
|
|
49
57
|
test('should fail', () => {
|
|
50
|
-
|
|
58
|
+
try {
|
|
51
59
|
qsStrict({}, qsDefinition, '?user=lol');
|
|
52
|
-
|
|
60
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
61
|
+
} catch (err) {
|
|
62
|
+
expect(err).toMatchInlineSnapshot(
|
|
63
|
+
`[YError: E_UNSUPPORTED_TYPE (["user","object"]): E_UNSUPPORTED_TYPE]`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
53
66
|
});
|
|
54
67
|
});
|
|
55
68
|
|
|
@@ -63,20 +76,24 @@ describe('strict-qs', () => {
|
|
|
63
76
|
type: 'number',
|
|
64
77
|
},
|
|
65
78
|
ordered: true,
|
|
66
|
-
description: 'The pages to print',
|
|
67
79
|
},
|
|
68
80
|
];
|
|
69
81
|
|
|
70
82
|
test('should work when params are ordered', () => {
|
|
71
|
-
|
|
72
|
-
pages: [0, 1, 2],
|
|
83
|
+
expect(qsStrict({}, qsDefinition, '?pages=0&pages=1&pages=2')).toEqual({
|
|
84
|
+
pages: [0, 1, 2],
|
|
73
85
|
});
|
|
74
86
|
});
|
|
75
87
|
|
|
76
88
|
test('should fail when params are not ordered', () => {
|
|
77
|
-
|
|
89
|
+
try {
|
|
78
90
|
qsStrict({}, qsDefinition, '?pages=0&pages=2&pages=1');
|
|
79
|
-
|
|
91
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
92
|
+
} catch (err) {
|
|
93
|
+
expect(err).toMatchInlineSnapshot(
|
|
94
|
+
`[YError: E_UNORDERED_QUERY_PARAMS ([1,2]): E_UNORDERED_QUERY_PARAMS]`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
80
97
|
});
|
|
81
98
|
});
|
|
82
99
|
|
|
@@ -90,16 +107,15 @@ describe('strict-qs', () => {
|
|
|
90
107
|
];
|
|
91
108
|
|
|
92
109
|
test('should work', () => {
|
|
93
|
-
|
|
110
|
+
expect(
|
|
94
111
|
qsStrict(
|
|
95
112
|
{},
|
|
96
113
|
qsDefinition,
|
|
97
114
|
'?redirectURL=' + encodeURIComponent('http://localhost/plop'),
|
|
98
115
|
),
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
);
|
|
116
|
+
).toEqual({
|
|
117
|
+
redirectURL: 'http://localhost/plop',
|
|
118
|
+
});
|
|
103
119
|
});
|
|
104
120
|
});
|
|
105
121
|
|
|
@@ -113,7 +129,7 @@ describe('strict-qs', () => {
|
|
|
113
129
|
];
|
|
114
130
|
|
|
115
131
|
test('should work', () => {
|
|
116
|
-
|
|
132
|
+
expect(qsStrict({}, qsDefinition, '?query=a+b+c')).toEqual({
|
|
117
133
|
query: 'a b c',
|
|
118
134
|
});
|
|
119
135
|
});
|
|
@@ -128,7 +144,6 @@ describe('strict-qs', () => {
|
|
|
128
144
|
required: true,
|
|
129
145
|
default: 'en',
|
|
130
146
|
enum: ['fr', 'en', 'de'],
|
|
131
|
-
description: 'The language for the search',
|
|
132
147
|
},
|
|
133
148
|
{
|
|
134
149
|
name: 'types',
|
|
@@ -138,20 +153,17 @@ describe('strict-qs', () => {
|
|
|
138
153
|
type: 'string',
|
|
139
154
|
enum: ['open', 'closed', 'pending', 'idle', 'invalid'],
|
|
140
155
|
},
|
|
141
|
-
description: 'The types of the search',
|
|
142
156
|
},
|
|
143
157
|
{
|
|
144
158
|
name: 'code',
|
|
145
159
|
in: 'query',
|
|
146
160
|
type: 'number',
|
|
147
161
|
pattern: '^[0-9]+$',
|
|
148
|
-
description: 'The code id',
|
|
149
162
|
},
|
|
150
163
|
{
|
|
151
164
|
name: 'full',
|
|
152
165
|
in: 'query',
|
|
153
166
|
type: 'boolean',
|
|
154
|
-
description: 'Wether it is a full search or not',
|
|
155
167
|
},
|
|
156
168
|
{
|
|
157
169
|
name: 'nums',
|
|
@@ -159,204 +171,243 @@ describe('strict-qs', () => {
|
|
|
159
171
|
type: 'array',
|
|
160
172
|
items: {
|
|
161
173
|
type: 'number',
|
|
162
|
-
enum: [1, 2, 3, 4],
|
|
174
|
+
enum: [1, 2, 3, 4],
|
|
163
175
|
},
|
|
164
|
-
description: 'The types of the search',
|
|
165
176
|
},
|
|
166
177
|
];
|
|
167
178
|
|
|
168
179
|
test('should work with good params', () => {
|
|
169
|
-
|
|
180
|
+
expect(
|
|
170
181
|
qsStrict(
|
|
171
182
|
{},
|
|
172
183
|
qsDefinition,
|
|
173
184
|
'?lang=fr&types=open&types=closed&types=pending&code=3&full=true',
|
|
174
185
|
),
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
qsStrict.bind(
|
|
184
|
-
null,
|
|
186
|
+
).toEqual({
|
|
187
|
+
lang: 'fr',
|
|
188
|
+
types: ['open', 'closed', 'pending'],
|
|
189
|
+
code: 3,
|
|
190
|
+
full: true,
|
|
191
|
+
});
|
|
192
|
+
try {
|
|
193
|
+
qsStrict(
|
|
185
194
|
{},
|
|
186
195
|
qsDefinition,
|
|
187
196
|
'?lang=cn&types=open&types=closed&types=pending&code=3&full=true',
|
|
188
|
-
)
|
|
189
|
-
|
|
190
|
-
)
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
);
|
|
198
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
199
|
+
} catch (err) {
|
|
200
|
+
expect(err).toMatchInlineSnapshot(
|
|
201
|
+
`[YError: E_NOT_IN_ENUM (["lang","cn"]): E_NOT_IN_ENUM]`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
qsStrict(
|
|
194
206
|
{},
|
|
195
207
|
qsDefinition,
|
|
196
208
|
'?lang=fr&types=open&types=closed&types=pending&code=3.4&full=true',
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
)
|
|
209
|
+
);
|
|
210
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
211
|
+
} catch (err) {
|
|
212
|
+
expect(err).toMatchInlineSnapshot(
|
|
213
|
+
`[YError: E_PATTERN_DOES_NOT_MATCH (["code",3.4,"^[0-9]+$"]): E_PATTERN_DOES_NOT_MATCH]`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
200
216
|
});
|
|
201
217
|
|
|
202
218
|
test('should work with all params', () => {
|
|
203
|
-
|
|
219
|
+
expect(
|
|
204
220
|
qsStrict(
|
|
205
221
|
{},
|
|
206
222
|
qsDefinition,
|
|
207
223
|
'?lang=fr&types=open&types=closed&types=pending&code=3&full=false&nums=4',
|
|
208
224
|
),
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
);
|
|
225
|
+
).toEqual({
|
|
226
|
+
lang: 'fr',
|
|
227
|
+
types: ['open', 'closed', 'pending'],
|
|
228
|
+
code: 3,
|
|
229
|
+
full: false,
|
|
230
|
+
nums: [4],
|
|
231
|
+
});
|
|
217
232
|
});
|
|
218
233
|
|
|
219
234
|
test('should fail when required params are not in order', () => {
|
|
220
|
-
|
|
235
|
+
try {
|
|
221
236
|
qsStrict(
|
|
222
237
|
{},
|
|
223
238
|
qsDefinition,
|
|
224
239
|
'?lang=fr&types=open&lang=hop&types=closed&types=pending&code=3&full=true',
|
|
225
240
|
);
|
|
226
|
-
|
|
241
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
242
|
+
} catch (err) {
|
|
243
|
+
expect(err).toMatchInlineSnapshot(
|
|
244
|
+
`[YError: E_BAD_QUERY_PARAM_POSITION (["lang",0,2]): E_BAD_QUERY_PARAM_POSITION]`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
227
247
|
});
|
|
228
248
|
|
|
229
249
|
test('should work when required params are not in order with option allowUnorderedParams', () => {
|
|
230
|
-
|
|
250
|
+
expect(
|
|
231
251
|
qsStrict(
|
|
232
252
|
{ allowUnorderedParams: true },
|
|
233
253
|
qsDefinition,
|
|
234
254
|
'?lang=fr&types=open&lang=fr&types=closed&types=pending&code=3&full=true',
|
|
235
255
|
),
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
);
|
|
256
|
+
).toEqual({
|
|
257
|
+
lang: 'fr',
|
|
258
|
+
types: ['open', 'closed', 'pending'],
|
|
259
|
+
code: 3,
|
|
260
|
+
full: true,
|
|
261
|
+
});
|
|
243
262
|
});
|
|
244
263
|
|
|
245
264
|
test('should work when required params are not in order with option allowUnorderedParams and allowUnknownParams', () => {
|
|
246
|
-
|
|
265
|
+
expect(
|
|
247
266
|
qsStrict(
|
|
248
267
|
{ allowUnorderedParams: true, allowUnknownParams: true },
|
|
249
268
|
qsDefinition,
|
|
250
269
|
'?lang=fr&types=open&pippip=yeah&lang=fr&types=closed&types=pending&code=3&full=true',
|
|
251
270
|
),
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
);
|
|
271
|
+
).toEqual({
|
|
272
|
+
lang: 'fr',
|
|
273
|
+
types: ['open', 'closed', 'pending'],
|
|
274
|
+
code: 3,
|
|
275
|
+
full: true,
|
|
276
|
+
});
|
|
259
277
|
});
|
|
260
278
|
|
|
261
279
|
test('should fail when required params are not provided', () => {
|
|
262
|
-
|
|
280
|
+
try {
|
|
263
281
|
qsStrict(
|
|
264
282
|
{},
|
|
265
283
|
qsDefinition,
|
|
266
284
|
'?types=open&types=closed&types=pending&code=3&full=true',
|
|
267
285
|
);
|
|
268
|
-
|
|
286
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
287
|
+
} catch (err) {
|
|
288
|
+
expect(err).toMatchInlineSnapshot(
|
|
289
|
+
`[YError: E_REQUIRED_QUERY_PARAM (["lang"]): E_REQUIRED_QUERY_PARAM]`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
269
292
|
});
|
|
270
293
|
|
|
271
294
|
test('should fail when a bad boolean is provided', () => {
|
|
272
|
-
|
|
295
|
+
try {
|
|
273
296
|
qsStrict(
|
|
274
297
|
{},
|
|
275
298
|
qsDefinition,
|
|
276
299
|
'?lang=fr&types=open&types=closed&types=pending&code=3&full=1',
|
|
277
300
|
);
|
|
278
|
-
|
|
301
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
302
|
+
} catch (err) {
|
|
303
|
+
expect(err).toMatchInlineSnapshot(
|
|
304
|
+
`[YError: E_BAD_BOOLEAN (["1"]): E_BAD_BOOLEAN]`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
279
307
|
});
|
|
280
308
|
|
|
281
309
|
test('should fail when a bad array item is given', () => {
|
|
282
|
-
|
|
310
|
+
try {
|
|
283
311
|
qsStrict(
|
|
284
312
|
{},
|
|
285
313
|
qsDefinition,
|
|
286
314
|
'?lang=fr&types=open&types=closed&types=pending&code=3&full=true&nums=1&nums=0.0',
|
|
287
315
|
);
|
|
288
|
-
|
|
316
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
317
|
+
} catch (err) {
|
|
318
|
+
expect(err).toMatchInlineSnapshot(
|
|
319
|
+
`[YError: E_NON_REENTRANT_NUMBER (["0.0","0"]): E_NON_REENTRANT_NUMBER]`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
289
322
|
});
|
|
290
323
|
|
|
291
324
|
test('should work whith an unexisting param and the allowUnknownParams option', () => {
|
|
292
|
-
|
|
325
|
+
expect(
|
|
293
326
|
qsStrict(
|
|
294
327
|
{ allowUnknownParams: true },
|
|
295
328
|
qsDefinition,
|
|
296
329
|
'?lol=9&lang=fr&types=open&types=closed&types=pending&code=3&full=true',
|
|
297
330
|
),
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
);
|
|
331
|
+
).toEqual({
|
|
332
|
+
code: 3,
|
|
333
|
+
full: true,
|
|
334
|
+
lang: 'fr',
|
|
335
|
+
types: ['open', 'closed', 'pending'],
|
|
336
|
+
});
|
|
305
337
|
});
|
|
306
338
|
|
|
307
339
|
test('should fail when an unexisting param is set at the begin', () => {
|
|
308
|
-
|
|
340
|
+
try {
|
|
309
341
|
qsStrict(
|
|
310
342
|
{},
|
|
311
343
|
qsDefinition,
|
|
312
344
|
'?lol=9&lang=fr&types=open&types=closed&types=pending&code=3&full=true',
|
|
313
345
|
);
|
|
314
|
-
|
|
346
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
347
|
+
} catch (err) {
|
|
348
|
+
expect(err).toMatchInlineSnapshot(
|
|
349
|
+
`[YError: E_UNAUTHORIZED_QUERY_PARAM (["lol"]): E_UNAUTHORIZED_QUERY_PARAM]`,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
315
352
|
});
|
|
316
353
|
|
|
317
354
|
test('should fail when an unexisting param is set in the middle', () => {
|
|
318
|
-
|
|
355
|
+
try {
|
|
319
356
|
qsStrict(
|
|
320
357
|
{},
|
|
321
358
|
qsDefinition,
|
|
322
359
|
'?lang=fr&types=open&types=closed&types=pendinglol=9&&code=3&full=true',
|
|
323
360
|
);
|
|
324
|
-
|
|
361
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
362
|
+
} catch (err) {
|
|
363
|
+
expect(err).toMatchInlineSnapshot(
|
|
364
|
+
`[YError: E_UNAUTHORIZED_QUERY_PARAM ([""]): E_UNAUTHORIZED_QUERY_PARAM]`,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
325
367
|
});
|
|
326
368
|
|
|
327
369
|
test('should fail when an unexisting param is set at the end', () => {
|
|
328
|
-
|
|
370
|
+
try {
|
|
329
371
|
qsStrict(
|
|
330
372
|
{},
|
|
331
373
|
qsDefinition,
|
|
332
374
|
'?lang=fr&types=open&types=closed&types=pending&code=3&full=true&lol=9',
|
|
333
375
|
);
|
|
334
|
-
|
|
376
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
377
|
+
} catch (err) {
|
|
378
|
+
expect(err).toMatchInlineSnapshot(
|
|
379
|
+
`[YError: E_UNAUTHORIZED_QUERY_PARAM (["lol"]): E_UNAUTHORIZED_QUERY_PARAM]`,
|
|
380
|
+
);
|
|
381
|
+
}
|
|
335
382
|
});
|
|
336
383
|
|
|
337
384
|
test('should fail when setting a query param to the default value', () => {
|
|
338
|
-
|
|
385
|
+
try {
|
|
339
386
|
qsStrict(
|
|
340
387
|
{},
|
|
341
388
|
qsDefinition,
|
|
342
389
|
'?lang=en&types=open&types=closed&types=pending&code=3&full=true',
|
|
343
390
|
);
|
|
344
|
-
|
|
391
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
392
|
+
} catch (err) {
|
|
393
|
+
expect(err).toMatchInlineSnapshot(
|
|
394
|
+
`[YError: E_CANNOT_SET_TO_DEFAULT (["lang","en"]): E_CANNOT_SET_TO_DEFAULT]`,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
345
397
|
});
|
|
346
398
|
test('should work when setting a query param to the default value and allowDefault option', () => {
|
|
347
|
-
|
|
399
|
+
expect(
|
|
348
400
|
qsStrict(
|
|
349
401
|
{ allowDefault: true },
|
|
350
402
|
qsDefinition,
|
|
351
403
|
'?lang=en&types=open&types=closed&types=pending&code=3&full=true',
|
|
352
404
|
),
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
);
|
|
405
|
+
).toEqual({
|
|
406
|
+
code: 3,
|
|
407
|
+
full: true,
|
|
408
|
+
lang: 'en',
|
|
409
|
+
types: ['open', 'closed', 'pending'],
|
|
410
|
+
});
|
|
360
411
|
});
|
|
361
412
|
});
|
|
362
413
|
});
|