@based/schema 1.1.6 → 1.1.8
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/dist/set/fields/array.js +12 -6
- package/dist/set/fields/array.js.map +1 -1
- package/dist/set/fields/index.js +1 -1
- package/dist/set/fields/index.js.map +1 -1
- package/dist/set/fields/object.js +0 -1
- package/dist/set/fields/object.js.map +1 -1
- package/dist/set/fields/references.js +1 -1
- package/dist/set/fields/references.js.map +1 -1
- package/dist/set/fields/string.js +6 -2
- package/dist/set/fields/string.js.map +1 -1
- package/dist/set/index.js +3 -2
- package/dist/set/index.js.map +1 -1
- package/dist/walker/args.js +0 -7
- package/dist/walker/args.js.map +1 -1
- package/dist/walker/parse.js +6 -15
- package/dist/walker/parse.js.map +1 -1
- package/package.json +1 -1
- package/src/set/fields/array.ts +14 -7
- package/src/set/fields/index.ts +1 -1
- package/src/set/fields/object.ts +0 -3
- package/src/set/fields/references.ts +1 -1
- package/src/set/fields/string.ts +5 -2
- package/src/set/index.ts +3 -3
- package/src/walker/args.ts +0 -9
- package/src/walker/parse.ts +8 -16
- package/test/array.ts +388 -0
- package/test/number.ts +60 -0
- package/test/reference.ts +218 -159
- package/test/rest.ts +148 -113
- package/test/set.ts +80 -106
- package/test/text.ts +37 -4
- package/test/walker.ts +47 -1
package/test/array.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
import { BasedSchema, setWalker } from '../src/index'
|
|
3
|
+
import { errorCollect, resultCollect } from './utils'
|
|
4
|
+
|
|
5
|
+
const schema: BasedSchema = {
|
|
6
|
+
types: {
|
|
7
|
+
bla: {
|
|
8
|
+
prefix: 'bl',
|
|
9
|
+
fields: {
|
|
10
|
+
arrNum: {
|
|
11
|
+
type: 'array',
|
|
12
|
+
values: {
|
|
13
|
+
type: 'number',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
objArray: {
|
|
17
|
+
type: 'array',
|
|
18
|
+
values: {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
snurp: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
arrStr: {
|
|
28
|
+
type: 'array',
|
|
29
|
+
values: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
intarray: {
|
|
34
|
+
type: 'array',
|
|
35
|
+
values: {
|
|
36
|
+
type: 'integer',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
$defs: {},
|
|
43
|
+
languages: ['en'],
|
|
44
|
+
root: {
|
|
45
|
+
fields: {},
|
|
46
|
+
},
|
|
47
|
+
prefixToTypeMapping: {
|
|
48
|
+
bl: 'bla',
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
test('arrayNum', async (t) => {
|
|
53
|
+
const err = await setWalker(schema, {
|
|
54
|
+
$id: 'bl1',
|
|
55
|
+
arrNum: ['1', '2'],
|
|
56
|
+
})
|
|
57
|
+
const err1 = await setWalker(schema, {
|
|
58
|
+
$id: 'bla',
|
|
59
|
+
ref: 1,
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const res = await setWalker(schema, {
|
|
63
|
+
$id: 'bla',
|
|
64
|
+
arrNum: [1, 2],
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
t.true(errorCollect(err, err1).length > 0)
|
|
68
|
+
|
|
69
|
+
t.deepEqual(resultCollect(res), [
|
|
70
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
71
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
72
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
73
|
+
])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('value arr', async (t) => {
|
|
77
|
+
const err = await setWalker(schema, {
|
|
78
|
+
$id: 'bl1',
|
|
79
|
+
arrNum: { $value: ['1', '2'] },
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
t.true(err.errors.length > 1)
|
|
83
|
+
|
|
84
|
+
const res = await setWalker(schema, {
|
|
85
|
+
$id: 'bla',
|
|
86
|
+
arrNum: [{ $value: 1 }, { $value: 2 }],
|
|
87
|
+
})
|
|
88
|
+
const res1 = await setWalker(schema, {
|
|
89
|
+
$id: 'bla',
|
|
90
|
+
arrNum: { $value: [1, 2] },
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
t.deepEqual(resultCollect(res, res1), [
|
|
94
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
95
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
96
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
97
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
98
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
99
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
100
|
+
])
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('default arr', async (t) => {
|
|
104
|
+
const err = await setWalker(schema, {
|
|
105
|
+
$id: 'bl1',
|
|
106
|
+
arrNum: ['1', '2'],
|
|
107
|
+
})
|
|
108
|
+
const err1 = await setWalker(schema, {
|
|
109
|
+
$id: 'bla',
|
|
110
|
+
ref: 1,
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const res = await setWalker(schema, {
|
|
114
|
+
$id: 'bla',
|
|
115
|
+
arrNum: [{ $default: 1 }, { $default: 2 }],
|
|
116
|
+
})
|
|
117
|
+
const res1 = await setWalker(schema, {
|
|
118
|
+
$id: 'bla',
|
|
119
|
+
arrNum: { $default: [1, 2] },
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
t.true(errorCollect(err, err1).length > 0)
|
|
123
|
+
t.deepEqual(resultCollect(res, res1), [
|
|
124
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
125
|
+
{ path: ['arrNum', 0], value: { $default: 1 } },
|
|
126
|
+
{ path: ['arrNum', 1], value: { $default: 2 } },
|
|
127
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
128
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
129
|
+
{ path: ['arrNum'], value: { $default: [1, 2] } },
|
|
130
|
+
])
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
let r
|
|
134
|
+
|
|
135
|
+
test('assign idx value', async (t) => {
|
|
136
|
+
r = await setWalker(schema, {
|
|
137
|
+
$id: 'bl120',
|
|
138
|
+
intarray: {
|
|
139
|
+
$assign: {
|
|
140
|
+
$idx: 0,
|
|
141
|
+
$value: 6,
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
t.deepEqual(resultCollect(r), [{ path: ['intarray', 0], value: 6 }])
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('push ints', async (t) => {
|
|
150
|
+
r = await setWalker(schema, {
|
|
151
|
+
$id: 'bl120',
|
|
152
|
+
intarray: {
|
|
153
|
+
$push: [1, 2, 3, 4, 5],
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
t.deepEqual(resultCollect(r), [
|
|
158
|
+
{ path: ['intarray'], value: { $push: [1, 2, 3, 4, 5] } },
|
|
159
|
+
])
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('push objs', async (t) => {
|
|
163
|
+
r = await setWalker(schema, {
|
|
164
|
+
$id: 'bl120',
|
|
165
|
+
objArray: {
|
|
166
|
+
$push: [{ snurp: 'a' }, { snurp: 'b' }, { snurp: 'c' }],
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
t.deepEqual(resultCollect(r), [
|
|
171
|
+
{
|
|
172
|
+
path: ['objArray'],
|
|
173
|
+
value: {
|
|
174
|
+
$push: [{ snurp: 'a' }, { snurp: 'b' }, { snurp: 'c' }],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{ path: ['objArray', -3, 'snurp'], value: 'a' },
|
|
178
|
+
{ path: ['objArray', -2, 'snurp'], value: 'b' },
|
|
179
|
+
{ path: ['objArray', -1, 'snurp'], value: 'c' },
|
|
180
|
+
{ path: ['objArray', -3], value: { snurp: 'a' } },
|
|
181
|
+
{ path: ['objArray', -2], value: { snurp: 'b' } },
|
|
182
|
+
{ path: ['objArray', -1], value: { snurp: 'c' } },
|
|
183
|
+
])
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('unshift ints', async (t) => {
|
|
187
|
+
r = await setWalker(schema, {
|
|
188
|
+
$id: 'bl120',
|
|
189
|
+
intarray: {
|
|
190
|
+
$unshift: [1, 2, 3, 4, 5],
|
|
191
|
+
},
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
t.deepEqual(resultCollect(r), [
|
|
195
|
+
{ path: ['intarray'], value: { $unshift: [1, 2, 3, 4, 5] } },
|
|
196
|
+
])
|
|
197
|
+
t.true(true)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test('nested default unshift', async (t) => {
|
|
201
|
+
r = await setWalker(schema, {
|
|
202
|
+
$id: 'bl120',
|
|
203
|
+
intarray: {
|
|
204
|
+
$unshift: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
t.is(r.errors.length, 0)
|
|
209
|
+
|
|
210
|
+
t.deepEqual(resultCollect(r), [
|
|
211
|
+
{
|
|
212
|
+
path: ['intarray'],
|
|
213
|
+
value: { $unshift: [1, { $default: 2 }, 3, 4, 5] },
|
|
214
|
+
},
|
|
215
|
+
])
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('nested default in push', async (t) => {
|
|
219
|
+
r = await setWalker(schema, {
|
|
220
|
+
$id: 'bl120',
|
|
221
|
+
intarray: {
|
|
222
|
+
$push: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
|
|
223
|
+
},
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
t.is(r.errors.length, 0)
|
|
227
|
+
|
|
228
|
+
t.deepEqual(resultCollect(r), [
|
|
229
|
+
{
|
|
230
|
+
path: ['intarray'],
|
|
231
|
+
value: { $push: [1, { $default: 2 }, 3, 4, 5] },
|
|
232
|
+
},
|
|
233
|
+
])
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('assign idx default value error', async (t) => {
|
|
237
|
+
r = await setWalker(schema, {
|
|
238
|
+
$id: 'bl120',
|
|
239
|
+
intarray: {
|
|
240
|
+
$assign: {
|
|
241
|
+
$idx: { $default: 0 },
|
|
242
|
+
$value: 6,
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
})
|
|
246
|
+
t.true(r.errors.length > 0)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('assign idx no value', async (t) => {
|
|
250
|
+
r = await setWalker(schema, {
|
|
251
|
+
$id: 'bl120',
|
|
252
|
+
intarray: {
|
|
253
|
+
$assign: {
|
|
254
|
+
$idx: { $default: 0 },
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
})
|
|
258
|
+
t.true(r.errors.length > 0)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('assign idx value spelled wrong', async (t) => {
|
|
262
|
+
r = await setWalker(schema, {
|
|
263
|
+
$id: 'bl120',
|
|
264
|
+
intarray: {
|
|
265
|
+
$assign: {
|
|
266
|
+
$idx: 0,
|
|
267
|
+
value: 5,
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
t.true(r.errors.length > 0)
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test('assign idx value wrong type', async (t) => {
|
|
276
|
+
r = await setWalker(schema, {
|
|
277
|
+
$id: 'bl120',
|
|
278
|
+
intarray: {
|
|
279
|
+
$assign: {
|
|
280
|
+
$idx: 0,
|
|
281
|
+
$value: 5.6,
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
t.true(r.errors.length > 0)
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
test('assign idx value value wrong type', async (t) => {
|
|
290
|
+
r = await setWalker(schema, {
|
|
291
|
+
$id: 'bl120',
|
|
292
|
+
intarray: {
|
|
293
|
+
$assign: {
|
|
294
|
+
$idx: 0,
|
|
295
|
+
$value: { $value: 5.6 },
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
t.true(r.errors.length > 0)
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
test('assign idx default value', async (t) => {
|
|
304
|
+
r = await setWalker(schema, {
|
|
305
|
+
$id: 'bl120',
|
|
306
|
+
intarray: {
|
|
307
|
+
$assign: {
|
|
308
|
+
$idx: 0,
|
|
309
|
+
$value: { $default: 5 },
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
t.deepEqual(resultCollect(r), [
|
|
315
|
+
{ path: ['intarray', 0], value: { $default: 5 } },
|
|
316
|
+
])
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
test('assign idx value value error', async (t) => {
|
|
320
|
+
r = await setWalker(schema, {
|
|
321
|
+
$id: 'bl120',
|
|
322
|
+
intarray: {
|
|
323
|
+
$assign: {
|
|
324
|
+
$idx: { $value: 0 },
|
|
325
|
+
$value: 6,
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
t.true(r.errors.length > 0)
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
test('insert idx intarray', async (t) => {
|
|
334
|
+
r = await setWalker(schema, {
|
|
335
|
+
$id: 'bl120',
|
|
336
|
+
intarray: {
|
|
337
|
+
$insert: {
|
|
338
|
+
$idx: 10,
|
|
339
|
+
$value: 1212,
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
t.true(r.errors.length === 0)
|
|
345
|
+
t.deepEqual(resultCollect(r), [
|
|
346
|
+
{
|
|
347
|
+
path: ['intarray'],
|
|
348
|
+
value: { $insert: { $idx: 10, $value: [1212] } },
|
|
349
|
+
},
|
|
350
|
+
])
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
test('unshift array', async (t) => {
|
|
354
|
+
r = await setWalker(schema, {
|
|
355
|
+
$id: 'bl120',
|
|
356
|
+
intarray: {
|
|
357
|
+
$unshift: { $value: [-10, -20, -30] },
|
|
358
|
+
},
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
t.true(r.errors.length === 0)
|
|
362
|
+
t.deepEqual(resultCollect(r), [
|
|
363
|
+
{ path: ['intarray'], value: { $unshift: [-10, -20, -30] } },
|
|
364
|
+
])
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
test('assign + $delete', async (t) => {
|
|
368
|
+
r = await setWalker(schema, {
|
|
369
|
+
$id: 'bl120',
|
|
370
|
+
objArray: {
|
|
371
|
+
$assign: {
|
|
372
|
+
$idx: 3,
|
|
373
|
+
$value: {
|
|
374
|
+
snurp: {
|
|
375
|
+
$delete: true,
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
t.is(r.errors.length, 0)
|
|
383
|
+
|
|
384
|
+
t.deepEqual(resultCollect(r), [
|
|
385
|
+
{ path: ['objArray', 3, 'snurp'], value: { $delete: true } },
|
|
386
|
+
{ path: ['objArray', 3], value: { snurp: { $delete: true } } },
|
|
387
|
+
])
|
|
388
|
+
})
|
package/test/number.ts
CHANGED
|
@@ -20,6 +20,12 @@ const schema: BasedSchema = {
|
|
|
20
20
|
maximum: 6,
|
|
21
21
|
minimum: 3,
|
|
22
22
|
},
|
|
23
|
+
infiniteNum: {
|
|
24
|
+
type: 'number',
|
|
25
|
+
},
|
|
26
|
+
infiniteInt: {
|
|
27
|
+
type: 'integer',
|
|
28
|
+
},
|
|
23
29
|
exclusiveminmax: {
|
|
24
30
|
type: 'number',
|
|
25
31
|
minimum: 3,
|
|
@@ -395,3 +401,57 @@ test('increment', async (t) => {
|
|
|
395
401
|
{ path: ['multipleOf'], value: { $increment: 9 } },
|
|
396
402
|
])
|
|
397
403
|
})
|
|
404
|
+
|
|
405
|
+
let r
|
|
406
|
+
|
|
407
|
+
test('NaN', async (t) => {
|
|
408
|
+
r = await setWalker(schema, {
|
|
409
|
+
$id: 'bl120',
|
|
410
|
+
integer: NaN,
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
t.true(r.errors.length === 1)
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
test('Infinity', async (t) => {
|
|
417
|
+
r = await setWalker(schema, {
|
|
418
|
+
$id: 'bl120',
|
|
419
|
+
integer: Infinity,
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
console.dir(r.errors)
|
|
423
|
+
console.dir(
|
|
424
|
+
r.collected.map((v) => ({ path: v.path, value: v.value })),
|
|
425
|
+
{ depth: 10 }
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
t.true(true)
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
//infinity does exceed maximum but doesnt error when no max
|
|
432
|
+
test('number infinity', async (t) => {
|
|
433
|
+
r = await setWalker(schema, {
|
|
434
|
+
$id: 'bl120',
|
|
435
|
+
infiniteNum: Infinity,
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
t.true(r.errors.length === 1)
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
test('number -infinity', async (t) => {
|
|
442
|
+
r = await setWalker(schema, {
|
|
443
|
+
$id: 'bl120',
|
|
444
|
+
infiniteNum: -Infinity,
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
t.true(r.errors.length === 1)
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
test('number with max infinity', async (t) => {
|
|
451
|
+
r = await setWalker(schema, {
|
|
452
|
+
$id: 'bl120',
|
|
453
|
+
number: Infinity,
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
t.true(r.errors.length === 1)
|
|
457
|
+
})
|