@based/schema 0.0.9 → 0.0.11
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/set/collections.js +52 -19
- package/dist/set/collections.js.map +1 -1
- package/dist/set/error.d.ts +5 -2
- package/dist/set/error.js +10 -3
- package/dist/set/error.js.map +1 -1
- package/dist/set/index.d.ts +2 -2
- package/dist/set/index.js +67 -12
- package/dist/set/index.js.map +1 -1
- package/dist/set/number.js +37 -23
- package/dist/set/number.js.map +1 -1
- package/dist/set/parseDefaultAndValue.js +5 -3
- package/dist/set/parseDefaultAndValue.js.map +1 -1
- package/dist/set/references.js +12 -5
- package/dist/set/references.js.map +1 -1
- package/dist/set/rest.js +4 -4
- package/dist/set/rest.js.map +1 -1
- package/dist/set/string.js +48 -31
- package/dist/set/string.js.map +1 -1
- package/dist/set2/index.js +1 -0
- package/dist/set2/index.js.map +1 -0
- package/dist/types.d.ts +10 -2
- package/dist/types.js.map +1 -1
- package/dist/updateSchema.d.ts +2 -0
- package/dist/updateSchema.js +16 -0
- package/dist/updateSchema.js.map +1 -0
- package/dist/validateSchema.js +5 -1
- package/dist/validateSchema.js.map +1 -1
- package/dist/walker.d.ts +50 -0
- package/dist/walker.js +83 -0
- package/dist/walker.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/set/collections.ts +56 -21
- package/src/set/error.ts +14 -3
- package/src/set/index.ts +87 -12
- package/src/set/number.ts +39 -22
- package/src/set/parseDefaultAndValue.ts +13 -3
- package/src/set/references.ts +11 -5
- package/src/set/rest.ts +4 -4
- package/src/set/string.ts +78 -35
- package/src/set2/index.ts +0 -0
- package/src/types.ts +18 -11
- package/src/updateSchema.ts +18 -0
- package/src/validateSchema.ts +6 -1
- package/src/walker.ts +152 -0
- package/test/number.ts +646 -0
- package/test/reference.ts +207 -0
- package/test/setWalker.ts +223 -53
- package/test/string.ts +183 -0
- package/test/text.ts +171 -0
- package/test/validateSchema.ts +0 -1
- package/test/walker.ts +99 -0
- package/dist/deepPartial.js +0 -3
- package/dist/deepPartial.js.map +0 -1
- package/dist/schema.d.ts +0 -1
- package/dist/schema.js +0 -3
- package/dist/schema.js.map +0 -1
- package/dist/set/checkDefaultAndValue.d.ts +0 -3
- package/dist/set/checkDefaultAndValue.js +0 -33
- package/dist/set/checkDefaultAndValue.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/set/number copy.d.ts +0 -4
- package/dist/set/number copy.js +0 -57
- package/dist/set/number copy.js.map +0 -1
- package/dist/set/rest copy.d.ts +0 -5
- package/dist/set/rest copy.js +0 -53
- package/dist/set/rest copy.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/dist/{deepPartial.d.ts → set2/index.d.ts} +0 -0
package/src/set/collections.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Parser } from './types'
|
|
|
2
2
|
import { error, ParseError } from './error'
|
|
3
3
|
import { fieldWalker } from '.'
|
|
4
4
|
|
|
5
|
+
// what about making errors return signature
|
|
6
|
+
|
|
5
7
|
export const set: Parser<'set'> = async (
|
|
6
8
|
path,
|
|
7
9
|
value,
|
|
@@ -11,8 +13,19 @@ export const set: Parser<'set'> = async (
|
|
|
11
13
|
handlers,
|
|
12
14
|
noCollect
|
|
13
15
|
) => {
|
|
16
|
+
if (value && typeof value === 'object') {
|
|
17
|
+
if (value.$default) {
|
|
18
|
+
error(handlers, ParseError.defaultNotSupported, path)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (value.$value) {
|
|
22
|
+
value = value.$value
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
const q: Promise<void>[] = []
|
|
15
27
|
const fieldDef = fieldSchema.items
|
|
28
|
+
|
|
16
29
|
if (Array.isArray(value)) {
|
|
17
30
|
const parsedArray = []
|
|
18
31
|
for (let i = 0; i < value.length; i++) {
|
|
@@ -51,12 +64,12 @@ export const set: Parser<'set'> = async (
|
|
|
51
64
|
)
|
|
52
65
|
}
|
|
53
66
|
}
|
|
54
|
-
if (value.$
|
|
55
|
-
for (let i = 0; i < value.$
|
|
67
|
+
if (value.$remove) {
|
|
68
|
+
for (let i = 0; i < value.$remove.length; i++) {
|
|
56
69
|
q.push(
|
|
57
70
|
fieldWalker(
|
|
58
71
|
path,
|
|
59
|
-
value.$
|
|
72
|
+
value.$remove[i],
|
|
60
73
|
fieldDef,
|
|
61
74
|
typeSchema,
|
|
62
75
|
target,
|
|
@@ -83,17 +96,20 @@ export const object: Parser<'object'> = async (
|
|
|
83
96
|
noCollect
|
|
84
97
|
) => {
|
|
85
98
|
if (typeof value !== 'object') {
|
|
86
|
-
error(
|
|
99
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
100
|
+
return
|
|
87
101
|
}
|
|
88
102
|
const isArray = Array.isArray(value)
|
|
89
103
|
if (isArray) {
|
|
90
|
-
error(
|
|
104
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
105
|
+
return
|
|
91
106
|
}
|
|
92
107
|
const q: Promise<void>[] = []
|
|
93
108
|
for (const key in value) {
|
|
94
109
|
const propDef = fieldSchema.properties[key]
|
|
95
110
|
if (!propDef) {
|
|
96
|
-
error([...path, key]
|
|
111
|
+
error(handlers, ParseError.fieldDoesNotExist, [...path, key])
|
|
112
|
+
continue
|
|
97
113
|
}
|
|
98
114
|
q.push(
|
|
99
115
|
fieldWalker(
|
|
@@ -108,9 +124,16 @@ export const object: Parser<'object'> = async (
|
|
|
108
124
|
)
|
|
109
125
|
}
|
|
110
126
|
await Promise.all(q)
|
|
127
|
+
if (fieldSchema.required) {
|
|
128
|
+
for (const req of fieldSchema.required) {
|
|
129
|
+
if (!(req in value)) {
|
|
130
|
+
target.required.push([...path, req])
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
111
134
|
}
|
|
112
135
|
|
|
113
|
-
//
|
|
136
|
+
// IF REQUIRED AND PUSH OR UNSHIFT just throw here scince we dont need to parse at all...
|
|
114
137
|
export const array: Parser<'array'> = async (
|
|
115
138
|
path,
|
|
116
139
|
value,
|
|
@@ -120,13 +143,10 @@ export const array: Parser<'array'> = async (
|
|
|
120
143
|
handlers,
|
|
121
144
|
noCollect
|
|
122
145
|
) => {
|
|
123
|
-
// $value
|
|
124
|
-
|
|
125
146
|
let isArray = Array.isArray(value)
|
|
126
147
|
let parsedValue = value
|
|
127
148
|
let opCount = 0
|
|
128
149
|
let has$Value = false
|
|
129
|
-
|
|
130
150
|
if (typeof parsedValue === 'object' && !isArray) {
|
|
131
151
|
if (value.$value) {
|
|
132
152
|
opCount++
|
|
@@ -137,13 +157,15 @@ export const array: Parser<'array'> = async (
|
|
|
137
157
|
if (value.$insert) {
|
|
138
158
|
opCount++
|
|
139
159
|
if (opCount > 1) {
|
|
140
|
-
error(
|
|
160
|
+
error(handlers, ParseError.multipleOperationsNotAllowed, path)
|
|
161
|
+
return
|
|
141
162
|
}
|
|
142
163
|
if (
|
|
143
164
|
typeof value.$insert !== 'object' ||
|
|
144
165
|
value.$insert.$idx === undefined
|
|
145
166
|
) {
|
|
146
|
-
error(
|
|
167
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
168
|
+
return
|
|
147
169
|
} else {
|
|
148
170
|
const insert = Array.isArray(value.$insert.$value)
|
|
149
171
|
? value.$insert.$value
|
|
@@ -168,24 +190,29 @@ export const array: Parser<'array'> = async (
|
|
|
168
190
|
if (value.$remove) {
|
|
169
191
|
opCount++
|
|
170
192
|
if (opCount > 1) {
|
|
171
|
-
error(
|
|
193
|
+
error(handlers, ParseError.multipleOperationsNotAllowed, path)
|
|
194
|
+
return
|
|
172
195
|
}
|
|
173
196
|
if (value.$remove.$idx === undefined) {
|
|
174
|
-
error(
|
|
197
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
198
|
+
return
|
|
175
199
|
}
|
|
176
200
|
}
|
|
177
201
|
if (value.$push) {
|
|
178
202
|
opCount++
|
|
179
203
|
if (opCount > 1) {
|
|
180
|
-
error(
|
|
204
|
+
error(handlers, ParseError.multipleOperationsNotAllowed, path)
|
|
205
|
+
return
|
|
181
206
|
}
|
|
182
207
|
|
|
208
|
+
// TODO: FIX PUSH PARSING
|
|
183
209
|
const q: Promise<void>[] = []
|
|
184
210
|
const push = Array.isArray(value.$push) ? value.$push : [value.$push]
|
|
185
211
|
for (let i = 0; i < push.length; i++) {
|
|
186
212
|
q.push(
|
|
187
213
|
fieldWalker(
|
|
188
|
-
|
|
214
|
+
// exception
|
|
215
|
+
[...path, '$push', i],
|
|
189
216
|
push[i],
|
|
190
217
|
fieldSchema.values,
|
|
191
218
|
typeSchema,
|
|
@@ -201,16 +228,20 @@ export const array: Parser<'array'> = async (
|
|
|
201
228
|
if (value.$unshift) {
|
|
202
229
|
opCount++
|
|
203
230
|
if (opCount > 1) {
|
|
204
|
-
error(
|
|
231
|
+
error(handlers, ParseError.multipleOperationsNotAllowed, path)
|
|
232
|
+
return
|
|
205
233
|
}
|
|
206
234
|
const q: Promise<void>[] = []
|
|
207
235
|
const unshift = Array.isArray(value.$unshift)
|
|
208
236
|
? value.$unshift
|
|
209
237
|
: [value.$unshift]
|
|
238
|
+
|
|
239
|
+
// TODO: FIX UNSHIFT PARSING
|
|
210
240
|
for (let i = 0; i < unshift.length; i++) {
|
|
211
241
|
q.push(
|
|
212
242
|
fieldWalker(
|
|
213
|
-
|
|
243
|
+
// exception
|
|
244
|
+
[...path, '$unshift', i],
|
|
214
245
|
unshift[i],
|
|
215
246
|
fieldSchema.values,
|
|
216
247
|
typeSchema,
|
|
@@ -226,13 +257,15 @@ export const array: Parser<'array'> = async (
|
|
|
226
257
|
if (value.$assign) {
|
|
227
258
|
opCount++
|
|
228
259
|
if (opCount > 1) {
|
|
229
|
-
error(
|
|
260
|
+
error(handlers, ParseError.multipleOperationsNotAllowed, path)
|
|
261
|
+
return
|
|
230
262
|
}
|
|
231
263
|
if (
|
|
232
264
|
typeof value.$assign !== 'object' ||
|
|
233
265
|
typeof value.$assign.$idx !== 'number'
|
|
234
266
|
) {
|
|
235
|
-
error(
|
|
267
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
268
|
+
return
|
|
236
269
|
}
|
|
237
270
|
await fieldWalker(
|
|
238
271
|
[...path, value.$assign.$idx],
|
|
@@ -259,7 +292,8 @@ export const array: Parser<'array'> = async (
|
|
|
259
292
|
}
|
|
260
293
|
}
|
|
261
294
|
if (!isArray) {
|
|
262
|
-
error(
|
|
295
|
+
error(handlers, ParseError.incorrectFieldType, path)
|
|
296
|
+
return
|
|
263
297
|
}
|
|
264
298
|
const q: Promise<void>[] = []
|
|
265
299
|
const collector: any[] = []
|
|
@@ -284,6 +318,7 @@ export const array: Parser<'array'> = async (
|
|
|
284
318
|
)
|
|
285
319
|
)
|
|
286
320
|
}
|
|
321
|
+
|
|
287
322
|
await Promise.all(q)
|
|
288
323
|
|
|
289
324
|
if (!noCollect) {
|
package/src/set/error.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BasedSetHandlers } from '../types'
|
|
2
|
+
|
|
1
3
|
export enum ParseError {
|
|
2
4
|
'incorrectFieldType',
|
|
3
5
|
'incorrectNodeType',
|
|
@@ -9,12 +11,21 @@ export enum ParseError {
|
|
|
9
11
|
'valueAndDefault',
|
|
10
12
|
'defaultNotSupported',
|
|
11
13
|
'multipleOperationsNotAllowed',
|
|
14
|
+
'requiredFieldNotDefined',
|
|
15
|
+
'languageNotSupported',
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
export const error = (
|
|
15
|
-
|
|
19
|
+
handlers: BasedSetHandlers,
|
|
16
20
|
error: ParseError,
|
|
17
|
-
|
|
21
|
+
path?: (number | string)[]
|
|
18
22
|
) => {
|
|
19
|
-
|
|
23
|
+
const message = path
|
|
24
|
+
? `${ParseError[error]} ${path.join('.')}`
|
|
25
|
+
: `${ParseError[error]}`
|
|
26
|
+
|
|
27
|
+
handlers.collectErrors({
|
|
28
|
+
message,
|
|
29
|
+
code: error,
|
|
30
|
+
})
|
|
20
31
|
}
|
package/src/set/index.ts
CHANGED
|
@@ -4,9 +4,12 @@ import {
|
|
|
4
4
|
BasedSetHandlers,
|
|
5
5
|
BasedSchema,
|
|
6
6
|
BasedSetTarget,
|
|
7
|
+
BasedSchemaCollectProps,
|
|
8
|
+
BasedSetOptionalHandlers,
|
|
7
9
|
} from '../types'
|
|
8
10
|
import { error, ParseError } from './error'
|
|
9
11
|
import parsers from './parsers'
|
|
12
|
+
import { SetOptional } from 'type-fest'
|
|
10
13
|
|
|
11
14
|
export const fieldWalker = async (
|
|
12
15
|
path: (string | number)[],
|
|
@@ -34,13 +37,13 @@ export const fieldWalker = async (
|
|
|
34
37
|
const typeDef = fieldSchema.type ?? ('enum' in fieldSchema ? 'enum' : '')
|
|
35
38
|
|
|
36
39
|
if (!typeDef) {
|
|
37
|
-
error(
|
|
40
|
+
error(handlers, ParseError.fieldDoesNotExist, path)
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
if ('customValidator' in fieldSchema) {
|
|
41
44
|
const customValidator = fieldSchema.customValidator
|
|
42
45
|
if (!(await customValidator(value, path, target))) {
|
|
43
|
-
error(
|
|
46
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
44
47
|
}
|
|
45
48
|
}
|
|
46
49
|
|
|
@@ -54,20 +57,51 @@ export const fieldWalker = async (
|
|
|
54
57
|
export const setWalker = async (
|
|
55
58
|
schema: BasedSchema,
|
|
56
59
|
value: { [key: string]: any },
|
|
57
|
-
|
|
60
|
+
inHandlers: BasedSetOptionalHandlers
|
|
58
61
|
): Promise<BasedSetTarget> => {
|
|
62
|
+
let errors: {
|
|
63
|
+
message: string
|
|
64
|
+
code: ParseError
|
|
65
|
+
}[]
|
|
66
|
+
|
|
67
|
+
const collect: BasedSchemaCollectProps[] = []
|
|
68
|
+
|
|
69
|
+
const x = { ...inHandlers }
|
|
70
|
+
|
|
71
|
+
if (!('collectErrors' in x)) {
|
|
72
|
+
errors = []
|
|
73
|
+
x.collectErrors = (info) => {
|
|
74
|
+
errors.push(info)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let prevCollect: any
|
|
79
|
+
|
|
80
|
+
if (!('collect' in x)) {
|
|
81
|
+
x.collect = (info) => {
|
|
82
|
+
collect.push(info)
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
prevCollect = x.collect
|
|
86
|
+
x.collect = (info) => {
|
|
87
|
+
collect.push(info)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const handlers: BasedSetHandlers = <BasedSetHandlers>x
|
|
92
|
+
|
|
59
93
|
let type: string
|
|
60
94
|
|
|
61
95
|
if (value.$id) {
|
|
62
96
|
type = schema.prefixToTypeMapping[value.$id.slice(0, 2)]
|
|
63
97
|
if (!type) {
|
|
64
|
-
error([value.$id]
|
|
98
|
+
error(handlers, ParseError.incorrectNodeType, [value.$id])
|
|
65
99
|
}
|
|
66
100
|
}
|
|
67
101
|
|
|
68
102
|
if (value.type) {
|
|
69
103
|
if (type && value.type !== type) {
|
|
70
|
-
error([value.$id, value.type]
|
|
104
|
+
error(handlers, ParseError.incorrectNodeType, [value.$id, value.type])
|
|
71
105
|
}
|
|
72
106
|
type = value.type
|
|
73
107
|
}
|
|
@@ -75,12 +109,20 @@ export const setWalker = async (
|
|
|
75
109
|
const schemaType = schema.types[type]
|
|
76
110
|
|
|
77
111
|
if (!schemaType) {
|
|
78
|
-
error(
|
|
112
|
+
error(handlers, ParseError.incorrectNodeType, [type])
|
|
79
113
|
}
|
|
80
114
|
|
|
81
115
|
const target: BasedSetTarget = {
|
|
82
116
|
type,
|
|
83
117
|
schema,
|
|
118
|
+
required: [],
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (value.$language) {
|
|
122
|
+
if (!schema.languages.includes(value.$language)) {
|
|
123
|
+
error(handlers, ParseError.languageNotSupported, ['$language'])
|
|
124
|
+
}
|
|
125
|
+
target.$language = value.$language
|
|
84
126
|
}
|
|
85
127
|
|
|
86
128
|
if (value.$id) {
|
|
@@ -92,10 +134,10 @@ export const setWalker = async (
|
|
|
92
134
|
const q: Promise<void>[] = []
|
|
93
135
|
|
|
94
136
|
for (const key in value) {
|
|
95
|
-
if (key[0] !== '$') {
|
|
137
|
+
if (key[0] !== '$' && key !== 'type') {
|
|
96
138
|
const fieldSchema = schemaType.fields[key]
|
|
97
139
|
if (!fieldSchema) {
|
|
98
|
-
error(
|
|
140
|
+
error(handlers, ParseError.fieldDoesNotExist, [key])
|
|
99
141
|
} else {
|
|
100
142
|
q.push(
|
|
101
143
|
fieldWalker(
|
|
@@ -113,10 +155,43 @@ export const setWalker = async (
|
|
|
113
155
|
|
|
114
156
|
await Promise.all(q)
|
|
115
157
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
158
|
+
if (schemaType.required) {
|
|
159
|
+
for (const req of schemaType.required) {
|
|
160
|
+
if (!(req in value)) {
|
|
161
|
+
target.required.push([req])
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (target.required?.length) {
|
|
167
|
+
const requireDefined = await Promise.all(
|
|
168
|
+
target.required.map(async (req) => {
|
|
169
|
+
return handlers.checkRequiredFields(req)
|
|
170
|
+
})
|
|
171
|
+
)
|
|
172
|
+
for (let i = 0; i < requireDefined.length; i++) {
|
|
173
|
+
if (!requireDefined[i]) {
|
|
174
|
+
const r = target.required[i]
|
|
175
|
+
error(handlers, ParseError.requiredFieldNotDefined, r)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (errors?.length) {
|
|
181
|
+
const err = new Error(
|
|
182
|
+
'Errors in in set' +
|
|
183
|
+
errors.reduce((str, info) => {
|
|
184
|
+
return str + `\n - ${info.message}`
|
|
185
|
+
}, '')
|
|
186
|
+
)
|
|
187
|
+
throw err
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (prevCollect) {
|
|
191
|
+
for (const x of collect) {
|
|
192
|
+
prevCollect(x)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
120
195
|
|
|
121
196
|
return target
|
|
122
197
|
}
|
package/src/set/number.ts
CHANGED
|
@@ -4,21 +4,25 @@ import {
|
|
|
4
4
|
BasedSchemaFieldInteger,
|
|
5
5
|
BasedSchemaFieldNumber,
|
|
6
6
|
BasedSchemaFieldTimeStamp,
|
|
7
|
+
BasedSetHandlers,
|
|
7
8
|
} from '../types'
|
|
8
9
|
|
|
9
10
|
const validate = (
|
|
11
|
+
handlers: BasedSetHandlers,
|
|
10
12
|
path: (number | string)[],
|
|
11
13
|
value: any,
|
|
12
14
|
fieldSchema:
|
|
13
15
|
| BasedSchemaFieldInteger
|
|
14
16
|
| BasedSchemaFieldNumber
|
|
15
17
|
| BasedSchemaFieldTimeStamp
|
|
16
|
-
): number => {
|
|
18
|
+
): number | false => {
|
|
17
19
|
if (typeof value !== 'number') {
|
|
18
|
-
error(
|
|
20
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
21
|
+
return false
|
|
19
22
|
}
|
|
20
23
|
if (fieldSchema.type === 'integer' && value - Math.floor(value) !== 0) {
|
|
21
|
-
error(
|
|
24
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
25
|
+
return false
|
|
22
26
|
}
|
|
23
27
|
if (
|
|
24
28
|
fieldSchema.multipleOf &&
|
|
@@ -26,26 +30,36 @@ const validate = (
|
|
|
26
30
|
Math.floor(value / fieldSchema.multipleOf) !==
|
|
27
31
|
0
|
|
28
32
|
) {
|
|
29
|
-
error(
|
|
33
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
34
|
+
return false
|
|
30
35
|
}
|
|
31
36
|
if (fieldSchema.maximum) {
|
|
32
|
-
if (fieldSchema.exclusiveMaximum
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
if (fieldSchema.exclusiveMaximum) {
|
|
38
|
+
if (value >= fieldSchema.maximum) {
|
|
39
|
+
error(handlers, ParseError.exceedsMaximum, path)
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
} else if (value > fieldSchema.maximum) {
|
|
43
|
+
error(handlers, ParseError.exceedsMaximum, path)
|
|
44
|
+
return false
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
if (fieldSchema.minimum) {
|
|
39
|
-
if (fieldSchema.exclusiveMinimum
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
if (fieldSchema.exclusiveMinimum) {
|
|
49
|
+
if (value <= fieldSchema.minimum) {
|
|
50
|
+
error(handlers, ParseError.subceedsMinimum, path)
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
} else if (value < fieldSchema.minimum) {
|
|
54
|
+
error(handlers, ParseError.subceedsMinimum, path)
|
|
55
|
+
return false
|
|
43
56
|
}
|
|
44
57
|
}
|
|
45
58
|
return value
|
|
46
59
|
}
|
|
47
60
|
|
|
48
61
|
const shared = (
|
|
62
|
+
handlers: BasedSetHandlers,
|
|
49
63
|
path: (number | string)[],
|
|
50
64
|
value: any,
|
|
51
65
|
fieldSchema:
|
|
@@ -55,22 +69,23 @@ const shared = (
|
|
|
55
69
|
): any => {
|
|
56
70
|
if (typeof value === 'object') {
|
|
57
71
|
if (value.$increment) {
|
|
58
|
-
validate([...path, '$increment'], value.$increment, fieldSchema)
|
|
72
|
+
validate(handlers, [...path, '$increment'], value.$increment, fieldSchema)
|
|
59
73
|
}
|
|
60
74
|
if (value.$decrement) {
|
|
61
|
-
validate([...path, '$decrement'], value.$decrement, fieldSchema)
|
|
75
|
+
validate(handlers, [...path, '$decrement'], value.$decrement, fieldSchema)
|
|
62
76
|
}
|
|
63
77
|
if (value.$value !== undefined) {
|
|
64
|
-
validate(path, value.$value, fieldSchema)
|
|
78
|
+
validate(handlers, path, value.$value, fieldSchema)
|
|
65
79
|
}
|
|
66
80
|
if (value.$default !== undefined) {
|
|
67
81
|
if (value.$value !== undefined) {
|
|
68
|
-
error(
|
|
82
|
+
error(handlers, ParseError.valueAndDefault, path)
|
|
83
|
+
return
|
|
69
84
|
}
|
|
70
|
-
validate(path, value.$default, fieldSchema)
|
|
85
|
+
validate(handlers, path, value.$default, fieldSchema)
|
|
71
86
|
}
|
|
72
87
|
} else {
|
|
73
|
-
validate(path, value, fieldSchema)
|
|
88
|
+
validate(handlers, path, value, fieldSchema)
|
|
74
89
|
}
|
|
75
90
|
return value
|
|
76
91
|
}
|
|
@@ -85,17 +100,19 @@ export const timestamp: Parser<'timestamp'> = async (
|
|
|
85
100
|
noCollect
|
|
86
101
|
) => {
|
|
87
102
|
if (typeof value === 'string') {
|
|
103
|
+
// TODO: now + 10 and stuff
|
|
88
104
|
if (value === 'now') {
|
|
89
105
|
value = Date.now()
|
|
90
106
|
} else {
|
|
91
107
|
const d = new Date(value)
|
|
92
108
|
value = d.valueOf()
|
|
93
109
|
if (isNaN(value)) {
|
|
94
|
-
error(
|
|
110
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
111
|
+
return
|
|
95
112
|
}
|
|
96
113
|
}
|
|
97
114
|
}
|
|
98
|
-
const parsedValue = shared(path, value, fieldSchema)
|
|
115
|
+
const parsedValue = shared(handlers, path, value, fieldSchema)
|
|
99
116
|
if (!noCollect) {
|
|
100
117
|
handlers.collect({
|
|
101
118
|
path,
|
|
@@ -116,7 +133,7 @@ export const number: Parser<'number'> = async (
|
|
|
116
133
|
handlers,
|
|
117
134
|
noCollect
|
|
118
135
|
) => {
|
|
119
|
-
const parsedValue = shared(path, value, fieldSchema)
|
|
136
|
+
const parsedValue = shared(handlers, path, value, fieldSchema)
|
|
120
137
|
if (!noCollect) {
|
|
121
138
|
handlers.collect({
|
|
122
139
|
path,
|
|
@@ -137,7 +154,7 @@ export const integer: Parser<'integer'> = async (
|
|
|
137
154
|
handlers,
|
|
138
155
|
noCollect
|
|
139
156
|
) => {
|
|
140
|
-
const parsedValue = shared(path, value, fieldSchema)
|
|
157
|
+
const parsedValue = shared(handlers, path, value, fieldSchema)
|
|
141
158
|
if (!noCollect) {
|
|
142
159
|
handlers.collect({
|
|
143
160
|
path,
|
|
@@ -18,14 +18,23 @@ export const parseValueAndDefault: Parser<keyof BasedSchemaFields> = async (
|
|
|
18
18
|
const parse = parsers[typeDef]
|
|
19
19
|
if (value.$value !== undefined) {
|
|
20
20
|
// TODO: for errors handle path a bit smarter...
|
|
21
|
-
parse(
|
|
21
|
+
await parse(
|
|
22
|
+
path,
|
|
23
|
+
value.$value,
|
|
24
|
+
fieldSchema,
|
|
25
|
+
typeSchema,
|
|
26
|
+
target,
|
|
27
|
+
handlers,
|
|
28
|
+
true
|
|
29
|
+
)
|
|
22
30
|
handled = true
|
|
23
31
|
}
|
|
24
32
|
if (value.$default !== undefined) {
|
|
25
33
|
if (value.$value !== undefined) {
|
|
26
|
-
error(
|
|
34
|
+
error(handlers, ParseError.valueAndDefault, path)
|
|
27
35
|
}
|
|
28
|
-
|
|
36
|
+
console.info('->', path, 'go')
|
|
37
|
+
await parse(
|
|
29
38
|
path,
|
|
30
39
|
value.$default,
|
|
31
40
|
fieldSchema,
|
|
@@ -38,6 +47,7 @@ export const parseValueAndDefault: Parser<keyof BasedSchemaFields> = async (
|
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
if (handled && !noCollect) {
|
|
50
|
+
console.log('collect?')
|
|
41
51
|
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
42
52
|
}
|
|
43
53
|
return handled
|
package/src/set/references.ts
CHANGED
|
@@ -16,14 +16,15 @@ export const reference: Parser<'reference'> = async (
|
|
|
16
16
|
// $value
|
|
17
17
|
|
|
18
18
|
if (typeof value !== 'string') {
|
|
19
|
-
error(
|
|
19
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
if ('allowedTypes' in fieldSchema) {
|
|
23
23
|
const prefix = value.slice(0, 2)
|
|
24
24
|
const targetType = target.schema.prefixToTypeMapping[prefix]
|
|
25
25
|
if (!targetType) {
|
|
26
|
-
error(
|
|
26
|
+
error(handlers, ParseError.referenceIsIncorrectType, path)
|
|
27
|
+
return
|
|
27
28
|
}
|
|
28
29
|
let typeMatches = false
|
|
29
30
|
for (const t of fieldSchema.allowedTypes) {
|
|
@@ -37,18 +38,21 @@ export const reference: Parser<'reference'> = async (
|
|
|
37
38
|
typeMatches = true
|
|
38
39
|
if (t.$filter) {
|
|
39
40
|
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
40
|
-
error(
|
|
41
|
+
error(handlers, ParseError.referenceIsIncorrectType, path)
|
|
42
|
+
return
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
} else if (!t.type && t.$filter) {
|
|
44
46
|
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
45
|
-
error(
|
|
47
|
+
error(handlers, ParseError.referenceIsIncorrectType, path)
|
|
48
|
+
return
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
53
|
if (typeMatches === false) {
|
|
51
|
-
error(
|
|
54
|
+
error(handlers, ParseError.referenceIsIncorrectType, path)
|
|
55
|
+
return
|
|
52
56
|
}
|
|
53
57
|
}
|
|
54
58
|
if (!noCollect) {
|
|
@@ -100,6 +104,8 @@ export const references: Parser<'references'> = async (
|
|
|
100
104
|
})
|
|
101
105
|
)
|
|
102
106
|
}
|
|
107
|
+
} else {
|
|
108
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
103
109
|
}
|
|
104
110
|
if (!noCollect) {
|
|
105
111
|
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
package/src/set/rest.ts
CHANGED
|
@@ -15,7 +15,7 @@ export const cardinality: Parser<'cardinality'> = async (
|
|
|
15
15
|
) => {
|
|
16
16
|
if (value && typeof value === 'object') {
|
|
17
17
|
if (value.$default !== undefined) {
|
|
18
|
-
error(
|
|
18
|
+
error(handlers, ParseError.defaultNotSupported, path)
|
|
19
19
|
}
|
|
20
20
|
if (value.$value !== undefined) {
|
|
21
21
|
value = hashObjectIgnoreKeyOrder(value.$value).toString(16)
|
|
@@ -54,7 +54,7 @@ export const boolean: Parser<'boolean'> = async (
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
if (typeof value !== 'boolean') {
|
|
57
|
-
error(
|
|
57
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
if (!noCollect) {
|
|
@@ -91,7 +91,7 @@ export const enumParser: Parser<'enum'> = async (
|
|
|
91
91
|
return
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
-
error(
|
|
94
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
|
|
@@ -130,6 +130,6 @@ export const json: Parser<'json'> = async (
|
|
|
130
130
|
})
|
|
131
131
|
}
|
|
132
132
|
} catch (err) {
|
|
133
|
-
error(
|
|
133
|
+
error(handlers, ParseError.incorrectFormat, path)
|
|
134
134
|
}
|
|
135
135
|
}
|