@based/schema 1.0.8 → 1.0.10

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/text.ts ADDED
@@ -0,0 +1,199 @@
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
+
43
+ t.true(r.errors.length > 0)
44
+ })
45
+
46
+ test.only('simple case', async (t) => {
47
+ let r = await setWalker(schema, {
48
+ $id: 'bl120',
49
+ text: { en: 'flap' },
50
+ })
51
+ t.deepEqual(resultCollect(r), [{ path: ['text'], value: { en: 'flap' } }])
52
+ t.true(r.errors.length > 0)
53
+ })
54
+
55
+ test('string max length', async (t) => {
56
+ r = await setWalker(schema, {
57
+ $id: 'bl120',
58
+ $language: 'za',
59
+ text: { $value: 'sdsdds' },
60
+ })
61
+
62
+ // TPDP MAKE INTO TEST
63
+ console.log(
64
+ r.errors,
65
+ r.collected.map((v) => ({ path: v.path, value: v.value }))
66
+ )
67
+
68
+ t.true(true)
69
+ })
70
+
71
+ test('text default', async (t) => {
72
+ r = await setWalker(schema, {
73
+ $id: 'bl120',
74
+ $language: 'za',
75
+ text: { $default: 'sdsdds' },
76
+ })
77
+
78
+ t.deepEqual(resultCollect(r), [
79
+ { path: ['text'], value: { $default: { za: 'sdsdds' } } },
80
+ ])
81
+ })
82
+
83
+ test('default and lang:default', async (t) => {
84
+ r = await setWalker(schema, {
85
+ $id: 'bl120',
86
+ $language: 'za',
87
+ text: { $default: 'sdsdds', en: { $default: 'flapflap' } },
88
+ })
89
+
90
+ t.deepEqual(resultCollect(r), [
91
+ {
92
+ path: ['text'],
93
+ value: { $default: { za: 'sdsdds', en: 'flapflap' } },
94
+ },
95
+ ])
96
+ })
97
+
98
+ test('default: lang, lang', async (t) => {
99
+ r = await setWalker(schema, {
100
+ $id: 'bl120',
101
+ $language: 'za',
102
+ text: { $default: { de: 'dsnfds' }, en: { $default: 'flapflap' } },
103
+ })
104
+
105
+ t.deepEqual(resultCollect(r), [
106
+ {
107
+ path: ['text'],
108
+ value: { $default: { de: 'dsnfds', en: 'flapflap' } },
109
+ },
110
+ ])
111
+ })
112
+
113
+ test('defaullt:lang, lang, lang:default', async (t) => {
114
+ r = await setWalker(schema, {
115
+ $id: 'bl120',
116
+ $language: 'za',
117
+ text: {
118
+ $default: { de: 'dsnfds' },
119
+ nl: 'flapperonus',
120
+ en: { $default: 'flapflap' },
121
+ },
122
+ })
123
+
124
+ t.deepEqual(resultCollect(r), [
125
+ {
126
+ path: ['text'],
127
+ value: { $default: { de: 'dsnfds', en: 'flapflap' }, nl: 'flapperonus' },
128
+ },
129
+ ])
130
+ })
131
+
132
+ test('default:lang, lang, lang:value, lang:default', async (t) => {
133
+ r = await setWalker(schema, {
134
+ $id: 'bl120',
135
+ $language: 'za',
136
+ text: {
137
+ $default: { de: 'dsnfds' },
138
+ nl: 'flapperonus',
139
+ ro: { $value: 'durp' },
140
+ en: { $default: 'flapflap' },
141
+ },
142
+ })
143
+
144
+ t.deepEqual(resultCollect(r), [
145
+ {
146
+ path: ['text'],
147
+ value: {
148
+ $default: { de: 'dsnfds', en: 'flapflap' },
149
+ nl: 'flapperonus',
150
+ ro: 'durp',
151
+ },
152
+ },
153
+ ])
154
+ })
155
+
156
+ test('value:lang, lang, default:lang, lang:value, lang:default', async (t) => {
157
+ r = await setWalker(schema, {
158
+ $id: 'bl120',
159
+ $language: 'za',
160
+ text: {
161
+ $value: 'durp',
162
+ nl: 'flapperonus',
163
+ $default: {
164
+ ae: 'habibi',
165
+ },
166
+ ro: { $value: 'durp' },
167
+ en: { $default: 'flapflap' },
168
+ },
169
+ })
170
+
171
+ t.deepEqual(resultCollect(r), [
172
+ {
173
+ path: ['text'],
174
+ value: {
175
+ nl: 'flapperonus',
176
+ $default: { ae: 'habibi', en: 'flapflap' },
177
+ ro: 'durp',
178
+ },
179
+ },
180
+ ])
181
+ })
182
+
183
+ test('value:wrongpatter, lang, default:lang, lang:value, lang:default', async (t) => {
184
+ r = await setWalker(schema, {
185
+ $id: 'bl120',
186
+ $language: 'za',
187
+ text: {
188
+ $value: 'xz',
189
+ nl: 'flapperonus',
190
+ $default: {
191
+ ae: 'habibi',
192
+ },
193
+ ro: { $value: 'durp' },
194
+ en: { $default: 'xzxz' },
195
+ },
196
+ })
197
+
198
+ t.assert(r.errors.length === 4)
199
+ })
package/test/walker.ts CHANGED
@@ -21,6 +21,9 @@ const schema: BasedSchema = {
21
21
  type: 'reference',
22
22
  allowedTypes: ['thing'],
23
23
  },
