@based/schema 1.1.6 → 1.1.8

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/reference.ts CHANGED
@@ -1,159 +1,218 @@
1
- // import test from 'ava'
2
- // import { BasedSchema, setWalker2 } from '../src/index'
3
- // import { resultCollect, errorCollect } from './utils'
4
-
5
- // const schema: BasedSchema = {
6
- // types: {
7
- // bla: {
8
- // prefix: 'bl',
9
- // fields: {
10
- // ref: {
11
- // type: 'reference',
12
- // allowedTypes: ['bl', 'bl1'],
13
- // },
14
- // ref2: {
15
- // type: 'references',
16
- // allowedTypes: ['bl'],
17
- // },
18
- // arr: {
19
- // type: 'array',
20
- // title: '',
21
- // },
22
- // },
23
- // },
24
- // },
25
- // $defs: {},
26
- // languages: ['en'],
27
- // root: {
28
- // fields: {},
29
- // },
30
- // prefixToTypeMapping: {
31
- // bl: 'bla',
32
- // },
33
- // }
34
-
35
- // test('reference', async (t) => {
36
- // const e1 = await setWalker2(schema, {
37
- // $id: 'bl1',
38
- // ref: ['1', '2'],
39
- // })
40
- // const e2 = await setWalker2(schema, {
41
- // $id: 'bl1',
42
- // ref: 1,
43
- // })
44
-
45
- // const res = await setWalker2(schema, {
46
- // $id: 'bl1',
47
- // ref: 'asdasdasdasdasd',
48
- // })
49
-
50
- // t.assert(errorCollect([e1, e2]).length > 0)
51
- // t.deepEqual(resultCollect([res]), [
52
- // { path: ['ref'], value: 'asdasdasdasdasd' },
53
- // ])
54
- // })
55
-
56
- // test('multiple references', async (t) => {
57
- // // const e = await (
58
- // // setWalker2(
59
- // // schema,
60
- // // {
61
- // // $id: 'bl1',
62
- // // ref2: 0.5,
63
- // // },
64
- // //
65
- // // )
66
- // // )
67
- // // const e = await (
68
- // // setWalker2(
69
- // // schema,
70
- // // {
71
- // // $id: 'bl1',
72
- // // ref2: 1,
73
- // // },
74
- // //
75
- // // )
76
- // // )
77
- // // these should throw, array of refs doesnt
78
- // //??? todo?
79
- // const e = await setWalker2(schema, {
80
- // $id: 'bl1',
81
- // ref2: [1, 1, 1, 1, 1, 1, 1],
82
- // })
83
- // const res1 = await setWalker2(schema, {
84
- // $id: 'bl1',
85
- // ref2: ['1', '2', '3'],
86
- // })
87
-
88
- // t.assert(errorCollect([e]).length === 1)
89
- // t.deepEqual(resultCollect([res1]), [
90
- // { path: ['ref2'], value: { $value: ['1', '2', '3'] } },
91
- // ])
92
- // })
93
-
94
- // test('value of references', async (t) => {
95
- // const e = await setWalker2(schema, {
96
- // $id: 'bl1',
97
- // ref: { $value: ['1', '2'] },
98
- // })
99
- // const e1 = await setWalker2(schema, {
100
- // $id: 'bl1',
101
- // ref: { $value: 1 },
102
- // })
103
-
104
- // const res1 = await setWalker2(schema, {
105
- // $id: 'bl1',
106
- // ref: { $value: 'asdasdasdasdasd' },
107
- // })
108
-
109
- // //error here?
110
- // t.assert(errorCollect([e, e1]).length === 2)
111
- // t.deepEqual(resultCollect([res1]), [
112
- // { path: ['ref'], value: { $value: 'asdasdasdasdasd' } },
113
- // ])
114
- // })
115
-
116
- // test('default of references', async (t) => {
117
- // const e2 = await setWalker2(schema, {
118
- // $id: 'bl1',
119
- // ref: { $default: ['1', '2'] },
120
- // })
121
- // const e1 = await setWalker2(schema, {
122
- // $id: 'bl1',
123
- // ref: { $default: 1 },
124
- // })
125
-
126
- // // await setWalker2(
127
- // // schema,
128
- // // {
129
- // // $id: 'bl1',
130
- // // ref: { $default: 'asdasdasdasdasd' },
131
- // // },
132
- // //
133
- // // )
134
- // // console.log('----:XX', resultCollect)
135
- // // //error here?
136
- // // t.deepEqual(resultCollect, [
137
- // // { path: ['ref'], value: { $default: 'asdasdasdasdasd' } },
138
- // // ])
139
- // t.assert(errorCollect([e2, e1]).length === 2)
140
- // })
141
-
142
- // test.only('allowedTypes', async (t) => {
143
- // const e1 = await setWalker2(schema, {
144
- // $id: 'bl1',
145
- // ref: ['1', '2'],
146
- // })
147
- // const e2 = await setWalker2(schema, {
148
- // $id: 'bs1',
149
- // ref: 1,
150
- // })
151
- // const res1 = await setWalker2(schema, {
152
- // $id: 'bl1',
153
- // ref: 'blastuff',
154
- // })
155
-
156
- // t.assert(errorCollect([e2, e1]).length === 2)
157
- // t.deepEqual(resultCollect([res1]), [{ path: ['ref'], value: 'bl1stuff' }])
158
- // // is this wrong or am i wrong
159
- // })
1
+ import test from 'ava'
2
+ import { BasedSchema, setWalker } from '../src/index'
3
+ import { resultCollect } from './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
+ ref: {
18
+ type: 'reference',
19
+ },
20
+ children: {
21
+ type: 'references',
22
+ },
23
+ referencesToThings: {
24
+ type: 'references',
25
+ allowedTypes: ['thing'],
26
+ },
27
+ referenceToThing: {
28
+ type: 'reference',
29
+ allowedTypes: ['thing'],
30
+ },
31
+ },
32
+ },
33
+ },
34
+ $defs: {},
35
+ languages: ['en', 'de', 'nl', 'ro', 'za', 'ae'],
36
+ root: {
37
+ fields: {},
38
+ },
39
+ prefixToTypeMapping: {
40
+ bl: 'bla',
41
+ ti: 'thing',
42
+ },
43
+ }
44
+
45
+ let r
46
+
47
+ test('simple error', async (t) => {
48
+ r = await setWalker(schema, {
49
+ $id: 'bl120',
50
+ referenceToThing: 'sdfefewfewfewewffwe',
51
+ })
52
+
53
+ t.true(r.errors.length === 1)
54
+ })
55
+
56
+ test('simple case ', async (t) => {
57
+ r = await setWalker(schema, {
58
+ $id: 'bl120',
59
+ referenceToThing: 'tibla',
60
+ })
61
+
62
+ t.deepEqual(resultCollect(r), [
63
+ { path: ['referenceToThing'], value: 'tibla' },
64
+ ])
65
+ })
66
+
67
+ test('reference to wrong type', async (t) => {
68
+ r = await setWalker(schema, {
69
+ $id: 'bl120',
70
+ referenceToThing: 'blbla',
71
+ })
72
+ t.true(r.errors.length === 1)
73
+ })
74
+
75
+ test('references with wrongly formatted ids and incorrect types ', async (t) => {
76
+ r = await setWalker(schema, {
77
+ $id: 'bl120',
78
+ referencesToThings: ['blbla', 'ti123', 'ewiohfdoweihfw'],
79
+ })
80
+
81
+ t.true(r.errors.length === 2)
82
+ })
83
+
84
+ test('references to empty array (clear)', async (t) => {
85
+ r = await setWalker(schema, {
86
+ $id: 'bl120',
87
+ referencesToThings: [],
88
+ })
89
+ t.deepEqual(resultCollect(r), [
90
+ { path: ['referencesToThings'], value: { $value: [] } },
91
+ ])
92
+ t.is(r.errors.length, 0)
93
+ })
94
+
95
+ test('$remove references', async (t) => {
96
+ r = await setWalker(schema, {
97
+ $id: 'bl120',
98
+ referencesToThings: { $remove: ['ti123'] },
99
+ })
100
+
101
+ t.deepEqual(resultCollect(r), [
102
+ { path: ['referencesToThings'], value: { $remove: ['ti123'] } },
103
+ ])
104
+ })
105
+
106
+ test('$add 0 2 wrong', async (t) => {
107
+ r = await setWalker(schema, {
108
+ $id: 'bl120',
109
+ referencesToThings: { $add: ['blbla', 'ti123', 'ewiohfdoweihfw'] },
110
+ })
111
+
112
+ t.true(r.errors.length === 2)
113
+ })
114
+
115
+ test('$add correct ', async (t) => {
116
+ r = await setWalker(schema, {
117
+ $id: 'bl120',
118
+ referencesToThings: { $add: 'ti123' },
119
+ })
120
+
121
+ t.deepEqual(resultCollect(r), [
122
+ { path: ['referencesToThings'], value: { $add: ['ti123'] } },
123
+ ])
124
+ })
125
+
126
+ test('$remove $value not allowed', async (t) => {
127
+ r = await setWalker(schema, {
128
+ $id: 'bl120',
129
+ referencesToThings: { $remove: { $value: 'ti123' } },
130
+ })
131
+
132
+ t.true(r.errors.length > 0)
133
+ })
134
+
135
+ test('reference to an object', async (t) => {
136
+ r = await setWalker(
137
+ schema,
138
+ {
139
+ $id: 'bl120',
140
+ referenceToThing: {
141
+ type: 'thing',
142
+ priority: 9000,
143
+ },
144
+ },
145
+ async (args) => {
146
+ if (args.value.type === 'thing') {
147
+ return 'tilil'
148
+ } else {
149
+ return 'bl1221'
150
+ }
151
+ }
152
+ )
153
+
154
+ t.is(r.errors.length, 0)
155
+
156
+ t.deepEqual(resultCollect(r), [
157
+ { path: ['referenceToThing'], value: 'tilil' },
158
+ ])
159
+
160
+ t.true(true)
161
+ })
162
+
163
+ test('nested references + $add (deep)', async (t) => {
164
+ r = await setWalker(
165
+ schema,
166
+ {
167
+ $id: 'bl1221',
168
+ children: {
169
+ $add: [
170
+ {
171
+ type: 'bla',
172
+ },
173
+ ],
174
+ },
175
+ },
176
+ async (args) => {
177
+ if (args.value.type === 'thing') {
178
+ return 'tilil'
179
+ } else {
180
+ return 'bl1221'
181
+ }
182
+ }
183
+ )
184
+ t.is(r.errors.length, 0)
185
+ t.deepEqual(resultCollect(r), [
186
+ { path: ['children'], value: { $add: ['bl1221'] } },
187
+ ])
188
+ })
189
+
190
+ test('nested ref + references', async (t) => {
191
+ r = await setWalker(
192
+ schema,
193
+ {
194
+ $id: 'bl1221',
195
+ ref: {
196
+ $id: 'bl1221',
197
+ },
198
+ children: [
199
+ {
200
+ $id: 'bl1221',
201
+ },
202
+ ],
203
+ },
204
+ async (args) => {
205
+ if (args.value.type === 'thing') {
206
+ return 'tilil'
207
+ } else {
208
+ return 'bl1221'
209
+ }
210
+ }
211
+ )
212
+ t.is(r.errors.length, 0)
213
+
214
+ t.deepEqual(resultCollect(r), [
215
+ { path: ['ref'], value: 'bl1221' },
216
+ { path: ['children'], value: { $value: ['bl1221'] } },
217
+ ])
218
+ })