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