@based/schema 1.3.2 → 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.
Files changed (71) hide show
  1. package/dist/set/fields/string.d.ts.map +1 -1
  2. package/dist/set/fields/string.js +3 -1
  3. package/dist/set/fields/string.js.map +1 -1
  4. package/dist/set/index.d.ts.map +1 -1
  5. package/dist/set/index.js +3 -1
  6. package/dist/set/index.js.map +1 -1
  7. package/dist/types.d.ts +5 -2
  8. package/dist/types.d.ts.map +1 -1
  9. package/dist/types.js +17 -1
  10. package/dist/types.js.map +1 -1
  11. package/dist/updateSchema.js +1 -1
  12. package/dist/updateSchema.js.map +1 -1
  13. package/dist/validateSchema.d.ts.map +1 -1
  14. package/dist/validateSchema.js +5 -2
  15. package/dist/validateSchema.js.map +1 -1
  16. package/package.json +1 -1
  17. package/dist/set/collections.d.ts +0 -5
  18. package/dist/set/collections.js +0 -211
  19. package/dist/set/collections.js.map +0 -1
  20. package/dist/set/error.d.ts +0 -15
  21. package/dist/set/error.js +0 -24
  22. package/dist/set/error.js.map +0 -1
  23. package/dist/set/number.d.ts +0 -4
  24. package/dist/set/number.js +0 -111
  25. package/dist/set/number.js.map +0 -1
  26. package/dist/set/parseDefaultAndValue.d.ts +0 -3
  27. package/dist/set/parseDefaultAndValue.js +0 -33
  28. package/dist/set/parseDefaultAndValue.js.map +0 -1
  29. package/dist/set/parsers.d.ts +0 -3
  30. package/dist/set/parsers.js +0 -42
  31. package/dist/set/parsers.js.map +0 -1
  32. package/dist/set/references.d.ts +0 -3
  33. package/dist/set/references.js +0 -77
  34. package/dist/set/references.js.map +0 -1
  35. package/dist/set/rest.d.ts +0 -5
  36. package/dist/set/rest.js +0 -76
  37. package/dist/set/rest.js.map +0 -1
  38. package/dist/set/string.d.ts +0 -3
  39. package/dist/set/string.js +0 -173
  40. package/dist/set/string.js.map +0 -1
  41. package/src/error.ts +0 -19
  42. package/src/index.ts +0 -7
  43. package/src/languages.ts +0 -188
  44. package/src/set/fields/array.ts +0 -155
  45. package/src/set/fields/index.ts +0 -70
  46. package/src/set/fields/number.ts +0 -144
  47. package/src/set/fields/object.ts +0 -31
  48. package/src/set/fields/references.ts +0 -140
  49. package/src/set/fields/set.ts +0 -63
  50. package/src/set/fields/string.ts +0 -287
  51. package/src/set/index.ts +0 -182
  52. package/src/set/isValidId.ts +0 -23
  53. package/src/set/types.ts +0 -0
  54. package/src/types.ts +0 -361
  55. package/src/updateSchema.ts +0 -18
  56. package/src/validateSchema.ts +0 -61
  57. package/src/walker/args.ts +0 -209
  58. package/src/walker/index.ts +0 -48
  59. package/src/walker/parse.ts +0 -233
  60. package/src/walker/types.ts +0 -81
  61. package/test/array.ts +0 -388
  62. package/test/number.ts +0 -435
  63. package/test/reference.ts +0 -218
  64. package/test/rest.ts +0 -184
  65. package/test/set.ts +0 -103
  66. package/test/string.ts +0 -118
  67. package/test/text.ts +0 -342
  68. package/test/utils/index.ts +0 -23
  69. package/test/validateSchema.ts +0 -41
  70. package/test/walker.ts +0 -318
  71. package/tsconfig.json +0 -9
