@based/schema 1.0.7 → 1.0.9

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
@@ -86,26 +86,29 @@ const schema: BasedSchema = {
86
86
  }
87
87
 
88
88
  test('walker', async (t) => {
89
- const x = await walk(
89
+ const results: any[] = []
90
+
91
+ const setObj = {
92
+ x: {
93
+ y: {
94
+ a: 10,
95
+ bla: [1, 2, 3, 4, 5],
96
+ },
97
+ c: 40,
98
+ },
99
+ }
100
+
101
+ await walk<{ lullz: true }>(
90
102
  schema,
91
103
  {
92
- init: async (args) => {
93
- return { ...args, target: { lullz: true } }
104
+ init: async () => {
105
+ return { target: { lullz: true } }
94
106
  },
95
107
  parsers: {
96
- keys: {
97
- // $list: async (args) => {
98
- // return {
99
- // ...args,
100
- // value: { flapdrol: true },
101
- // }
102
- // },
103
- },
104
- fields: {
105
- // string: () => {}
106
- },
108
+ keys: {},
109
+ fields: {},
107
110
  any: async (args) => {
108
- args.collect(args)
111
+ args.collect()
109
112
  return args
110
113
  },
111
114
  },
@@ -113,47 +116,92 @@ test('walker', async (t) => {
113
116
  return args.path.join('.')
114
117
  },
115
118
  backtrack: (args, fromBt, collected) => {
116
- console.log(
117
- ' \n-----------BACK TRACK GOOD GO',
118
- '\n',
119
- 'path:',
120
- args.path.join('.'),
121
- '\n',
122
- 'backtracked:',
123
- JSON.stringify(fromBt),
124
- '\n',
125
- 'collected:',
126
- collected,
127
- '--------------------'
128
- )
119
+ results.push({ path: args.path, bt: [...fromBt] })
129
120
  return fromBt.length ? fromBt : collected
130
121
  },
131
122
  },
123
+ setObj
124
+ )
125
+
126
+ t.deepEqual(results, [
127
+ { path: ['x', 'y', 'bla'], bt: [] },
132
128
  {
133
- gurk: [1, 2, 3, 4],
134
- x: {
135
- y: {
136
- z: {
137
- a: 10,
138
- b: 20,
139
- gur: {
140
- x: true,
141
- y: true,
142
- $list: {
143
- $sort: true,
144
- },
145
- },
146
- c: 40,
147
- $list: true,
148
- },
129
+ path: ['x', 'y'],
130
+ bt: [['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']],
131
+ },
132
+ {
133
+ path: ['x'],
134
+ bt: [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
135
+ },
136
+ {
137
+ path: [],
138
+ bt: [
139
+ [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
140
+ ],
141
+ },
142
+ ])
143
+
144
+ console.log('--------------------------------------')
145
+ const results2: any[] = []
146
+
147
+ await walk<{ lullz: true }>(
148
+ schema,
149
+ {
150
+ init: async () => {
151
+ return { target: { lullz: true } }
152
+ },
153
+ parsers: {
154
+ keys: {},
155
+ fields: {},
156
+ any: async (args) => {
157
+ args.collect()
158
+ return { target: { lullz: true } }
149
159
  },
150
160
  },
151
- }
161
+ collect: (args) => {
162
+ return args.path.join('.')
163
+ },
164
+ backtrack: (args, fromBt, collected) => {
165
+ results2.push({ path: args.path, bt: [...fromBt] })
166
+ return fromBt.length ? fromBt : collected
167
+ },
168
+ },
169
+ setObj
152
170
  )
153
171
 
154
- console.info('------------')
172
+ t.deepEqual(results, results2)
155
173
 
156
- t.true(true)
174
+ console.log('--------------------------------------')
175
+ const results3: any[] = []
176
+
177
+ let cnt = 0
178
+ await walk<{ lullz: true }>(
179
+ schema,
180
+ {
181
+ init: async () => {
182
+ return { target: { lullz: true } }
183
+ },
184
+ parsers: {
185
+ keys: {},
186
+ fields: {},
187
+ any: async (args) => {
188
+ cnt++
189
+ args.collect()
190
+ return cnt % 2 ? args : { target: { lullz: true } }
191
+ },
192
+ },
193
+ collect: (args) => {
194
+ return args.path.join('.')
195
+ },
196
+ backtrack: (args, fromBt, collected) => {
197
+ results3.push({ path: args.path, bt: [...fromBt] })
198
+ return fromBt.length ? fromBt : collected
199
+ },
200
+ },
201
+ setObj
202
+ )
203
+
204
+ t.deepEqual(results, results3)
157
205
  })
158
206
 
159
207
  test('set walker', async (t) => {
@@ -259,7 +307,7 @@ test('set walker', async (t) => {
259
307
  t.true(true)
260
308
  })
261
309
 
262
- test('perf setWalker', async (t) => {
310
+ test.serial('perf setWalker', async (t) => {
263
311
  let d = Date.now()
264
312
  let collected = 0
265
313
  let errs = 0
@@ -272,10 +320,12 @@ test('perf setWalker', async (t) => {
272
320
  errs += x.errors.length
273
321
  collected += x.collected.length
274
322
  }
323
+ d = Date.now() - d
324
+ console.info('setting 200k', d, 'ms')
275
325
  t.true(d < 1e3)
276
326
  })
277
327
 
278
- test.only('string', async (t) => {
328
+ test('string', async (t) => {
279
329
  // for (let i = 0; i < 10; i++) {
280
330
  // console.log(
281
331
  // (await setWalker(schema, { $id: 'bl120', name: 'blax' })).target
@@ -672,8 +722,32 @@ test.only('string', async (t) => {
672
722
  },
673
723
  })
674
724
 
675
- r.collected.forEach((v) => {
676
- console.info(v.root.typeSchema)
725
+ console.log(r.errors)
726
+ console.dir(
727
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
728
+ { depth: 10 }
729
+ )
730
+
731
+ console.info('---- doink 27 ------')
732
+ r = await setWalker(schema, {
733
+ $id: 'bl120',
734
+ intarray: {
735
+ $push: [1, 2, 3, 4, 5],
736
+ },
737
+ })
738
+
739
+ console.log(r.errors)
740
+ console.dir(
741
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
742
+ { depth: 10 }
743
+ )
744
+
745
+ console.info('---- doink 28 ------')
746
+ r = await setWalker(schema, {
747
+ $id: 'bl120',
748
+ intarray: {
749
+ $unshift: [1, 2, 3, 4, 5],
750
+ },
677
751
  })
678
752
 
679
753
  console.log(r.errors)
@@ -682,5 +756,69 @@ test.only('string', async (t) => {
682
756
  { depth: 10 }
683
757
  )
684
758
 
759
+ console.info('---- doink 29 ------')
760
+ r = await setWalker(schema, {
761
+ $id: 'bl120',
762
+ text: {
763
+ en: 'bla',
764
+ },
765
+ })
766
+
767
+ console.dir(
768
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
769
+ { depth: 10 }
770
+ )
771
+
772
+ t.true(true)
773
+
774
+ console.info('---- doink 30 ------')
775
+ r = await setWalker(schema, {
776
+ $id: 'bl120',
777
+ text: {
778
+ $delete: true,
779
+ },
780
+ })
781
+
782
+ console.dir(
783
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
784
+ { depth: 10 }
785
+ )
786
+
787
+ console.info('---- doink 31 ------')
788
+ r = await setWalker(schema, {
789
+ $id: 'bl120',
790
+ $delete: true,
791
+ })
792
+
793
+ console.dir(r.errors)
794
+ console.dir(
795
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
796
+ { depth: 10 }
797
+ )
798
+
799
+ console.info('---- doink 32 ------')
800
+ r = await setWalker(schema, {
801
+ $id: 'bl120',
802
+ $alias: 'bla',
803
+ })
804
+
805
+ console.dir(r.errors)
806
+ console.dir(
807
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
808
+ { depth: 10 }
809
+ )
810
+
811
+ console.info('---- doink 33 ------')
812
+ r = await setWalker(schema, {
813
+ $id: 'bl120',
814
+ $alias: ['bla'],
815
+ })
816
+
817
+ console.dir(r.errors)
818
+ console.dir(
819
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
820
+ { depth: 10 }
821
+ )
822
+
685
823
  t.true(true)
686
824
  })