@based/schema 1.1.5 → 1.1.7
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/dist/set/fields/index.js +3 -0
- package/dist/set/fields/index.js.map +1 -1
- package/dist/set/fields/references.js +1 -1
- package/dist/set/fields/references.js.map +1 -1
- package/dist/set/fields/string.js +6 -2
- package/dist/set/fields/string.js.map +1 -1
- package/dist/set/index.js +5 -1
- package/dist/set/index.js.map +1 -1
- package/dist/walker/parse.js +15 -22
- package/dist/walker/parse.js.map +1 -1
- package/package.json +1 -1
- package/src/set/fields/index.ts +3 -0
- package/src/set/fields/references.ts +1 -1
- package/src/set/fields/string.ts +5 -2
- package/src/set/index.ts +5 -2
- package/src/walker/parse.ts +19 -25
- package/test/array.ts +369 -0
- package/test/number.ts +60 -0
- package/test/reference.ts +218 -159
- package/test/rest.ts +143 -113
- package/test/set.ts +80 -106
- package/test/text.ts +39 -12
- package/test/walker.ts +47 -1
package/test/reference.ts
CHANGED
|
@@ -1,159 +1,218 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
+
})
|