@based/schema 2.0.0 → 2.1.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/test/array.ts DELETED
@@ -1,388 +0,0 @@
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
- language: '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.only('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
-
124
- t.deepEqual(resultCollect(res, res1), [
125
- { path: ['arrNum'], value: { $delete: true } },
126
- { path: ['arrNum', 0], value: { $default: 1 } },
127
- { path: ['arrNum', 1], value: { $default: 2 } },
128
- // TODO bit sketchy needs some work
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
- })