@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/walker.ts DELETED
@@ -1,319 +0,0 @@
1
- import test from 'ava'
2
- import { BasedSchema, setWalker, walk } from '../src/index'
3
- import { wait } from '@saulx/utils'
4
-
5
- const schema: BasedSchema = {
6
- types: {
7
- thing: {
8
- prefix: 'ti',
9
- fields: {
10
- priority: { type: 'number' },
11
- something: { type: 'string', format: 'strongPassword' },
12
- },
13
- },
14
- bla: {
15
- prefix: 'bl',
16
- fields: {
17
- referencesToThings: {
18
- type: 'references',
19
- allowedTypes: ['thing'],
20
- },
21
- referenceToThing: {
22
- type: 'reference',
23
- allowedTypes: ['thing'],
24
- },
25
- enum: {
26
- enum: ['tony', 'jim'],
27
- },
28
- exclusiveminmax: {
29
- type: 'number',
30
- minimum: 3,
31
- exclusiveMinimum: true,
32
- maximum: 6,
33
- exclusiveMaximum: true,
34
- },
35
- text: {
36
- type: 'text',
37
- pattern: '[^xz]{1,10}',
38
- },
39
- timestamp: {
40
- type: 'timestamp',
41
- },
42
- setOfNumbers: {
43
- type: 'set',
44
- items: {
45
- type: 'number',
46
- },
47
- },
48
- intarray: {
49
- type: 'array',
50
- values: {
51
- type: 'integer',
52
- },
53
- },
54
- object: {
55
- type: 'object',
56
- properties: {
57
- flap: { type: 'boolean' },
58
- },
59
- },
60
- name: {
61
- minLength: 3,
62
- maxLength: 6,
63
- type: 'string',
64
- },
65
- phonkName: {
66
- type: 'string',
67
- pattern: '\\${1,4}',
68
- },
69
- flap: {
70
- type: 'boolean',
71
- },
72
- x: {
73
- type: 'object',
74
- properties: {
75
- flap: {
76
- type: 'boolean',
77
- },
78
- },
79
- },
80
- record: {
81
- type: 'record',
82
- values: {
83
- type: 'object',
84
- properties: {
85
- bla: {
86
- type: 'array',
87
- values: {
88
- type: 'object',
89
- properties: {
90
- snux: {
91
- type: 'object',
92
- properties: {
93
- x: {
94
- type: 'number',
95
- },
96
- },
97
- },
98
- flap: { type: 'number' },
99
- },
100
- },
101
- },
102
- },
103
- },
104
- },
105
- bla: {
106
- type: 'set',
107
- items: { type: 'string', minLength: 3, maxLength: 6 },
108
- },
109
- },
110
- },
111
- },
112
- $defs: {},
113
- language: 'en',
114
- translations: ['de', 'nl', 'ro', 'za', 'ae'],
115
- root: {
116
- fields: {},
117
- },
118
- prefixToTypeMapping: {
119
- bl: 'bla',
120
- ti: 'thing',
121
- },
122
- }
123
-
124
- test('backtracking', async (t) => {
125
- const results: any[] = []
126
-
127
- const setObj = {
128
- x: {
129
- y: {
130
- a: 10,
131
- bla: [1, 2, 3, 4, 5],
132
- },
133
- c: 40,
134
- },
135
- }
136
-
137
- await walk<{ lullz: true }>(
138
- schema,
139
- {
140
- init: async () => {
141
- return { target: { lullz: true } }
142
- },
143
- parsers: {
144
- keys: {},
145
- fields: {},
146
- any: async (args) => {
147
- args.collect()
148
- return args
149
- },
150
- },
151
- collect: (args) => {
152
- return args.path.join('.')
153
- },
154
- backtrack: (args, fromBt, collected) => {
155
- results.push({ path: args.path, bt: [...fromBt] })
156
- return fromBt.length ? fromBt : collected
157
- },
158
- },
159
- setObj
160
- )
161
-
162
- t.deepEqual(results, [
163
- { path: ['x', 'y', 'bla'], bt: [] },
164
- {
165
- path: ['x', 'y'],
166
- bt: [['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']],
167
- },
168
- {
169
- path: ['x'],
170
- bt: [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
171
- },
172
- {
173
- path: [],
174
- bt: [
175
- [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
176
- ],
177
- },
178
- ])
179
-
180
- const results2: any[] = []
181
-
182
- await walk<{ lullz: true }>(
183
- schema,
184
- {
185
- init: async () => {
186
- return { target: { lullz: true } }
187
- },
188
- parsers: {
189
- keys: {},
190
- fields: {},
191
- any: async (args) => {
192
- args.collect()
193
- return { target: { lullz: true } }
194
- },
195
- },
196
- collect: (args) => {
197
- return args.path.join('.')
198
- },
199
- backtrack: (args, fromBt, collected) => {
200
- results2.push({ path: args.path, bt: [...fromBt] })
201
- return fromBt.length ? fromBt : collected
202
- },
203
- },
204
- setObj
205
- )
206
-
207
- t.deepEqual(results, results2)
208
-
209
- const results3: any[] = []
210
-
211
- let cnt = 0
212
- await walk<{ lullz: true }>(
213
- schema,
214
- {
215
- init: async () => {
216
- return { target: { lullz: true } }
217
- },
218
- parsers: {
219
- keys: {},
220
- fields: {},
221
- any: async (args) => {
222
- cnt++
223
- args.collect()
224
- return cnt % 2 ? args : { target: { lullz: true } }
225
- },
226
- },
227
- collect: (args) => {
228
- return args.path.join('.')
229
- },
230
- backtrack: (args, fromBt, collected) => {
231
- results3.push({ path: args.path, bt: [...fromBt] })
232
- return fromBt.length ? fromBt : collected
233
- },
234
- },
235
- setObj
236
- )
237
-
238
- t.deepEqual(results, results3)
239
- })
240
-
241
- test.only('parseTop update target', async (t) => {
242
- const parsed: { path: (number | string)[]; target: any }[] = []
243
- await walk<any>(
244
- schema,
245
- {
246
- init: async () => {
247
- return { target: { path: [] } }
248
- },
249
- parsers: {
250
- keys: {},
251
- fields: {},
252
- any: async (args) => {
253
- args.collect()
254
- if (args.prev.key !== args.key) {
255
- return {
256
- parseTopLevel: true,
257
- target: {
258
- path: args.path.join('.'),
259
- prevTarget: args.target.id,
260
- id: args.id,
261
- },
262
- value: args.value,
263
- }
264
- }
265
- },
266
- },
267
- collect: (args) => {
268
- parsed.push({ path: args.path, target: args.target })
269
- return args.path.join('.')
270
- },
271
- },
272
- {
273
- x: {
274
- y: {
275
- z: 'bla!',
276
- },
277
- },
278
- }
279
- )
280
-
281
- t.deepEqual(parsed, [
282
- { path: ['x'], target: { path: [] } },
283
- {
284
- path: ['x'],
285
- target: { path: 'x', prevTarget: undefined, id: 2 },
286
- },
287
- {
288
- path: ['x', 'y'],
289
- target: { path: 'x', prevTarget: undefined, id: 2 },
290
- },
291
- { path: ['x', 'y'], target: { path: 'x.y', prevTarget: 2, id: 4 } },
292
- {
293
- path: ['x', 'y', 'z'],
294
- target: { path: 'x.y', prevTarget: 2, id: 4 },
295
- },
296
- {
297
- path: ['x', 'y', 'z'],
298
- target: { path: 'x.y.z', prevTarget: 4, id: 6 },
299
- },
300
- ])
301
- })
302
-
303
- test.serial('perf setWalker', async (t) => {
304
- let d = Date.now()
305
- let collected = 0
306
- let errs = 0
307
- for (let i = 0; i < 1e5; i++) {
308
- const x = await setWalker(schema, {
309
- $id: 'bl120',
310
- name: 'blasdsdsd',
311
- x: { flap: true },
312
- })
313
- errs += x.errors.length
314
- collected += x.collected.length
315
- }
316
- d = Date.now() - d
317
- console.info('setting 200k', d, 'ms')
318
- t.true(d < 1e3)
319
- })
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "dist"
6
- },
7
- "include": ["src/**/*", "src/**/*.json"],
8
- "exclude": ["node_modules", "test", "dist", "tmp", "examples"]
9
- }