@based/schema 0.0.4 → 0.0.6
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/parse.d.ts +2 -0
- package/dist/parse.js +9 -0
- package/dist/parse.js.map +1 -0
- package/dist/set/collections.d.ts +5 -0
- package/dist/set/collections.js +139 -0
- package/dist/set/collections.js.map +1 -0
- package/dist/set/error.d.ts +10 -0
- package/dist/set/error.js +19 -0
- package/dist/set/error.js.map +1 -0
- package/dist/set/index.js +8 -14
- package/dist/set/index.js.map +1 -1
- package/dist/set/number.d.ts +4 -0
- package/dist/set/number.js +57 -0
- package/dist/set/number.js.map +1 -0
- package/dist/set/parsers.d.ts +2 -5
- package/dist/set/parsers.js +35 -308
- package/dist/set/parsers.js.map +1 -1
- package/dist/set/references.d.ts +3 -0
- package/dist/set/references.js +81 -0
- package/dist/set/references.js.map +1 -0
- package/dist/set/rest.d.ts +5 -0
- package/dist/set/rest.js +53 -0
- package/dist/set/rest.js.map +1 -0
- package/dist/set/string.d.ts +3 -0
- package/dist/set/string.js +136 -0
- package/dist/set/string.js.map +1 -0
- package/dist/set/types.d.ts +5 -0
- package/dist/{deepPartial.js → set/types.js} +1 -1
- package/dist/set/types.js.map +1 -0
- package/dist/types.d.ts +36 -17
- package/package.json +3 -1
- package/src/set/collections.ts +246 -0
- package/src/set/error.ts +17 -0
- package/src/set/index.ts +8 -18
- package/src/set/number.ts +78 -0
- package/src/set/parsers.ts +17 -478
- package/src/set/references.ts +107 -0
- package/src/set/rest.ts +76 -0
- package/src/set/string.ts +159 -0
- package/src/set/types.ts +19 -0
- package/src/types.ts +105 -20
- package/test/setWalker.ts +16 -7
- package/dist/deepPartial.d.ts +0 -0
- package/dist/deepPartial.js.map +0 -1
- package/dist/set/enum.d.ts +0 -2
- package/dist/set/enum.js +0 -15
- package/dist/set/enum.js.map +0 -1
- package/dist/set/fieldValidator.d.ts +0 -6
- package/dist/set/fieldValidator.js +0 -144
- package/dist/set/fieldValidator.js.map +0 -1
- package/dist/set/handleError.d.ts +0 -1
- package/dist/set/handleError.js +0 -9
- package/dist/set/handleError.js.map +0 -1
- package/dist/setWalker.d.ts +0 -11
- package/dist/setWalker.js +0 -189
- package/dist/setWalker.js.map +0 -1
- package/dist/transformers.d.ts +0 -3
- package/dist/transformers.js +0 -18
- package/dist/transformers.js.map +0 -1
- package/dist/typeWalker.d.ts +0 -3
- package/dist/typeWalker.js +0 -18
- package/dist/typeWalker.js.map +0 -1
- package/dist/validate.d.ts +0 -4
- package/dist/validate.js +0 -34
- package/dist/validate.js.map +0 -1
- package/dist/validateFields.d.ts +0 -4
- package/dist/validateFields.js +0 -34
- package/dist/validateFields.js.map +0 -1
- package/dist/validateSchema copy.d.ts +0 -4
- package/dist/validateSchema copy.js +0 -34
- package/dist/validateSchema copy.js.map +0 -1
- package/src/set/handleError.ts +0 -15
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { error, ParseError } from './error'
|
|
3
|
+
|
|
4
|
+
export const timestamp: Parser<'timestamp'> = async (
|
|
5
|
+
path,
|
|
6
|
+
value,
|
|
7
|
+
fieldSchema,
|
|
8
|
+
typeSchema,
|
|
9
|
+
target,
|
|
10
|
+
handlers
|
|
11
|
+
) => {
|
|
12
|
+
if (typeof value === 'string') {
|
|
13
|
+
if (value === 'now') {
|
|
14
|
+
value = Date.now()
|
|
15
|
+
} else {
|
|
16
|
+
const d = new Date(value)
|
|
17
|
+
value = d.valueOf()
|
|
18
|
+
if (isNaN(value)) {
|
|
19
|
+
throw error(path, ParseError.incorrectFormat)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// // smaller then / larger then steps
|
|
24
|
+
// if (typeof value !== 'number') {
|
|
25
|
+
// throw createError(path, target.type, 'timestamp', value)
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
// if (fieldSchema.maximum) {
|
|
29
|
+
// if (fieldSchema.exclusiveMaximum && value > value) {
|
|
30
|
+
// throw createError(path, target.type, 'timestamp', value)
|
|
31
|
+
// } else if (value >= value) {
|
|
32
|
+
// throw createError(path, target.type, 'timestamp', value)
|
|
33
|
+
// }
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
// if (fieldSchema.minimum) {
|
|
37
|
+
// if (fieldSchema.exclusiveMinimum && value < value) {
|
|
38
|
+
// throw createError(path, target.type, 'timestamp', value)
|
|
39
|
+
// } else if (value <= value) {
|
|
40
|
+
// throw createError(path, target.type, 'timestamp', value)
|
|
41
|
+
// }
|
|
42
|
+
// }
|
|
43
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const number: Parser<'number'> = async (
|
|
47
|
+
path,
|
|
48
|
+
value,
|
|
49
|
+
fieldSchema,
|
|
50
|
+
typeSchema,
|
|
51
|
+
target,
|
|
52
|
+
handlers
|
|
53
|
+
) => {
|
|
54
|
+
// value .default
|
|
55
|
+
// $increment / $decrement
|
|
56
|
+
|
|
57
|
+
if (typeof value !== 'number') {
|
|
58
|
+
error(path, ParseError.incorrectFormat)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const integer: Parser<'integer'> = async (
|
|
65
|
+
path,
|
|
66
|
+
value,
|
|
67
|
+
fieldSchema,
|
|
68
|
+
typeSchema,
|
|
69
|
+
target,
|
|
70
|
+
handlers
|
|
71
|
+
) => {
|
|
72
|
+
// value .default
|
|
73
|
+
// $increment / $decrement
|
|
74
|
+
if (typeof value !== 'number' || value - Math.floor(value) !== 0) {
|
|
75
|
+
error(path, ParseError.incorrectFormat)
|
|
76
|
+
}
|
|
77
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
78
|
+
}
|
package/src/set/parsers.ts
CHANGED
|
@@ -1,481 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
) => Promise<void>
|
|
19
|
-
|
|
20
|
-
const reference: Parser = async (
|
|
21
|
-
path,
|
|
22
|
-
value,
|
|
23
|
-
fieldSchema,
|
|
24
|
-
typeSchema,
|
|
25
|
-
target,
|
|
26
|
-
handlers
|
|
27
|
-
) => {
|
|
28
|
-
// $no root
|
|
29
|
-
|
|
30
|
-
// prob pass these as options
|
|
31
|
-
// value .default
|
|
32
|
-
// $value
|
|
33
|
-
|
|
34
|
-
if (typeof value !== 'string') {
|
|
35
|
-
throw createError(path, target.type, 'reference', value)
|
|
36
|
-
}
|
|
37
|
-
if ('allowedTypes' in fieldSchema) {
|
|
38
|
-
const prefix = value.slice(0, 2)
|
|
39
|
-
const targetType = target.schema.prefixToTypeMapping[prefix]
|
|
40
|
-
if (!targetType) {
|
|
41
|
-
throw createError(
|
|
42
|
-
path,
|
|
43
|
-
target.type,
|
|
44
|
-
'reference',
|
|
45
|
-
value,
|
|
46
|
-
'',
|
|
47
|
-
`${prefix} does not exist in database`
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
|
-
let typeMatches = false
|
|
51
|
-
for (const t of fieldSchema.allowedTypes) {
|
|
52
|
-
if (typeof t === 'string') {
|
|
53
|
-
if (t === targetType) {
|
|
54
|
-
typeMatches = true
|
|
55
|
-
break
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
if (t.type && t.type === targetType) {
|
|
59
|
-
typeMatches = true
|
|
60
|
-
if (t.$filter) {
|
|
61
|
-
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
62
|
-
throw createError(
|
|
63
|
-
path,
|
|
64
|
-
target.type,
|
|
65
|
-
'reference',
|
|
66
|
-
value,
|
|
67
|
-
'',
|
|
68
|
-
`${targetType} does not match allowedReferences`
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
} else if (!t.type && t.$filter) {
|
|
73
|
-
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
74
|
-
throw createError(
|
|
75
|
-
path,
|
|
76
|
-
target.type,
|
|
77
|
-
'reference',
|
|
78
|
-
value,
|
|
79
|
-
'',
|
|
80
|
-
`${targetType} does not match allowedReferences`
|
|
81
|
-
)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (typeMatches === false) {
|
|
87
|
-
throw createError(
|
|
88
|
-
path,
|
|
89
|
-
target.type,
|
|
90
|
-
'reference',
|
|
91
|
-
value,
|
|
92
|
-
'',
|
|
93
|
-
`${targetType} does not match allowedReferences`
|
|
94
|
-
)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const parsers: {
|
|
101
|
-
[key: string]: Parser
|
|
102
|
-
} = {
|
|
103
|
-
enum: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
104
|
-
// value .default
|
|
105
|
-
|
|
106
|
-
// @ts-ignore
|
|
107
|
-
const enumValues = fieldSchema.enum
|
|
108
|
-
for (let i = 0; i < enumValues.length; i++) {
|
|
109
|
-
if (deepEqual(enumValues[i], value)) {
|
|
110
|
-
handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
throw createError(path, target.type, 'enum', value)
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
array: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
118
|
-
// value .default
|
|
119
|
-
|
|
120
|
-
// TODO: ADD ARRAY OPERATORS
|
|
121
|
-
const isArray = Array.isArray(value)
|
|
122
|
-
|
|
123
|
-
if (typeof value === 'object' && !isArray) {
|
|
124
|
-
const q: Promise<void>[] = []
|
|
125
|
-
|
|
126
|
-
if (value.$insert) {
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (value.$remove) {
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (value.$push) {
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (value.$assign) {
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// value.$value :/
|
|
139
|
-
// fix
|
|
140
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
141
|
-
|
|
142
|
-
return
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!isArray) {
|
|
146
|
-
throw createError(path, target.type, 'array', value)
|
|
147
|
-
}
|
|
148
|
-
const q: Promise<void>[] = []
|
|
149
|
-
for (let i = 0; i < value.length; i++) {
|
|
150
|
-
q.push(
|
|
151
|
-
fieldWalker(
|
|
152
|
-
[...path, i],
|
|
153
|
-
value[i],
|
|
154
|
-
// @ts-ignore
|
|
155
|
-
fieldSchema.values,
|
|
156
|
-
typeSchema,
|
|
157
|
-
target,
|
|
158
|
-
handlers
|
|
159
|
-
)
|
|
160
|
-
)
|
|
161
|
-
}
|
|
162
|
-
await Promise.all(q)
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
object: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
166
|
-
// value .default
|
|
167
|
-
|
|
168
|
-
if (typeof value !== 'object') {
|
|
169
|
-
throw createError(path, target.type, 'object', value)
|
|
170
|
-
}
|
|
171
|
-
const isArray = Array.isArray(value)
|
|
172
|
-
if (isArray) {
|
|
173
|
-
throw createError(path, target.type, 'object', value)
|
|
174
|
-
}
|
|
175
|
-
const q: Promise<void>[] = []
|
|
176
|
-
for (const key in value) {
|
|
177
|
-
// @ts-ignore
|
|
178
|
-
const propDef = fieldSchema.properties[key]
|
|
179
|
-
if (!propDef) {
|
|
180
|
-
throw createError(
|
|
181
|
-
[...path, key],
|
|
182
|
-
target.type,
|
|
183
|
-
'object',
|
|
184
|
-
value[key],
|
|
185
|
-
key
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
q.push(
|
|
189
|
-
fieldWalker(
|
|
190
|
-
[...path, key],
|
|
191
|
-
value[key],
|
|
192
|
-
propDef,
|
|
193
|
-
typeSchema,
|
|
194
|
-
target,
|
|
195
|
-
handlers
|
|
196
|
-
)
|
|
197
|
-
)
|
|
198
|
-
}
|
|
199
|
-
await Promise.all(q)
|
|
200
|
-
},
|
|
201
|
-
|
|
202
|
-
set: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
203
|
-
// value .default
|
|
204
|
-
|
|
205
|
-
const q: Promise<void>[] = []
|
|
206
|
-
// @ts-ignore
|
|
207
|
-
const fieldDef = fieldSchema.items
|
|
208
|
-
|
|
209
|
-
if (Array.isArray(value)) {
|
|
210
|
-
const handlerNest = {
|
|
211
|
-
...handlers,
|
|
212
|
-
collect: ({ path, value }) => {
|
|
213
|
-
parsedArray.push(value)
|
|
214
|
-
},
|
|
215
|
-
}
|
|
216
|
-
const parsedArray = []
|
|
217
|
-
for (let i = 0; i < value.length; i++) {
|
|
218
|
-
q.push(
|
|
219
|
-
fieldWalker(
|
|
220
|
-
[...path, i],
|
|
221
|
-
value[i],
|
|
222
|
-
fieldDef,
|
|
223
|
-
typeSchema,
|
|
224
|
-
target,
|
|
225
|
-
handlerNest
|
|
226
|
-
)
|
|
227
|
-
)
|
|
228
|
-
}
|
|
229
|
-
await Promise.all(q)
|
|
230
|
-
handlers.collect({
|
|
231
|
-
path,
|
|
232
|
-
value: { $value: parsedArray },
|
|
233
|
-
typeSchema,
|
|
234
|
-
fieldSchema,
|
|
235
|
-
target,
|
|
236
|
-
})
|
|
237
|
-
} else {
|
|
238
|
-
const handlerNest = {
|
|
239
|
-
...handlers,
|
|
240
|
-
collect: ({ path, value }) => {},
|
|
241
|
-
}
|
|
242
|
-
if (value.$add) {
|
|
243
|
-
for (let i = 0; i < value.$add.length; i++) {
|
|
244
|
-
q.push(
|
|
245
|
-
fieldWalker(
|
|
246
|
-
[...path, '$add', i],
|
|
247
|
-
value.$add[i],
|
|
248
|
-
fieldDef,
|
|
249
|
-
typeSchema,
|
|
250
|
-
target,
|
|
251
|
-
handlerNest
|
|
252
|
-
)
|
|
253
|
-
)
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
if (value.$delete) {
|
|
257
|
-
for (let i = 0; i < value.$add.length; i++) {
|
|
258
|
-
q.push(
|
|
259
|
-
fieldWalker(
|
|
260
|
-
[...path, '$delete', i],
|
|
261
|
-
value.$delete[i],
|
|
262
|
-
fieldDef,
|
|
263
|
-
typeSchema,
|
|
264
|
-
target,
|
|
265
|
-
handlerNest
|
|
266
|
-
)
|
|
267
|
-
)
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
await Promise.all(q)
|
|
271
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
272
|
-
}
|
|
273
|
-
},
|
|
274
|
-
|
|
275
|
-
json: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
276
|
-
// value .default
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
const parsedValue = JSON.stringify(value)
|
|
280
|
-
handlers.collect({
|
|
281
|
-
path,
|
|
282
|
-
value: parsedValue,
|
|
283
|
-
typeSchema,
|
|
284
|
-
fieldSchema,
|
|
285
|
-
target,
|
|
286
|
-
})
|
|
287
|
-
} catch (err) {
|
|
288
|
-
throw createError(path, target.type, 'json', value)
|
|
289
|
-
}
|
|
290
|
-
},
|
|
291
|
-
|
|
292
|
-
timestamp: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
293
|
-
if (typeof value === 'string') {
|
|
294
|
-
if (value === 'now') {
|
|
295
|
-
value = Date.now()
|
|
296
|
-
} else {
|
|
297
|
-
const d = new Date(value)
|
|
298
|
-
value = d.valueOf()
|
|
299
|
-
if (isNaN(value)) {
|
|
300
|
-
throw createError(path, target.type, 'timestamp', value)
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// smaller then / larger then steps
|
|
306
|
-
|
|
307
|
-
if (typeof value !== 'number') {
|
|
308
|
-
throw createError(path, target.type, 'timestamp', value)
|
|
309
|
-
}
|
|
310
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
311
|
-
},
|
|
312
|
-
|
|
313
|
-
boolean: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
314
|
-
// value .default
|
|
315
|
-
// $increment / $decrement
|
|
316
|
-
|
|
317
|
-
if (typeof value !== 'boolean') {
|
|
318
|
-
throw createError(path, target.type, 'boolean', value)
|
|
319
|
-
}
|
|
320
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
321
|
-
},
|
|
322
|
-
|
|
323
|
-
number: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
324
|
-
// value .default
|
|
325
|
-
// $increment / $decrement
|
|
326
|
-
|
|
327
|
-
if (typeof value !== 'number') {
|
|
328
|
-
throw createError(path, target.type, 'number', value)
|
|
329
|
-
}
|
|
330
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
331
|
-
},
|
|
332
|
-
|
|
333
|
-
integer: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
334
|
-
// value .default
|
|
335
|
-
// $increment / $decrement
|
|
336
|
-
|
|
337
|
-
// smaller then / larger then steps
|
|
338
|
-
|
|
339
|
-
if (typeof value !== 'number' || value - Math.floor(value) !== 0) {
|
|
340
|
-
throw createError(path, target.type, 'integer', value)
|
|
341
|
-
}
|
|
342
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
343
|
-
},
|
|
344
|
-
|
|
345
|
-
string: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
346
|
-
// default
|
|
347
|
-
|
|
348
|
-
if (typeof value !== 'string') {
|
|
349
|
-
throw createError(path, target.type, 'string', value)
|
|
350
|
-
}
|
|
351
|
-
// @ts-ignore
|
|
352
|
-
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
353
|
-
throw createError(path, target.type, 'string', value)
|
|
354
|
-
}
|
|
355
|
-
// @ts-ignore
|
|
356
|
-
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
357
|
-
throw createError(path, target.type, 'string', value)
|
|
358
|
-
}
|
|
359
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
360
|
-
},
|
|
361
|
-
|
|
362
|
-
text: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
363
|
-
// default
|
|
364
|
-
|
|
365
|
-
const valueType = typeof value
|
|
366
|
-
if (target.$language && valueType === 'string') {
|
|
367
|
-
// @ts-ignore
|
|
368
|
-
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
369
|
-
throw createError(path, target.type, 'text', value)
|
|
370
|
-
}
|
|
371
|
-
// @ts-ignore
|
|
372
|
-
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
373
|
-
throw createError(path, target.type, 'text', value)
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
handlers.collect({
|
|
377
|
-
path,
|
|
378
|
-
value: { [target.$language]: value },
|
|
379
|
-
typeSchema,
|
|
380
|
-
fieldSchema,
|
|
381
|
-
target,
|
|
382
|
-
})
|
|
383
|
-
return
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
if (valueType !== 'object') {
|
|
387
|
-
throw createError(path, target.type, 'text', value)
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
for (const key in value) {
|
|
391
|
-
// @ts-ignore
|
|
392
|
-
if (fieldSchema.minLength && value[key].length < fieldSchema.minLength) {
|
|
393
|
-
throw createError([...path, key], target.type, 'text', value)
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// @ts-ignore
|
|
397
|
-
if (fieldSchema.maxLength && value[key].length > fieldSchema.maxLength) {
|
|
398
|
-
throw createError([...path, key], target.type, 'text', value)
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
402
|
-
handlers.collect({
|
|
403
|
-
path: [...path, key],
|
|
404
|
-
value: null,
|
|
405
|
-
typeSchema,
|
|
406
|
-
fieldSchema,
|
|
407
|
-
target,
|
|
408
|
-
})
|
|
409
|
-
continue
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (typeof value[key] !== 'string') {
|
|
413
|
-
throw createError([...path, key], target.type, 'text', value)
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
handlers.collect({
|
|
417
|
-
path: [...path, key],
|
|
418
|
-
value: value[key],
|
|
419
|
-
typeSchema,
|
|
420
|
-
fieldSchema,
|
|
421
|
-
target,
|
|
422
|
-
})
|
|
423
|
-
}
|
|
424
|
-
},
|
|
425
|
-
|
|
426
|
-
reference,
|
|
427
|
-
|
|
428
|
-
references: async (
|
|
429
|
-
path,
|
|
430
|
-
value,
|
|
431
|
-
fieldSchema,
|
|
432
|
-
typeSchema,
|
|
433
|
-
target,
|
|
434
|
-
handlers
|
|
435
|
-
) => {
|
|
436
|
-
// default
|
|
437
|
-
// $no root
|
|
438
|
-
|
|
439
|
-
if (Array.isArray(value)) {
|
|
440
|
-
const handler = {
|
|
441
|
-
...handlers,
|
|
442
|
-
collect: () => {},
|
|
443
|
-
}
|
|
444
|
-
await Promise.all(
|
|
445
|
-
value.map((v, i) => {
|
|
446
|
-
return reference(
|
|
447
|
-
[...path, i],
|
|
448
|
-
v,
|
|
449
|
-
fieldSchema,
|
|
450
|
-
typeSchema,
|
|
451
|
-
target,
|
|
452
|
-
handler
|
|
453
|
-
)
|
|
454
|
-
})
|
|
455
|
-
)
|
|
456
|
-
} else if (typeof value === 'object') {
|
|
457
|
-
if (value.$add) {
|
|
458
|
-
const handler = {
|
|
459
|
-
...handlers,
|
|
460
|
-
collect: () => {},
|
|
461
|
-
}
|
|
462
|
-
await Promise.all(
|
|
463
|
-
value.$add.map((v, i) => {
|
|
464
|
-
return reference(
|
|
465
|
-
[...path, '$add', i],
|
|
466
|
-
v,
|
|
467
|
-
fieldSchema,
|
|
468
|
-
typeSchema,
|
|
469
|
-
target,
|
|
470
|
-
handler
|
|
471
|
-
)
|
|
472
|
-
})
|
|
473
|
-
)
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
478
|
-
},
|
|
1
|
+
import { Parsers } from './types'
|
|
2
|
+
import * as references from './references'
|
|
3
|
+
import * as collections from './collections'
|
|
4
|
+
import * as number from './number'
|
|
5
|
+
import * as string from './string'
|
|
6
|
+
|
|
7
|
+
import { enumParser, boolean, cardinality, json } from './rest'
|
|
8
|
+
|
|
9
|
+
const parsers: Parsers = {
|
|
10
|
+
...string,
|
|
11
|
+
...references,
|
|
12
|
+
...collections,
|
|
13
|
+
...number,
|
|
14
|
+
enum: enumParser,
|
|
15
|
+
boolean,
|
|
16
|
+
cardinality,
|
|
17
|
+
json,
|
|
479
18
|
}
|
|
480
19
|
|
|
481
20
|
export default parsers
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { error, ParseError } from './error'
|
|
3
|
+
|
|
4
|
+
export const reference: Parser<'reference'> = async (
|
|
5
|
+
path,
|
|
6
|
+
value,
|
|
7
|
+
fieldSchema,
|
|
8
|
+
typeSchema,
|
|
9
|
+
target,
|
|
10
|
+
handlers
|
|
11
|
+
) => {
|
|
12
|
+
// $no root
|
|
13
|
+
// prob pass these as options
|
|
14
|
+
// value .default
|
|
15
|
+
// $value
|
|
16
|
+
|
|
17
|
+
if (typeof value !== 'string') {
|
|
18
|
+
error(path, ParseError.incorrectFormat)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ('allowedTypes' in fieldSchema) {
|
|
22
|
+
const prefix = value.slice(0, 2)
|
|
23
|
+
const targetType = target.schema.prefixToTypeMapping[prefix]
|
|
24
|
+
if (!targetType) {
|
|
25
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
26
|
+
}
|
|
27
|
+
let typeMatches = false
|
|
28
|
+
for (const t of fieldSchema.allowedTypes) {
|
|
29
|
+
if (typeof t === 'string') {
|
|
30
|
+
if (t === targetType) {
|
|
31
|
+
typeMatches = true
|
|
32
|
+
break
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (t.type && t.type === targetType) {
|
|
36
|
+
typeMatches = true
|
|
37
|
+
if (t.$filter) {
|
|
38
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
39
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else if (!t.type && t.$filter) {
|
|
43
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
44
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (typeMatches === false) {
|
|
50
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const references: Parser<'references'> = async (
|
|
57
|
+
path,
|
|
58
|
+
value,
|
|
59
|
+
fieldSchema,
|
|
60
|
+
typeSchema,
|
|
61
|
+
target,
|
|
62
|
+
handlers
|
|
63
|
+
) => {
|
|
64
|
+
// default
|
|
65
|
+
// $no root
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
const handler = {
|
|
68
|
+
...handlers,
|
|
69
|
+
collect: () => {},
|
|
70
|
+
}
|
|
71
|
+
await Promise.all(
|
|
72
|
+
value.map((v, i) => {
|
|
73
|
+
return reference(
|
|
74
|
+
[...path, i],
|
|
75
|
+
v,
|
|
76
|
+
// not nice slow
|
|
77
|
+
{ ...fieldSchema, type: 'reference' },
|
|
78
|
+
typeSchema,
|
|
79
|
+
target,
|
|
80
|
+
handler
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
)
|
|
84
|
+
value = { $value: value }
|
|
85
|
+
} else if (typeof value === 'object') {
|
|
86
|
+
if (value.$add) {
|
|
87
|
+
const handler = {
|
|
88
|
+
...handlers,
|
|
89
|
+
collect: () => {},
|
|
90
|
+
}
|
|
91
|
+
await Promise.all(
|
|
92
|
+
value.$add.map((v, i) => {
|
|
93
|
+
return reference(
|
|
94
|
+
[...path, '$add', i],
|
|
95
|
+
v,
|
|
96
|
+
// not nice slow
|
|
97
|
+
{ ...fieldSchema, type: 'reference' },
|
|
98
|
+
typeSchema,
|
|
99
|
+
target,
|
|
100
|
+
handler
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
107
|
+
}
|