@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/rest.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import test from 'ava'
2
- import { BasedSchema, setWalker, BasedSetOptionalHandlers } from '../src/index'
2
+ import { BasedSchema, setWalker } from '../src/index'
3
+ import { errorCollect, resultCollect } from './utils'
3
4
 
4
5
  const schema: BasedSchema = {
5
6
  types: {
@@ -31,57 +32,24 @@ const schema: BasedSchema = {
31
32
  },
32
33
  }
33
34
 
34
- const createHandlers = (): {
35
- results: { path: (number | string)[]; value: any }[]
36
- handlers: BasedSetOptionalHandlers
37
- } => {
38
- const results: { path: (number | string)[]; value: any }[] = []
39
- const handlers = {
40
- collect: ({ path, value, typeSchema, fieldSchema, target }) => {
41
- results.push({ path, value })
42
- },
43
- checkRequiredFields: async (paths) => {
44
- return true
45
- },
46
- referenceFilterCondition: async (id, filter) => {
47
- return true
48
- },
49
- }
50
- return { results, handlers }
51
- }
52
-
53
35
  test('arrayNum', async (t) => {
54
- const { handlers, results } = createHandlers()
55
- await t.throwsAsync(
56
- setWalker(
57
- schema,
58
- {
59
- $id: 'bl1',
60
- arrNum: ['1', '2'],
61
- },
62
- handlers
63
- )
64
- )
65
- await t.throwsAsync(
66
- setWalker(
67
- schema,
68
- {
69
- $id: 'bla',
70
- ref: 1,
71
- },
72
- handlers
73
- )
74
- )
36
+ const err = await setWalker(schema, {
37
+ $id: 'bl1',
38
+ arrNum: ['1', '2'],
39
+ })
40
+ const err1 = await setWalker(schema, {
41
+ $id: 'bla',
42
+ ref: 1,
43
+ })
75
44
 
76
- await setWalker(
77
- schema,
78
- {
79
- $id: 'bla',
80
- arrNum: [1, 2],
81
- },
82
- handlers
83
- )
84
- t.deepEqual(results, [
45
+ const res = await setWalker(schema, {
46
+ $id: 'bla',
47
+ arrNum: [1, 2],
48
+ })
49
+
50
+ t.true(errorCollect(err, err1).length > 0)
51
+
52
+ t.deepEqual(resultCollect(res), [
85
53
  { path: ['arrNum'], value: { $delete: true } },
86
54
  { path: ['arrNum', 0], value: 1 },
87
55
  { path: ['arrNum', 1], value: 2 },
@@ -89,98 +57,67 @@ test('arrayNum', async (t) => {
89
57
  })
90
58
 
91
59
  test.only('value arr', async (t) => {
92
- const { handlers, results } = createHandlers()
93
- await t.throwsAsync(
94
- setWalker(
95
- schema,
96
- {
97
- $id: 'bl1',
98
- arrNum: { $value: ['1', '2'] },
99
- },
100
- handlers
101
- )
102
- )
103
- await t.throwsAsync(
104
- setWalker(
105
- schema,
106
- {
107
- $id: 'bla',
108
- ref: { $value: 1 },
109
- },
110
- handlers
111
- )
112
- )
60
+ const err = await setWalker(schema, {
61
+ $id: 'bl1',
62
+ arrNum: { $value: ['1', '2'] },
63
+ })
64
+ const err1 = await setWalker(schema, {
65
+ $id: 'bla',
66
+ ref: { $value: 1 },
67
+ })
113
68
 
114
- await setWalker(
115
- schema,
116
- {
117
- $id: 'bla',
118
- arrNum: [{ $value: 1 }, { $value: 2 }],
119
- },
120
- handlers
121
- )
122
- await setWalker(
123
- schema,
124
- {
125
- $id: 'bla',
126
- arrNum: { $value: [1, 2] },
127
- },
128
- handlers
129
- )
130
- console.log(results)
131
- t.deepEqual(results, [
69
+ const res = await setWalker(schema, {
70
+ $id: 'bla',
71
+ arrNum: [{ $value: 1 }, { $value: 2 }],
72
+ })
73
+ const res1 = await setWalker(schema, {
74
+ $id: 'bla',
75
+ arrNum: { $value: [1, 2] },
76
+ })
77
+
78
+ t.true(errorCollect(err, err1).length > 0)
79
+ console.log(resultCollect(res, res1))
80
+
81
+ t.deepEqual(resultCollect(res, res1), [
132
82
  { path: ['arrNum'], value: { $delete: true } },
133
- { path: ['arrNum', 0], value: { $value: 1 } }, // just 1
134
- { path: ['arrNum', 1], value: { $value: 2 } }, // just 2
83
+ { path: ['arrNum', 0], value: 1 },
84
+ { path: ['arrNum', 1], value: 2 },
135
85
  { path: ['arrNum'], value: { $delete: true } },
136
86
  { path: ['arrNum', 0], value: 1 },
137
87
  { path: ['arrNum', 1], value: 2 },
88
+ { path: ['arrNum'], value: { $delete: true } },
138
89
  ])
90
+
91
+ // { path: ['arrNum'], value: { $delete: true } },
92
+ // { path: ['arrNum', 0], value: { $value: 1 } }, // just 1
93
+ // { path: ['arrNum', 1], value: { $value: 2 } }, // just 2
94
+ // { path: ['arrNum'], value: { $delete: true } },
95
+ // { path: ['arrNum', 0], value: 1 },
96
+ // { path: ['arrNum', 1], value: 2 },
139
97
  })
140
98
 
141
99
  test('default arr', async (t) => {
142
- const { handlers, results } = createHandlers()
100
+ const err = await setWalker(schema, {
101
+ $id: 'bl1',
102
+ arrNum: ['1', '2'],
103
+ })
104
+ const err1 = await setWalker(schema, {
105
+ $id: 'bla',
106
+ ref: 1,
107
+ })
143
108
 
144
- await t.throwsAsync(
145
- setWalker(
146
- schema,
147
- {
148
- $id: 'bl1',
149
- arrNum: ['1', '2'],
150
- },
151
- handlers
152
- )
153
- )
154
- await t.throwsAsync(
155
- setWalker(
156
- schema,
157
- {
158
- $id: 'bla',
159
- ref: 1,
160
- },
161
- handlers
162
- )
163
- )
109
+ const res = await setWalker(schema, {
110
+ $id: 'bla',
111
+ arrNum: [{ $default: 1 }, { $default: 2 }],
112
+ })
113
+ const res1 = await setWalker(schema, {
114
+ $id: 'bla',
115
+ arrNum: { $default: [1, 2] },
116
+ })
164
117
 
165
- await setWalker(
166
- schema,
167
- {
168
- $id: 'bla',
169
- arrNum: [{ $default: 1 }, { $default: 2 }],
170
- },
171
- handlers
172
- )
173
- await setWalker(
174
- schema,
175
- {
176
- $id: 'bla',
177
- arrNum: { $default: [1, 2] },
178
- },
179
- handlers
180
- )
181
- console.log(results)
118
+ t.true(errorCollect(err, err1).length > 0)
182
119
  //is this correct? is it supposed to be different
183
- t.deepEqual(results, [
120
+ t.deepEqual(resultCollect(res, res1), [
184
121
  { path: ['arrNum'], value: { $delete: true } },
185
122
  { path: ['arrNum', 0], value: { $default: 1 } },
186
123
  { path: ['arrNum', 1], value: { $default: 2 } },
@@ -189,25 +126,25 @@ test('default arr', async (t) => {
189
126
  ])
190
127
  })
191
128
  // test('arrStr', async (t) => {
192
- // const { handlers, results } = createHandlers()
193
- // await t.throwsAsync(
129
+ //
130
+ // const err = await(
194
131
  // setWalker(
195
132
  // schema,
196
133
  // {
197
134
  // $id: 'bl1',
198
135
  // arrStr: [1, '2'],
199
136
  // },
200
- // handlers
137
+ //
201
138
  // )
202
139
  // )
203
- // await t.throwsAsync(
140
+ // const err = await(
204
141
  // setWalker(
205
142
  // schema,
206
143
  // {
207
144
  // $id: 'bla',
208
145
  // ref: 1,
209
146
  // },
210
- // handlers
147
+ //
211
148
  // )
212
149
  // )
213
150
 
@@ -217,7 +154,7 @@ test('default arr', async (t) => {
217
154
  // $id: 'bla',
218
155
  // arrStr: [1, 2],
219
156
  // },
220
- // handlers
157
+ //
221
158
  // )
222
159
  // t.deepEqual(results, [
223
160
  // { path: ['arrStr'], value: { $delete: true } },
package/test/set.ts ADDED
@@ -0,0 +1,116 @@
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
+ // something: { type: 'string', format: 'strongPassword' },
11
+ // },
12
+ // },
13
+ // bla: {
14
+ // prefix: 'bl',
15
+ // fields: {
16
+ // referencesToThings: {
17
+ // type: 'references',
18
+ // allowedTypes: ['thing'],
19
+ // },
20
+ // referenceToThing: {
21
+ // type: 'reference',
22
+ // allowedTypes: ['thing'],
23
+ // },
24
+ // exclusiveminmax: {
25
+ // type: 'number',
26
+ // minimum: 3,
27
+ // exclusiveMinimum: true,
28
+ // maximum: 6,
29
+ // exclusiveMaximum: true,
30
+ // },
31
+ // text: {
32
+ // type: 'text',
33
+ // pattern: '[^xz]{1,10}',
34
+ // },
35
+ // timestamp: {
36
+ // type: 'timestamp',
37
+ // },
38
+ // setOfNumbers: {
39
+ // type: 'set',
40
+ // items: {
41
+ // type: 'number',
42
+ // },
43
+ // },
44
+ // x: {
45
+ // type: 'object',
46
+ // properties: {
47
+ // flap: {
48
+ // type: 'boolean',
49
+ // },
50
+ // },
51
+ // },
52
+ // bla: {
53
+ // type: 'set',
54
+ // items: { type: 'string', minLength: 3, maxLength: 6 },
55
+ // },
56
+ // },
57
+ // },
58
+ // },
59
+ // $defs: {},
60
+ // languages: ['en', 'de', 'nl', 'ro', 'za', 'ae'],
61
+ // root: {
62
+ // fields: {},
63
+ // },
64
+ // prefixToTypeMapping: {
65
+ // bl: 'bla',
66
+ // ti: 'thing',
67
+ // },
68
+ // }
69
+
70
+ // console.info('---- doink 15 ------')
71
+ // r = await setWalker(schema, {
72
+ // $id: 'bl120',
73
+ // setOfNumbers: [1, 2, 3, 4, 5],
74
+ // })
75
+
76
+ // console.log(r.errors)
77
+ // console.dir(
78
+ // r.collected.map((v) => ({ path: v.path, value: v.value })),
79
+ // { depth: 10 }
80
+ // )
81
+
82
+ // console.info('---- doink 16 ------')
83
+ // r = await setWalker(schema, {
84
+ // $id: 'bl120',
85
+ // setOfNumbers: { $add: 20 },
86
+ // })
87
+
88
+ // console.log(r.errors)
89
+ // console.dir(
90
+ // r.collected.map((v) => ({ path: v.path, value: v.value })),
91
+ // { depth: 10 }
92
+ // )
93
+
94
+ // console.info('---- doink 17 ------')
95
+ // r = await setWalker(schema, {
96
+ // $id: 'bl120',
97
+ // setOfNumbers: { $add: [1, 2, 3, 4, 5, 6] },
98
+ // })
99
+
100
+ // console.log(r.errors)
101
+ // console.dir(
102
+ // r.collected.map((v) => ({ path: v.path, value: v.value })),
103
+ // { depth: 10 }
104
+ // )
105
+
106
+ // console.info('---- doink 18 ------')
107
+ // r = await setWalker(schema, {
108
+ // $id: 'bl120',
109
+ // setOfNumbers: { $remove: [1, 2, 3, 4, 5, 6] },
110
+ // })
111
+
112
+ // console.log(r.errors)
113
+ // console.dir(
114
+ // r.collected.map((v) => ({ path: v.path, value: v.value })),
115
+ // { depth: 10 }
116
+ // )
package/test/string.ts CHANGED
@@ -1,139 +1,121 @@
1
- // import test from 'ava'
2
- // import { BasedSchema, setWalker2 } from '../src/index'
3
-
4
- // const schema: BasedSchema = {
5
- // types: {
6
- // bla: {
7
- // prefix: 'bl',
8
- // fields: {
9
- // name: {
10
- // minLength: 3,
11
- // maxLength: 6,
12
- // type: 'string',
13
- // },
14
- // phonkName: {
15
- // type: 'string',
16
- // pattern: '\\${1,4}',
17
- // },
18
- // bla: {
19
- // type: 'set',
20
- // items: { type: 'string', minLength: 3, maxLength: 6 },
21
- // },
22
- // },
23
- // },
24
- // },
25
- // $defs: {},
26
- // languages: ['en'],
27
- // root: {
28
- // fields: {},
29
- // },
30
- // prefixToTypeMapping: {
31
- // bl: 'bla',
32
- // },
33
- // }
34
-
35
- // test('string max length', async (t) => {
36
- // const err1 = await setWalker2(schema, {
37
- // $id: 'bl1',
38
- // name: 'ax',
39
- // })
40
-
41
- // const err2 = await setWalker2(schema, {
42
- // $id: 'bl1',
43
- // name: 'axaxaxax',
44
- // })
45
- // const res1 = await setWalker2(schema, {
46
- // $id: 'bl1',
47
- // name: 'xaxx',
48
- // })
49
-
50
- // console.log(err1.errors)
51
- // console.log(err2.errors)
52
- // const err = [err2.errors]
53
- // const res = [res1]
54
-
55
- // // t.assert(err[0].length === 2)
56
- // t.deepEqual(res[0].target.collected, [{ path: ['name'], value: 'xaxx' }])
57
- // })
58
-
59
- // //set parser
60
- // test('set with strings', async (t) => {
61
- // const err1 = setWalker2(schema, {
62
- // $id: 'bl1',
63
- // bla: ['ax', 'axa', 'axxxxa'],
64
- // })
65
-
66
- // const res1 = await setWalker2(schema, {
67
- // $id: 'bl1',
68
- // bla: ['axx', 'axxxx', 'blaaa'],
69
- // })
70
-
71
- // const err = [(await err1).errors]
72
- // const res = [await res1]
73
-
74
- // // t.assert(err[0].length === 1)
75
- // t.deepEqual(res[0].target.collected, [
76
- // { path: ['bla'], value: { $value: ['axx', 'axxxx', 'blaaa'] } },
77
- // ])
78
- // })
79
-
80
- // // this one causes weird array lenght issue in string max length test
81
- // test('string pattern', async (t) => {
82
- // const err1 = await setWalker2(schema, {
83
- // $id: 'bl1',
84
- // phonkName: 'blabla',
85
- // })
86
-
87
- // const res1 = await setWalker2(schema, {
88
- // $id: 'bl1',
89
- // phonkName: 'bla$',
90
- // })
91
-
92
- // const err = [err1.errors]
93
-
94
- // const res = [res1]
95
- // // t.assert(err[0].length === 1)
96
- // t.deepEqual(res[0].target.collected, [{ path: ['phonkName'], value: 'bla$' }])
97
- // })
98
-
99
- // test('setting $default', async (t) => {
100
- // const err = await setWalker2(schema, {
101
- // $id: 'bl1',
102
- // phonkName: { $default: 'blabla' },
103
- // })
104
- // t.assert(err.errors.length > 1)
105
-
106
- // const res1 = await setWalker2(schema, {
107
- // $id: 'bl1',
108
- // phonkName: { $default: 'bla$' },
109
- // })
110
-
111
- // const res = [res1]
112
- // console.log(res) // is ok
113
- // console.log(res1) // isnt ok???
114
-
115
- // t.deepEqual(res[0].target.collected, [
116
- // { path: ['phonkName'], value: { $default: 'bla$' } },
117
- // ])
118
- // })
119
-
120
- // test('setting $value', async (t) => {
121
- // const err = await setWalker2(schema, {
122
- // $id: 'bl1',
123
- // phonkName: { $value: 'blabla' },
124
- // })
125
- // t.assert(err.errors.length > 1)
126
-
127
- // const res1 = await setWalker2(schema, {
128
- // $id: 'bl1',
129
- // phonkName: { $value: 'bla$' },
130
- // })
131
-
132
- // const res = [res1]
133
- // console.log(res) // is ok
134
- // console.log(res1) // isnt ok???
135
-
136
- // t.deepEqual(res[0].target.collected, [
137
- // { path: ['phonkName'], value: { $value: 'bla$' } },
138
- // ])
139
- // })
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
+ name: {
11
+ minLength: 3,
12
+ maxLength: 6,
13
+ type: 'string',
14
+ },
15
+ phonkName: {
16
+ type: 'string',
17
+ pattern: '\\${1,4}',
18
+ },
19
+ bla: {
20
+ type: 'set',
21
+ items: { type: 'string', minLength: 3, maxLength: 6 },
22
+ },
23
+ },
24
+ },
25
+ },
26
+ $defs: {},
27
+ languages: ['en'],
28
+ root: {
29
+ fields: {},
30
+ },
31
+ prefixToTypeMapping: {
32
+ bl: 'bla',
33
+ },
34
+ }
35
+
36
+ test('string max length', async (t) => {
37
+ const err1 = await setWalker(schema, {
38
+ $id: 'bl1',
39
+ name: 'ax',
40
+ })
41
+
42
+ const err2 = await setWalker(schema, {
43
+ $id: 'bl1',
44
+ name: 'axaxaxax',
45
+ })
46
+ const res1 = await setWalker(schema, {
47
+ $id: 'bl1',
48
+ name: 'xaxx',
49
+ })
50
+
51
+ t.assert(errorCollect(err1, err2).length > 0)
52
+ t.deepEqual(resultCollect(res1), [{ path: ['name'], value: 'xaxx' }])
53
+ })
54
+
55
+ //set parser
56
+ test('set with strings', async (t) => {
57
+ const err1 = await setWalker(schema, {
58
+ $id: 'bl1',
59
+ bla: ['ax', 'axa', 'axxxxa'],
60
+ })
61
+
62
+ const res1 = await setWalker(schema, {
63
+ $id: 'bl1',
64
+ bla: ['axx', 'axxxx', 'blaaa'],
65
+ })
66
+
67
+ t.assert(errorCollect(err1).length > 0)
68
+ t.deepEqual(resultCollect(res1), [
69
+ { path: ['bla'], value: { $value: ['axx', 'axxxx', 'blaaa'] } },
70
+ ])
71
+ })
72
+
73
+ // this one causes weird array lenght issue in string max length test
74
+ test('string pattern', async (t) => {
75
+ const err1 = await setWalker(schema, {
76
+ $id: 'bl1',
77
+ phonkName: 'blabla',
78
+ })
79
+
80
+ const res1 = await setWalker(schema, {
81
+ $id: 'bl1',
82
+ phonkName: 'bla$',
83
+ })
84
+
85
+ t.assert(errorCollect(err1).length > 0)
86
+ t.deepEqual(resultCollect(res1), [{ path: ['phonkName'], value: 'bla$' }])
87
+ })
88
+
89
+ test('setting $default', async (t) => {
90
+ const err = await setWalker(schema, {
91
+ $id: 'bl1',
92
+ phonkName: { $default: 'blabla' },
93
+ })
94
+
95
+ t.assert(err.errors.length > 0)
96
+
97
+ const res1 = await setWalker(schema, {
98
+ $id: 'bl1',
99
+ phonkName: { $default: 'bla$' },
100
+ })
101
+
102
+ t.deepEqual(resultCollect(res1), [
103
+ { path: ['phonkName'], value: { $default: 'bla$' } },
104
+ ])
105
+ })
106
+
107
+ test('setting $value', async (t) => {
108
+ const err = await setWalker(schema, {
109
+ $id: 'bl1',
110
+ phonkName: { $value: 'blabla' },
111
+ })
112
+
113
+ t.assert(err.errors.length > 1)
114
+
115
+ const res1 = await setWalker(schema, {
116
+ $id: 'bl1',
117
+ phonkName: { $value: 'bla$' },
118
+ })
119
+
120
+ t.deepEqual(resultCollect(res1), [{ path: ['phonkName'], value: 'bla$' }])
121
+ })