package/test/text.ts DELETED
@@ -1,342 +0,0 @@
1
- import { setWalker, BasedSchema } from '../src'
2
- import test from 'ava'
3
- import { ParseError } from '../src/error'
4
- import { resultCollect } from './utils'
5
-
6
- const schema: BasedSchema = {
7
- types: {
8
- thing: {
9
- prefix: 'ti',
10
- fields: {
11
- something: { type: 'string', format: 'strongPassword' },
12
- },
13
- },
14
- bla: {
15
- prefix: 'bl',
16
- fields: {
17
- text: {
18
- type: 'text',
19
- pattern: '[^xz]{1,10}',
20
- },
21
- },
22
- },
23
- },
24
- $defs: {},
25
- languages: ['en', 'de', 'nl', 'ro', 'za', 'ae'],
26
- root: {
27
- fields: {},
28
- },
29
- prefixToTypeMapping: {
30
- bl: 'bla',
31
- ti: 'thing',
32
- },
33
- }
34
-
35
- let r
36
-
37
- test('throw error no language', async (t) => {
38
- let r = await setWalker(schema, {
39
- $id: 'bl120',
40
- text: { $value: 'x' },
41
- })
42
- t.true(r.errors.length > 0)
43
- })
44
-
45
- test('simple case', async (t) => {
46
- let r = await setWalker(schema, {
47
- $id: 'bl120',
48
- text: { en: 'flap' },
49
- })
50
- t.deepEqual(resultCollect(r), [
51
- { path: ['text', 'en'], value: 'flap' },
52
- { path: ['text'], value: { en: 'flap' } },
53
- ])
54
- })
55
-
56
- test('simple case $value', async (t) => {
57
- let r = await setWalker(schema, {
58
- $id: 'bl120',
59
- text: { en: { $value: 'flap' } },
60
- })
61
- t.deepEqual(resultCollect(r), [
62
- { path: ['text', 'en'], value: 'flap' },
63
- { path: ['text'], value: { en: { $value: 'flap' } } },
64
- ])
65
- })
66
-
67
- test('simple case $language', async (t) => {
68
- let r = await setWalker(schema, {
69
- $id: 'bl120',
70
- $language: 'en',
71
- text: 'flap',
72
- })
73
- t.deepEqual(resultCollect(r), [
74
- { path: ['text', 'en'], value: 'flap' },
75
- { path: ['text'], value: { en: 'flap' } },
76
- ])
77
- })
78
-
79
- test('simple case with value', async (t) => {
80
- r = await setWalker(schema, {
81
- $id: 'bl120',
82
- $language: 'za',
83
- text: { $value: 'flap' },
84
- })
85
- t.deepEqual(resultCollect(r), [
86
- { path: ['text', 'za'], value: 'flap' },
87
- { path: ['text'], value: { $value: 'flap' } },
88
- ])
89
- })
90
-
91
- test('simple case $value /w obj', async (t) => {
92
- let r = await setWalker(schema, {
93
- $id: 'bl120',
94
- text: { $value: { en: 'flap' } },
95
- })
96
- t.deepEqual(resultCollect(r), [
97
- { path: ['text', 'en'], value: 'flap' },
98
- { path: ['text'], value: { $value: { en: 'flap' } } },
99
- ])
100
- })
101
-
102
- test('text default', async (t) => {
103
- r = await setWalker(schema, {
104
- $id: 'bl120',
105
- $language: 'za',
106
- text: { $default: 'sdsdds' },
107
- })
108
- t.deepEqual(resultCollect(r), [
109
- { path: ['text', 'za'], value: { $default: 'sdsdds' } },
110
- { path: ['text'], value: { $default: 'sdsdds' } },
111
- ])
112
- })
113
-
114
- test('default and lang:default', async (t) => {
115
- r = await setWalker(schema, {
116
- $id: 'bl120',
117
- $language: 'za',
118
- text: { $default: 'sdsdds', en: { $default: 'flapflap' } },
119
- })
120
-
121
- t.deepEqual(resultCollect(r), [
122
- { path: ['text', 'za'], value: { $default: 'sdsdds' } },
123
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
124
- {
125
- path: ['text'],
126
- value: { $default: 'sdsdds', en: { $default: 'flapflap' } },
127
- },
128
- ])
129
- })
130
-
131
- test('default: lang, lang', async (t) => {
132
- r = await setWalker(schema, {
133
- $id: 'bl120',
134
- $language: 'za',
135
- text: { $default: { de: 'dsnfds' }, en: { $default: 'flapflap' } },
136
- })
137
- t.deepEqual(resultCollect(r), [
138
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
139
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
140
- {
141
- path: ['text'],
142
- value: { $default: { de: 'dsnfds' }, en: { $default: 'flapflap' } },
143
- },
144
- ])
145
- })
146
-
147
- test('defaullt:lang, lang, lang:default', async (t) => {
148
- r = await setWalker(schema, {
149
- $id: 'bl120',
150
- $language: 'za',
151
- text: {
152
- $default: { de: 'dsnfds' },
153
- nl: 'flapperonus',
154
- en: { $default: 'flapflap' },
155
- },
156
- })
157
- t.deepEqual(resultCollect(r), [
158
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
159
- { path: ['text', 'nl'], value: 'flapperonus' },
160
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
161
- {
162
- path: ['text'],
163
- value: {
164
- $default: { de: 'dsnfds' },
165
- nl: 'flapperonus',
166
- en: { $default: 'flapflap' },
167
- },
168
- },
169
- ])
170
- })
171
-
172
- test('default:lang, lang, lang:value, lang:default', async (t) => {
173
- r = await setWalker(schema, {
174
- $id: 'bl120',
175
- $language: 'za',
176
- text: {
177
- $default: { de: 'dsnfds' },
178
- nl: 'flapperonus',
179
- ro: { $value: 'durp' },
180
- en: { $default: 'flapflap' },
181
- },
182
- })
183
-
184
- t.deepEqual(resultCollect(r), [
185
- { path: ['text', 'de'], value: { $default: 'dsnfds' } },
186
- { path: ['text', 'nl'], value: 'flapperonus' },
187
- { path: ['text', 'ro'], value: 'durp' },
188
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
189
- {
190
- path: ['text'],
191
- value: {
192
- $default: { de: 'dsnfds' },
193
- nl: 'flapperonus',
194
- ro: { $value: 'durp' },
195
- en: { $default: 'flapflap' },
196
- },
197
- },
198
- ])
199
- })
200
-
201
- test('value:lang, lang, default:lang, lang:value, lang:default', async (t) => {
202
- r = await setWalker(schema, {
203
- $id: 'bl120',
204
- $language: 'za',
205
- text: {
206
- $value: 'durp',
207
- nl: 'flapperonus',
208
- $default: {
209
- ae: 'habibi',
210
- },
211
- ro: { $value: 'durp' },
212
- en: { $default: 'flapflap' },
213
- },
214
- })
215
-
216
- t.deepEqual(resultCollect(r), [
217
- { path: ['text', 'za'], value: 'durp' },
218
- { path: ['text', 'nl'], value: 'flapperonus' },
219
- { path: ['text', 'ae'], value: { $default: 'habibi' } },
220
- { path: ['text', 'ro'], value: 'durp' },
221
- { path: ['text', 'en'], value: { $default: 'flapflap' } },
222
- {
223
- path: ['text'],
224
- value: {
225
- $value: 'durp',
226
- nl: 'flapperonus',
227
- $default: {
228
- ae: 'habibi',
229
- },
230
- ro: { $value: 'durp' },
231
- en: { $default: 'flapflap' },
232
- },
233
- },
234
- ])
235
- })
236
-
237
- test('value: wrong pattern, lang, default:lang, lang:value, lang:default', async (t) => {
238
- r = await setWalker(schema, {
239
- $id: 'bl120',
240
- $language: 'za',
241
- text: {
242
- $value: 'xz',
243
- nl: 'flapperonus',
244
- $default: {
245
- ae: 'habibi',
246
- },
247
- ro: { $value: 'durp' },
248
- en: { $default: 'xzxz' },
249
- },
250
- })
251
- t.true(r.errors.length > 0)
252
- })
253
-
254
- test('text delete', async (t) => {
255
- r = await setWalker(schema, {
256
- $id: 'bl120',
257
- text: {
258
- $delete: true,
259
- },
260
- })
261
- t.deepEqual(resultCollect(r), [{ path: ['text'], value: { $delete: true } }])
262
- })
263
-
264
- test('text delete single language', async (t) => {
265
- r = await setWalker(schema, {
266
- $id: 'bl120',
267
- text: {
268
- en: {
269
- $delete: true,
270
- },
271
- },
272
- })
273
- t.deepEqual(resultCollect(r), [
274
- { path: ['text', 'en'], value: { $delete: true } },
275
- {
276
- path: ['text'],
277
- value: {
278
- en: {
279
- $delete: true,
280
- },
281
- },
282
- },
283
- ])
284
- })
285
-
286
- test('just delete', async (t) => {
287
- r = await setWalker(schema, {
288
- $id: 'bl120',
289
- $delete: true,
290
- })
291
- t.true(r.errors.length === 1)
292
- })
293
-
294
- test('$default in collected path', async (t) => {
295
- r = await setWalker(schema, {
296
- $id: 'bl120',
297
- text: {
298
- en: {
299
- $default: 'title',
300
- },
301
- },
302
- })
303
- t.is(r.errors.length, 0)
304
- t.deepEqual(resultCollect(r), [
305
- { path: ['text', 'en'], value: { $default: 'title' } },
306
- {
307
- path: ['text'],
308
- value: {
309
- en: {
310
- $default: 'title',
311
- },
312
- },
313
- },
314
- ])
315
- })
316
-
317
- test('$default in collected path + $merge:false', async (t) => {
318
- r = await setWalker(schema, {
319
- $id: 'bl120',
320
- text: {
321
- $merge: false,
322
- en: {
323
- $default: 'title',
324
- },
325
- },
326
- })
327
- t.is(r.errors.length, 0)
328
- t.deepEqual(resultCollect(r), [
329
- { path: ['text'], value: { $delete: true } },
330
-
331
- { path: ['text', 'en'], value: { $default: 'title' } },
332
- {
333
- path: ['text'],
334
- value: {
335
- $merge: false,
336
- en: {
337
- $default: 'title',
338
- },
339
- },
340
- },
341
- ])
342
- })
@@ -1,23 +0,0 @@
1
- import { Path } from '@saulx/utils'
2
- import { BasedSetTarget } from '../../src'
3
- import { ParseError } from '../../src/error'
4
-
5
- export const resultCollect = (...results: BasedSetTarget[]) => {
6
- const assertableResults: { path: Path; value: any }[] = []
7
- for (let i = 0; i < results.length; i++) {
8
- assertableResults.push(
9
- ...results[i].collected.map((v) => ({ path: v.path, value: v.value }))
10
- )
11
- }
12
- return assertableResults
13
- }
14
-
15
- export const errorCollect = (...results: BasedSetTarget[]) => {
16
- const errors: { path: Path; code: ParseError }[] = []
17
- for (let i = 0; i < results.length; i++) {
18
- if (results[i].errors) {
19
- errors.push(...results[i].errors)
20
- }
21
- }
22
- return errors
23
- }
@@ -1,41 +0,0 @@
1
- import test from 'ava'
2
- import { validateSchema } from '../src/index'
3
-
4
- test.serial('throw on invalid schema', async (t) => {
5
- const prefixError = t.throws(() => {
6
- validateSchema({
7
- $defs: {
8
- yuzi: {
9
- type: 'string',
10
- title: 'BLA',
11
- description: 'SNURP',
12
- },
13
- },
14
- types: {
15
- bla: {
16
- prefix: 'fix',
17
- fields: {
18
- yuzi: {
19
- type: 'object',
20
- customValidator: async (value, path, target) => {
21
- return true
22
- },
23
- properties: {
24
- gurt: {
25
- $ref: '/$defs/yuzi',
26
- },
27
- flap: {
28
- enum: ['bla', 'blap', 'flip'],
29
- },
30
- },
31
- },
32
- },
33
- },
34
- },
35
- })
36
- })
37
- t.is(
38
- prefixError.message,
39
- 'Incorrect prefix "fix" for type "bla" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"'
40
- )
41
- })
package/test/walker.ts DELETED
@@ -1,318 +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
- languages: ['en', 'de', 'nl', 'ro', 'za', 'ae'],
114
- root: {
115
- fields: {},
116
- },
117
- prefixToTypeMapping: {
118
- bl: 'bla',
119
- ti: 'thing',
120
- },
121
- }
122
-
123
- test('backtracking', async (t) => {
124
- const results: any[] = []
125
-
126
- const setObj = {
127
- x: {
128
- y: {
129
- a: 10,
130
- bla: [1, 2, 3, 4, 5],
131
- },
132
- c: 40,
133
- },
134
- }
135
-
136
- await walk<{ lullz: true }>(
137
- schema,
138
- {
139
- init: async () => {
140
- return { target: { lullz: true } }
141
- },
142
- parsers: {
143
- keys: {},
144
- fields: {},
145
- any: async (args) => {
146
- args.collect()
147
- return args
148
- },
149
- },
150
- collect: (args) => {
151
- return args.path.join('.')
152
- },
153
- backtrack: (args, fromBt, collected) => {
154
- results.push({ path: args.path, bt: [...fromBt] })
155
- return fromBt.length ? fromBt : collected
156
- },
157
- },
158
- setObj
159
- )
160
-
161
- t.deepEqual(results, [
162
- { path: ['x', 'y', 'bla'], bt: [] },
163
- {
164
- path: ['x', 'y'],
165
- bt: [['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']],
166
- },
167
- {
168
- path: ['x'],
169
- bt: [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
170
- },
171
- {
172
- path: [],
173
- bt: [
174
- [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
175
- ],
176
- },
177
- ])
178
-
179
- const results2: any[] = []
180
-
181
- await walk<{ lullz: true }>(
182
- schema,
183
- {
184
- init: async () => {
185
- return { target: { lullz: true } }
186
- },
187
- parsers: {
188
- keys: {},
189
- fields: {},
190
- any: async (args) => {
191
- args.collect()
192
- return { target: { lullz: true } }
193
- },
194
- },
195
- collect: (args) => {
196
- return args.path.join('.')
197
- },
198
- backtrack: (args, fromBt, collected) => {
199
- results2.push({ path: args.path, bt: [...fromBt] })
200
- return fromBt.length ? fromBt : collected
201
- },
202
- },
203
- setObj
204
- )
205
-
206
- t.deepEqual(results, results2)
207
-
208
- const results3: any[] = []
209
-
210
- let cnt = 0
211
- await walk<{ lullz: true }>(
212
- schema,
213
- {
214
- init: async () => {
215
- return { target: { lullz: true } }
216
- },
217
- parsers: {
218
- keys: {},
219
- fields: {},
220
- any: async (args) => {
221
- cnt++
222
- args.collect()
223
- return cnt % 2 ? args : { target: { lullz: true } }
224
- },
225
- },
226
- collect: (args) => {
227
- return args.path.join('.')
228
- },
229
- backtrack: (args, fromBt, collected) => {
230
- results3.push({ path: args.path, bt: [...fromBt] })
231
- return fromBt.length ? fromBt : collected
232
- },
233
- },
234
- setObj
235
- )
236
-
237
- t.deepEqual(results, results3)
238
- })
239
-
240
- test.only('parseTop update target', async (t) => {
241
- const parsed: { path: (number | string)[]; target: any }[] = []
242
- await walk<any>(
243
- schema,
244
- {
245
- init: async () => {
246
- return { target: { path: [] } }
247
- },
248
- parsers: {
249
- keys: {},
250
- fields: {},
251
- any: async (args) => {
252
- args.collect()
253
- if (args.prev.key !== args.key) {
254
- return {
255
- parseTopLevel: true,
256
- target: {
257
- path: args.path.join('.'),
258
- prevTarget: args.target.id,
259
- id: args.id,
260
- },
261
- value: args.value,
262
- }
263
- }
264
- },
265
- },
266
- collect: (args) => {
267
- parsed.push({ path: args.path, target: args.target })
268
- return args.path.join('.')
269
- },
270
- },
271
- {
272
- x: {
273
- y: {
274
- z: 'bla!',
275
- },
276
- },
277
- }
278
- )
279
-
280
- t.deepEqual(parsed, [
281
- { path: ['x'], target: { path: [] } },
282
- {
283
- path: ['x'],
284
- target: { path: 'x', prevTarget: undefined, id: 2 },
285
- },
286
- {
287
- path: ['x', 'y'],
288
- target: { path: 'x', prevTarget: undefined, id: 2 },
289
- },
290
- { path: ['x', 'y'], target: { path: 'x.y', prevTarget: 2, id: 4 } },
291
- {
292
- path: ['x', 'y', 'z'],
293
- target: { path: 'x.y', prevTarget: 2, id: 4 },
294
- },
295
- {
296
- path: ['x', 'y', 'z'],
297
- target: { path: 'x.y.z', prevTarget: 4, id: 6 },
298
- },
299
- ])
300
- })
301
-
302
- test.serial('perf setWalker', async (t) => {
303
- let d = Date.now()
304
- let collected = 0
305
- let errs = 0
306
- for (let i = 0; i < 1e5; i++) {
307
- const x = await setWalker(schema, {
308
- $id: 'bl120',
309
- name: 'blasdsdsd',
310
- x: { flap: true },
311
- })
312
- errs += x.errors.length
313
- collected += x.collected.length
314
- }
315
- d = Date.now() - d
316
- console.info('setting 200k', d, 'ms')
317
- t.true(d < 1e3)
318
- })