@based/schema 1.0.8 → 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/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
+ })
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
+ })