24
+ enum: {
25
+ enum: ['tony', 'jim'],
26
+ },
24
27
  exclusiveminmax: {
25
28
  type: 'number',
26
29
  minimum: 3,
@@ -85,8 +88,20 @@ const schema: BasedSchema = {
85
88
  },
86
89
  }
87
90
 
88
- test.only('walker', async (t) => {
89
- const x = await walk<{ lullz: true }>(
91
+ test('walker', async (t) => {
92
+ const results: any[] = []
93
+
94
+ const setObj = {
95
+ x: {
96
+ y: {
97
+ a: 10,
98
+ bla: [1, 2, 3, 4, 5],
99
+ },
100
+ c: 40,
101
+ },
102
+ }
103
+
104
+ await walk<{ lullz: true }>(
90
105
  schema,
91
106
  {
92
107
  init: async () => {
@@ -96,56 +111,100 @@ test.only('walker', async (t) => {
96
111
  keys: {},
97
112
  fields: {},
98
113
  any: async (args) => {
99
- args.collect(args)
114
+ args.collect()
100
115
  return args
101
116
  },
102
117
  },
103
118
  collect: (args) => {
104
- console.info('..', args.path)
105
119
  return args.path.join('.')
106
120
  },
107
121
  backtrack: (args, fromBt, collected) => {
108
- console.log(
109
- ' \n-----------BACK TRACK GOOD GO',
110
- '\n',
111
- 'path:',
112
- args.path.join('.'),
113
- '\n',
114
- 'backtracked:',
115
- JSON.stringify(fromBt),
116
- '\n',
117
- 'collected:',
118
- collected,
119
- '--------------------'
120
- )
122
+ results.push({ path: args.path, bt: [...fromBt] })
121
123
  return fromBt.length ? fromBt : collected
122
124
  },
123
125
  },
126
+ setObj
127
+ )
128
+
129
+ t.deepEqual(results, [
130
+ { path: ['x', 'y', 'bla'], bt: [] },
124
131
  {
125
- gurk: [1, 2, 3, 4],
126
- x: {
127
- y: {
128
- z: {
129
- a: 10,
130
- b: 20,
131
- gur: {
132
- x: true,
133
- y: true,
134
- $list: {
135
- $sort: true,
136
- },
137
- },
138
- c: 40,
139
- $list: true,
140
- },
132
+ path: ['x', 'y'],
133
+ bt: [['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']],
134
+ },
135
+ {
136
+ path: ['x'],
137
+ bt: [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
138
+ },
139
+ {
140
+ path: [],
141
+ bt: [
142
+ [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
143
+ ],
144
+ },
145
+ ])
146
+
147
+ console.log('--------------------------------------')
148
+ const results2: any[] = []
149
+
150
+ await walk<{ lullz: true }>(
151
+ schema,
152
+ {
153
+ init: async () => {
154
+ return { target: { lullz: true } }
155
+ },
156
+ parsers: {
157
+ keys: {},
158
+ fields: {},
159
+ any: async (args) => {
160
+ args.collect()
161
+ return { target: { lullz: true } }
141
162
  },
142
163
  },
143
- }
164
+ collect: (args) => {
165
+ return args.path.join('.')
166
+ },
167
+ backtrack: (args, fromBt, collected) => {
168
+ results2.push({ path: args.path, bt: [...fromBt] })
169
+ return fromBt.length ? fromBt : collected
170
+ },
171
+ },
172
+ setObj
144
173
  )
145
174
 
146
- console.info('------------')
175
+ t.deepEqual(results, results2)
147
176
 
148
- t.true(true)
177
+ console.log('--------------------------------------')
178
+ const results3: any[] = []
179
+
180
+ let cnt = 0
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
+ cnt++
192
+ args.collect()
193
+ return cnt % 2 ? args : { target: { lullz: true } }
194
+ },
195
+ },
196
+ collect: (args) => {
197
+ return args.path.join('.')
198
+ },
199
+ backtrack: (args, fromBt, collected) => {
200
+ results3.push({ path: args.path, bt: [...fromBt] })
201
+ return fromBt.length ? fromBt : collected
202
+ },
203
+ },
204
+ setObj
205
+ )
206
+
207
+ t.deepEqual(results, results3)
149
208
  })
150
209
 
151
210
  test('set walker', async (t) => {
@@ -251,7 +310,7 @@ test('set walker', async (t) => {
251
310
  t.true(true)
252
311
  })
253
312
 
254
- test('perf setWalker', async (t) => {
313
+ test.serial('perf setWalker', async (t) => {
255
314
  let d = Date.now()
256
315
  let collected = 0
257
316
  let errs = 0
@@ -264,6 +323,8 @@ test('perf setWalker', async (t) => {
264
323
  errs += x.errors.length
265
324
  collected += x.collected.length
266
325
  }
326
+ d = Date.now() - d
327
+ console.info('setting 200k', d, 'ms')
267
328
  t.true(d < 1e3)
268
329
  })
269
330
 
@@ -664,10 +725,6 @@ test('string', async (t) => {
664
725
  },
665
726
  })
666
727
 
667
- r.collected.forEach((v) => {
668
- console.info(v.root.typeSchema)
669
- })
670
-
671
728
  console.log(r.errors)
672
729
  console.dir(
673
730
  r.collected.map((v) => ({ path: v.path, value: v.value })),
@@ -682,10 +739,6 @@ test('string', async (t) => {
682
739
  },
683
740
  })
684
741
 
685
- r.collected.forEach((v) => {
686
- console.info(v.root.typeSchema)
687
- })
688
-
689
742
  console.log(r.errors)
690
743
  console.dir(
691
744
  r.collected.map((v) => ({ path: v.path, value: v.value })),
@@ -700,11 +753,95 @@ test('string', async (t) => {
700
753
  },
701
754
  })
702
755
 
703
- r.collected.forEach((v) => {
704
- console.info(v.root.typeSchema)
756
+ console.log(r.errors)
757
+ console.dir(
758
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
759
+ { depth: 10 }
760
+ )
761
+
762
+ console.info('---- doink 29 ------')
763
+ r = await setWalker(schema, {
764
+ $id: 'bl120',
765
+ text: {
766
+ en: 'bla',
767
+ },
768
+ })
769
+
770
+ console.dir(
771
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
772
+ { depth: 10 }
773
+ )
774
+
775
+ t.true(true)
776
+
777
+ console.info('---- doink 30 ------')
778
+ r = await setWalker(schema, {
779
+ $id: 'bl120',
780
+ text: {
781
+ $delete: true,
782
+ },
705
783
  })
706
784
 
707
- console.log(r.errors)
785
+ console.dir(
786
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
787
+ { depth: 10 }
788
+ )
789
+
790
+ console.info('---- doink 31 ------')
791
+ r = await setWalker(schema, {
792
+ $id: 'bl120',
793
+ $delete: true,
794
+ })
795
+
796
+ console.dir(r.errors)
797
+ console.dir(
798
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
799
+ { depth: 10 }
800
+ )
801
+
802
+ console.info('---- doink 32 ------')
803
+ r = await setWalker(schema, {
804
+ $id: 'bl120',
805
+ $alias: 'bla',
806
+ })
807
+
808
+ console.dir(r.errors)
809
+ console.dir(
810
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
811
+ { depth: 10 }
812
+ )
813
+
814
+ console.info('---- doink 33 ------')
815
+ r = await setWalker(schema, {
816
+ $id: 'bl120',
817
+ $alias: ['bla'],
818
+ })
819
+
820
+ console.dir(r.errors)
821
+ console.dir(
822
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
823
+ { depth: 10 }
824
+ )
825
+
826
+ console.info('---- doink 34 ------')
827
+ r = await setWalker(schema, {
828
+ $id: 'bl120',
829
+ enum: 'tony',
830
+ })
831
+
832
+ console.dir(r.errors)
833
+ console.dir(
834
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
835
+ { depth: 10 }
836
+ )
837
+
838
+ console.info('---- doink 35 ------')
839
+ r = await setWalker(schema, {
840
+ $id: 'bl120',
841
+ integer: NaN,
842
+ })
843
+
844
+ console.dir(r.errors)
708
845
  console.dir(
709
846
  r.collected.map((v) => ({ path: v.path, value: v.value })),
710
847
  { depth: 10 }