@based/schema 1.1.5 → 1.1.7

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/test/array.ts ADDED
@@ -0,0 +1,369 @@
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
+ const err1 = await setWalker(schema, {
82
+ $id: 'bla',
83
+ ref: { $value: 1 },
84
+ })
85
+
86
+ const res = await setWalker(schema, {
87
+ $id: 'bla',
88
+ arrNum: [{ $value: 1 }, { $value: 2 }],
89
+ })
90
+ const res1 = await setWalker(schema, {
91
+ $id: 'bla',
92
+ arrNum: { $value: [1, 2] },
93
+ })
94
+
95
+ t.true(errorCollect(err, err1).length > 0)
96
+
97
+ t.deepEqual(resultCollect(res, res1), [
98
+ { path: ['arrNum'], value: { $delete: true } },
99
+ { path: ['arrNum', 0], value: 1 },
100
+ { path: ['arrNum', 1], value: 2 },
101
+ { path: ['arrNum'], value: { $delete: true } },
102
+ { path: ['arrNum', 0], value: 1 },
103
+ { path: ['arrNum', 1], value: 2 },
104
+ { path: ['arrNum'], value: { $delete: true } },
105
+ ])
106
+ })
107
+
108
+ test('default arr', async (t) => {
109
+ const err = await setWalker(schema, {
110
+ $id: 'bl1',
111
+ arrNum: ['1', '2'],
112
+ })
113
+ const err1 = await setWalker(schema, {
114
+ $id: 'bla',
115
+ ref: 1,
116
+ })
117
+
118
+ const res = await setWalker(schema, {
119
+ $id: 'bla',
120
+ arrNum: [{ $default: 1 }, { $default: 2 }],
121
+ })
122
+ const res1 = await setWalker(schema, {
123
+ $id: 'bla',
124
+ arrNum: { $default: [1, 2] },
125
+ })
126
+
127
+ t.true(errorCollect(err, err1).length > 0)
128
+ t.deepEqual(resultCollect(res, res1), [
129
+ { path: ['arrNum'], value: { $delete: true } },
130
+ { path: ['arrNum', 0], value: { $default: 1 } },
131
+ { path: ['arrNum', 1], value: { $default: 2 } },
132
+ { path: ['arrNum', 0], value: 1 },
133
+ { path: ['arrNum', 1], value: 2 },
134
+ { path: ['arrNum'], value: { $default: [1, 2] } },
135
+ ])
136
+ })
137
+
138
+ let r
139
+
140
+ test('assign idx value', async (t) => {
141
+ r = await setWalker(schema, {
142
+ $id: 'bl120',
143
+ intarray: {
144
+ $assign: {
145
+ $idx: 0,
146
+ $value: 6,
147
+ },
148
+ },
149
+ })
150
+
151
+ t.deepEqual(resultCollect(r), [{ path: ['intarray', 0], value: 6 }])
152
+ })
153
+
154
+ test('push ints', async (t) => {
155
+ r = await setWalker(schema, {
156
+ $id: 'bl120',
157
+ intarray: {
158
+ $push: [1, 2, 3, 4, 5],
159
+ },
160
+ })
161
+
162
+ t.deepEqual(resultCollect(r), [
163
+ { path: ['intarray'], value: { $push: [1, 2, 3, 4, 5] } },
164
+ ])
165
+ })
166
+
167
+ test('unshift ints', async (t) => {
168
+ r = await setWalker(schema, {
169
+ $id: 'bl120',
170
+ intarray: {
171
+ $unshift: [1, 2, 3, 4, 5],
172
+ },
173
+ })
174
+
175
+ t.deepEqual(resultCollect(r), [
176
+ { path: ['intarray'], value: { $unshift: [1, 2, 3, 4, 5] } },
177
+ ])
178
+ t.true(true)
179
+ })
180
+
181
+ test('nested default unshift', async (t) => {
182
+ r = await setWalker(schema, {
183
+ $id: 'bl120',
184
+ intarray: {
185
+ $unshift: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
186
+ },
187
+ })
188
+
189
+ t.is(r.errors.length, 0)
190
+
191
+ t.deepEqual(resultCollect(r), [
192
+ {
193
+ path: ['intarray'],
194
+ value: { $unshift: [1, { $default: 2 }, 3, 4, 5] },
195
+ },
196
+ ])
197
+ })
198
+
199
+ test('nested default in push', async (t) => {
200
+ r = await setWalker(schema, {
201
+ $id: 'bl120',
202
+ intarray: {
203
+ $push: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
204
+ },
205
+ })
206
+
207
+ t.is(r.errors.length, 0)
208
+
209
+ t.deepEqual(resultCollect(r), [
210
+ {
211
+ path: ['intarray'],
212
+ value: { $push: [1, { $default: 2 }, 3, 4, 5] },
213
+ },
214
+ ])
215
+ })
216
+
217
+ test('assign idx default value error', async (t) => {
218
+ r = await setWalker(schema, {
219
+ $id: 'bl120',
220
+ intarray: {
221
+ $assign: {
222
+ $idx: { $default: 0 },
223
+ $value: 6,
224
+ },
225
+ },
226
+ })
227
+ t.true(r.errors.length > 0)
228
+ })
229
+
230
+ test('assign idx no value', async (t) => {
231
+ r = await setWalker(schema, {
232
+ $id: 'bl120',
233
+ intarray: {
234
+ $assign: {
235
+ $idx: { $default: 0 },
236
+ },
237
+ },
238
+ })
239
+ t.true(r.errors.length > 0)
240
+ })
241
+
242
+ test('assign idx value spelled wrong', async (t) => {
243
+ r = await setWalker(schema, {
244
+ $id: 'bl120',
245
+ intarray: {
246
+ $assign: {
247
+ $idx: 0,
248
+ value: 5,
249
+ },
250
+ },
251
+ })
252
+
253
+ t.true(r.errors.length > 0)
254
+ })
255
+
256
+ test('assign idx value wrong type', async (t) => {
257
+ r = await setWalker(schema, {
258
+ $id: 'bl120',
259
+ intarray: {
260
+ $assign: {
261
+ $idx: 0,
262
+ $value: 5.6,
263
+ },
264
+ },
265
+ })
266
+
267
+ t.true(r.errors.length > 0)
268
+ })
269
+
270
+ test('assign idx value value wrong type', async (t) => {
271
+ r = await setWalker(schema, {
272
+ $id: 'bl120',
273
+ intarray: {
274
+ $assign: {
275
+ $idx: 0,
276
+ $value: { $value: 5.6 },
277
+ },
278
+ },
279
+ })
280
+
281
+ t.true(r.errors.length > 0)
282
+ })
283
+
284
+ test('assign idx default value', async (t) => {
285
+ r = await setWalker(schema, {
286
+ $id: 'bl120',
287
+ intarray: {
288
+ $assign: {
289
+ $idx: 0,
290
+ $value: { $default: 5 },
291
+ },
292
+ },
293
+ })
294
+
295
+ t.deepEqual(resultCollect(r), [
296
+ { path: ['intarray', 0], value: { $default: 5 } },
297
+ ])
298
+ })
299
+
300
+ test('assign idx value value error', async (t) => {
301
+ r = await setWalker(schema, {
302
+ $id: 'bl120',
303
+ intarray: {
304
+ $assign: {
305
+ $idx: { $value: 0 },
306
+ $value: 6,
307
+ },
308
+ },
309
+ })
310
+
311
+ t.true(r.errors.length > 0)
312
+ })
313
+
314
+ test('insert idx intarray', async (t) => {
315
+ r = await setWalker(schema, {
316
+ $id: 'bl120',
317
+ intarray: {
318
+ $insert: {
319
+ $idx: 10,
320
+ $value: 1212,
321
+ },
322
+ },
323
+ })
324
+
325
+ t.true(r.errors.length === 0)
326
+ t.deepEqual(resultCollect(r), [
327
+ {
328
+ path: ['intarray'],
329
+ value: { $insert: { $idx: 10, $value: [1212] } },
330
+ },
331
+ ])
332
+ })
333
+
334
+ test('unshift array', async (t) => {
335
+ r = await setWalker(schema, {
336
+ $id: 'bl120',
337
+ intarray: {
338
+ $unshift: { $value: [-10, -20, -30] },
339
+ },
340
+ })
341
+
342
+ t.true(r.errors.length === 0)
343
+ t.deepEqual(resultCollect(r), [
344
+ { path: ['intarray'], value: { $unshift: [-10, -20, -30] } },
345
+ ])
346
+ })
347
+
348
+ test('assign + $delete', async (t) => {
349
+ r = await setWalker(schema, {
350
+ $id: 'bl120',
351
+ objArray: {
352
+ $assign: {
353
+ $idx: 3,
354
+ $value: {
355
+ snurp: {
356
+ $delete: true,
357
+ },
358
+ },
359
+ },
360
+ },
361
+ })
362
+
363
+ t.is(r.errors.length, 0)
364
+
365
+ t.deepEqual(resultCollect(r), [
366
+ { path: ['objArray', 3, 'snurp'], value: { $delete: true } },
367
+ { path: ['objArray', 3], value: { snurp: { $delete: true } } },
368
+ ])
369
+ })
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
+ })