@based/schema 4.1.1 → 5.0.0-alpha.0
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/README.md +152 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/lang.d.ts +152 -0
- package/dist/lang.js +149 -0
- package/dist/mermaid.d.ts +3 -0
- package/dist/mermaid.js +24 -0
- package/dist/parse/assert.d.ts +1 -1
- package/dist/parse/assert.js +2 -2
- package/dist/parse/errors.d.ts +2 -0
- package/dist/parse/errors.js +2 -0
- package/dist/parse/index.d.ts +5 -4
- package/dist/parse/index.js +43 -41
- package/dist/parse/props.js +159 -75
- package/dist/parse/utils.d.ts +2 -2
- package/dist/parse/utils.js +11 -2
- package/dist/types.d.ts +66 -23
- package/dist/types.js +93 -0
- package/package.json +4 -3
- package/dist/parsePayload/index.d.ts +0 -3
- package/dist/parsePayload/index.js +0 -2
- package/dist/parseSchema/assert.d.ts +0 -6
- package/dist/parseSchema/assert.js +0 -27
- package/dist/parseSchema/errors.d.ts +0 -19
- package/dist/parseSchema/errors.js +0 -19
- package/dist/parseSchema/index.d.ts +0 -20
- package/dist/parseSchema/index.js +0 -132
- package/dist/parseSchema/props.d.ts +0 -7
- package/dist/parseSchema/props.js +0 -256
- package/dist/parseSchema/utils.d.ts +0 -3
- package/dist/parseSchema/utils.js +0 -29
- package/dist/src/compat/index.d.ts +0 -6
- package/dist/src/compat/index.js +0 -217
- package/dist/src/compat/oldSchemaType.d.ts +0 -93
- package/dist/src/compat/oldSchemaType.js +0 -2
- package/dist/src/display/index.d.ts +0 -2
- package/dist/src/display/index.js +0 -16
- package/dist/src/display/number.d.ts +0 -4
- package/dist/src/display/number.js +0 -97
- package/dist/src/display/string.d.ts +0 -5
- package/dist/src/display/string.js +0 -26
- package/dist/src/display/timestamp.d.ts +0 -5
- package/dist/src/display/timestamp.js +0 -134
- package/dist/src/error.d.ts +0 -21
- package/dist/src/error.js +0 -23
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.js +0 -7
- package/dist/src/languages.d.ts +0 -188
- package/dist/src/languages.js +0 -188
- package/dist/src/set/fields/array.d.ts +0 -2
- package/dist/src/set/fields/array.js +0 -119
- package/dist/src/set/fields/index.d.ts +0 -3
- package/dist/src/set/fields/index.js +0 -85
- package/dist/src/set/fields/number.d.ts +0 -4
- package/dist/src/set/fields/number.js +0 -123
- package/dist/src/set/fields/object.d.ts +0 -3
- package/dist/src/set/fields/object.js +0 -28
- package/dist/src/set/fields/references.d.ts +0 -3
- package/dist/src/set/fields/references.js +0 -136
- package/dist/src/set/fields/set.d.ts +0 -2
- package/dist/src/set/fields/set.js +0 -59
- package/dist/src/set/fields/string.d.ts +0 -3
- package/dist/src/set/fields/string.js +0 -289
- package/dist/src/set/index.d.ts +0 -3
- package/dist/src/set/index.js +0 -205
- package/dist/src/set/isValidId.d.ts +0 -2
- package/dist/src/set/isValidId.js +0 -17
- package/dist/src/types.d.ts +0 -205
- package/dist/src/types.js +0 -113
- package/dist/src/updateSchema.d.ts +0 -2
- package/dist/src/updateSchema.js +0 -12
- package/dist/src/validateSchema/basedSchemaTypeValidator.d.ts +0 -3
- package/dist/src/validateSchema/basedSchemaTypeValidator.js +0 -45
- package/dist/src/validateSchema/fieldValidators.d.ts +0 -27
- package/dist/src/validateSchema/fieldValidators.js +0 -360
- package/dist/src/validateSchema/index.d.ts +0 -17
- package/dist/src/validateSchema/index.js +0 -109
- package/dist/src/validateSchema/utils.d.ts +0 -25
- package/dist/src/validateSchema/utils.js +0 -61
- package/dist/src/walker/args.d.ts +0 -36
- package/dist/src/walker/args.js +0 -158
- package/dist/src/walker/index.d.ts +0 -6
- package/dist/src/walker/index.js +0 -31
- package/dist/src/walker/parse.d.ts +0 -3
- package/dist/src/walker/parse.js +0 -182
- package/dist/src/walker/types.d.ts +0 -45
- package/dist/src/walker/types.js +0 -7
- package/dist/test/array.d.ts +0 -1
- package/dist/test/array.js +0 -342
- package/dist/test/compat.d.ts +0 -1
- package/dist/test/compat.js +0 -97
- package/dist/test/data/newSchemas.d.ts +0 -2
- package/dist/test/data/newSchemas.js +0 -489
- package/dist/test/data/oldSchemas.d.ts +0 -2
- package/dist/test/data/oldSchemas.js +0 -5058
- package/dist/test/display.d.ts +0 -1
- package/dist/test/display.js +0 -103
- package/dist/test/json.d.ts +0 -1
- package/dist/test/json.js +0 -40
- package/dist/test/number.d.ts +0 -1
- package/dist/test/number.js +0 -374
- package/dist/test/reference.d.ts +0 -1
- package/dist/test/reference.js +0 -188
- package/dist/test/rest.d.ts +0 -1
- package/dist/test/rest.js +0 -168
- package/dist/test/set.d.ts +0 -1
- package/dist/test/set.js +0 -95
- package/dist/test/string.d.ts +0 -1
- package/dist/test/string.js +0 -120
- package/dist/test/text.d.ts +0 -1
- package/dist/test/text.js +0 -324
- package/dist/test/utils/index.d.ts +0 -11
- package/dist/test/utils/index.js +0 -17
- package/dist/test/validateSchema/basic.d.ts +0 -1
- package/dist/test/validateSchema/basic.js +0 -94
- package/dist/test/validateSchema/fields.d.ts +0 -1
- package/dist/test/validateSchema/fields.js +0 -436
- package/dist/test/validateSchema/languages.d.ts +0 -1
- package/dist/test/validateSchema/languages.js +0 -124
- package/dist/test/validateSchema/realWorld.d.ts +0 -1
- package/dist/test/validateSchema/realWorld.js +0 -13
- package/dist/test/walker.d.ts +0 -1
- package/dist/test/walker.js +0 -289
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
import { basedSchemaNumberFormats } from '../display/number.js';
|
|
2
|
-
import { basedSchemaDisplayFormats } from '../display/string.js';
|
|
3
|
-
import { basedSchemaDateFormats } from '../display/timestamp.js';
|
|
4
|
-
import { ParseError } from '../error.js';
|
|
5
|
-
import { basedSchemaFieldTypes, basedSchemaStringFormatValues, languages, } from '../types.js';
|
|
6
|
-
import { validate } from './index.js';
|
|
7
|
-
import { mustBeArray, mustBeBidirectional, mustBeBoolean, mustBeNumber, mustBeString, mustBeStringArray, } from './utils.js';
|
|
8
|
-
export const mustBeField = (value, path, newSchema, oldSchema, options) => {
|
|
9
|
-
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
10
|
-
return [
|
|
11
|
-
{
|
|
12
|
-
code: ParseError.incorrectFormat,
|
|
13
|
-
path,
|
|
14
|
-
},
|
|
15
|
-
];
|
|
16
|
-
}
|
|
17
|
-
const type = value.hasOwnProperty('enum') ? 'enum' : value.type;
|
|
18
|
-
if ((options?.limitTo === 'primitives' &&
|
|
19
|
-
![
|
|
20
|
-
'string',
|
|
21
|
-
'number',
|
|
22
|
-
'integer',
|
|
23
|
-
'timestamp',
|
|
24
|
-
'json',
|
|
25
|
-
'boolean',
|
|
26
|
-
'enum',
|
|
27
|
-
].includes(type)) ||
|
|
28
|
-
(options?.limitTo === 'enumerables' &&
|
|
29
|
-
!['text', 'object', 'record', 'array', 'set'].includes(type))) {
|
|
30
|
-
return [
|
|
31
|
-
{
|
|
32
|
-
code: ParseError.incorrectFormat,
|
|
33
|
-
path: path.concat('type'),
|
|
34
|
-
},
|
|
35
|
-
];
|
|
36
|
-
}
|
|
37
|
-
let validator;
|
|
38
|
-
switch (type) {
|
|
39
|
-
case 'string':
|
|
40
|
-
validator = basedSchemaStringValidator;
|
|
41
|
-
break;
|
|
42
|
-
case 'enum':
|
|
43
|
-
validator = basedSchemaFieldEnumValidator;
|
|
44
|
-
break;
|
|
45
|
-
case 'cardinality':
|
|
46
|
-
validator = basedSchemaFieldCardinalityValidator;
|
|
47
|
-
break;
|
|
48
|
-
case 'number':
|
|
49
|
-
validator = basedSchemaFieldNumberValidator;
|
|
50
|
-
break;
|
|
51
|
-
case 'integer':
|
|
52
|
-
validator = basedSchemaFieldIntegerValidator;
|
|
53
|
-
break;
|
|
54
|
-
case 'timestamp':
|
|
55
|
-
validator = basedSchemaFieldTimeStampValidator;
|
|
56
|
-
break;
|
|
57
|
-
case 'boolean':
|
|
58
|
-
validator = basedSchemaFieldBooleanValidator;
|
|
59
|
-
break;
|
|
60
|
-
case 'json':
|
|
61
|
-
validator = basedSchemaFieldJSONValidator;
|
|
62
|
-
break;
|
|
63
|
-
case 'text':
|
|
64
|
-
validator = basedSchemaFieldTextValidator;
|
|
65
|
-
break;
|
|
66
|
-
case 'object':
|
|
67
|
-
validator = basedSchemaFieldObjectValidator;
|
|
68
|
-
break;
|
|
69
|
-
case 'record':
|
|
70
|
-
validator = basedSchemaFieldRecordValidator;
|
|
71
|
-
break;
|
|
72
|
-
case 'array':
|
|
73
|
-
validator = basedSchemaFieldArrayValidator;
|
|
74
|
-
break;
|
|
75
|
-
case 'set':
|
|
76
|
-
validator = basedSchemaFieldSetValidator;
|
|
77
|
-
break;
|
|
78
|
-
case 'reference':
|
|
79
|
-
validator = basedSchemaFieldReferenceValidator;
|
|
80
|
-
break;
|
|
81
|
-
case 'references':
|
|
82
|
-
validator = basedSchemaFieldReferencesValidator;
|
|
83
|
-
break;
|
|
84
|
-
default:
|
|
85
|
-
validator = basedSchemaFieldSharedValidator;
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
return validate(validator, value, path, newSchema, oldSchema);
|
|
89
|
-
};
|
|
90
|
-
export const mustBeFields = (value, path, newSchema, oldSchema) => {
|
|
91
|
-
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
92
|
-
return [
|
|
93
|
-
{
|
|
94
|
-
code: ParseError.incorrectFormat,
|
|
95
|
-
path,
|
|
96
|
-
},
|
|
97
|
-
];
|
|
98
|
-
}
|
|
99
|
-
const errors = [];
|
|
100
|
-
for (const key in value) {
|
|
101
|
-
if (value.hasOwnProperty(key)) {
|
|
102
|
-
errors.push(...mustBeField(value[key], path.concat(key), newSchema, oldSchema));
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return errors;
|
|
106
|
-
};
|
|
107
|
-
export const basedSchemaFieldSharedValidator = {
|
|
108
|
-
type: {
|
|
109
|
-
validator: (value, path) => basedSchemaFieldTypes.includes(value)
|
|
110
|
-
? []
|
|
111
|
-
: [{ code: ParseError.incorrectFieldType, path }],
|
|
112
|
-
},
|
|
113
|
-
hooks: {
|
|
114
|
-
validator: (value, path) => {
|
|
115
|
-
if (typeof value !== 'object') {
|
|
116
|
-
return [{ code: ParseError.incorrectFormat, path }];
|
|
117
|
-
}
|
|
118
|
-
const items = Array.isArray(value) ? value : [{ ...value }];
|
|
119
|
-
const errors = [];
|
|
120
|
-
items.forEach((item, index) => {
|
|
121
|
-
if (item.hook) {
|
|
122
|
-
errors.push(...mustBeString(item.hook, path.concat(Array.isArray(value) ? String(index) : [], 'hook')));
|
|
123
|
-
}
|
|
124
|
-
if (item.interval) {
|
|
125
|
-
errors.push(...mustBeNumber(item.interval, path.concat(Array.isArray(value) ? String(index) : [], 'interval')));
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
return errors;
|
|
129
|
-
},
|
|
130
|
-
optional: true,
|
|
131
|
-
},
|
|
132
|
-
$id: {
|
|
133
|
-
validator: mustBeString,
|
|
134
|
-
optional: true,
|
|
135
|
-
},
|
|
136
|
-
isRequired: {
|
|
137
|
-
validator: mustBeBoolean,
|
|
138
|
-
optional: true,
|
|
139
|
-
},
|
|
140
|
-
$schema: {
|
|
141
|
-
validator: mustBeString,
|
|
142
|
-
optional: true,
|
|
143
|
-
},
|
|
144
|
-
title: {
|
|
145
|
-
validator: mustBeString,
|
|
146
|
-
optional: true,
|
|
147
|
-
},
|
|
148
|
-
description: {
|
|
149
|
-
validator: mustBeString,
|
|
150
|
-
optional: true,
|
|
151
|
-
},
|
|
152
|
-
index: {
|
|
153
|
-
validator: mustBeNumber,
|
|
154
|
-
optional: true,
|
|
155
|
-
},
|
|
156
|
-
readOnly: {
|
|
157
|
-
validator: mustBeBoolean,
|
|
158
|
-
optional: true,
|
|
159
|
-
},
|
|
160
|
-
writeOnly: {
|
|
161
|
-
validator: mustBeBoolean,
|
|
162
|
-
optional: true,
|
|
163
|
-
},
|
|
164
|
-
$comment: {
|
|
165
|
-
validator: mustBeString,
|
|
166
|
-
optional: true,
|
|
167
|
-
},
|
|
168
|
-
examples: { optional: true },
|
|
169
|
-
default: { optional: true },
|
|
170
|
-
customValidator: { optional: true },
|
|
171
|
-
$defs: { optional: true },
|
|
172
|
-
$delete: {
|
|
173
|
-
validator: mustBeBoolean,
|
|
174
|
-
optional: true,
|
|
175
|
-
},
|
|
176
|
-
};
|
|
177
|
-
export const basedSchemaStringSharedValidator = {
|
|
178
|
-
minLength: {
|
|
179
|
-
validator: mustBeNumber,
|
|
180
|
-
optional: true,
|
|
181
|
-
},
|
|
182
|
-
maxLength: {
|
|
183
|
-
validator: mustBeNumber,
|
|
184
|
-
optional: true,
|
|
185
|
-
},
|
|
186
|
-
contentMediaEncoding: {
|
|
187
|
-
validator: mustBeString,
|
|
188
|
-
optional: true,
|
|
189
|
-
},
|
|
190
|
-
contentMediaType: {
|
|
191
|
-
validator: (value, path) => /^\w+\/\w+$/.test(value)
|
|
192
|
-
? []
|
|
193
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
194
|
-
optional: true,
|
|
195
|
-
},
|
|
196
|
-
pattern: {
|
|
197
|
-
validator: mustBeString,
|
|
198
|
-
optional: true,
|
|
199
|
-
},
|
|
200
|
-
format: {
|
|
201
|
-
validator: (value, path) => basedSchemaStringFormatValues.includes(value)
|
|
202
|
-
? []
|
|
203
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
204
|
-
optional: true,
|
|
205
|
-
},
|
|
206
|
-
display: {
|
|
207
|
-
validator: (value, path) => basedSchemaDisplayFormats.includes(value)
|
|
208
|
-
? []
|
|
209
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
210
|
-
optional: true,
|
|
211
|
-
},
|
|
212
|
-
multiline: {
|
|
213
|
-
validator: mustBeBoolean,
|
|
214
|
-
optional: true,
|
|
215
|
-
},
|
|
216
|
-
};
|
|
217
|
-
export const basedSchemaStringValidator = {
|
|
218
|
-
...basedSchemaFieldSharedValidator,
|
|
219
|
-
...basedSchemaStringSharedValidator,
|
|
220
|
-
};
|
|
221
|
-
export const basedSchemaFieldEnumValidator = {
|
|
222
|
-
...basedSchemaFieldSharedValidator,
|
|
223
|
-
enum: {
|
|
224
|
-
validator: mustBeArray,
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
export const basedSchemaFieldCardinalityValidator = {
|
|
228
|
-
...basedSchemaFieldSharedValidator,
|
|
229
|
-
};
|
|
230
|
-
export const basedSchemaNumberDefaultsValidator = {
|
|
231
|
-
multipleOf: {
|
|
232
|
-
validator: mustBeNumber,
|
|
233
|
-
optional: true,
|
|
234
|
-
},
|
|
235
|
-
minimum: {
|
|
236
|
-
validator: mustBeNumber,
|
|
237
|
-
optional: true,
|
|
238
|
-
},
|
|
239
|
-
maximum: {
|
|
240
|
-
validator: mustBeNumber,
|
|
241
|
-
optional: true,
|
|
242
|
-
},
|
|
243
|
-
exclusiveMaximum: {
|
|
244
|
-
validator: mustBeBoolean,
|
|
245
|
-
optional: true,
|
|
246
|
-
},
|
|
247
|
-
exclusiveMinimum: {
|
|
248
|
-
validator: mustBeBoolean,
|
|
249
|
-
optional: true,
|
|
250
|
-
},
|
|
251
|
-
};
|
|
252
|
-
export const basedSchemaFieldNumberValidator = {
|
|
253
|
-
...basedSchemaFieldSharedValidator,
|
|
254
|
-
...basedSchemaNumberDefaultsValidator,
|
|
255
|
-
display: {
|
|
256
|
-
validator: (value, path) => basedSchemaNumberFormats.includes(value) || /^round-\d+$/.test(value)
|
|
257
|
-
? []
|
|
258
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
259
|
-
optional: true,
|
|
260
|
-
},
|
|
261
|
-
};
|
|
262
|
-
export const basedSchemaFieldIntegerValidator = {
|
|
263
|
-
...basedSchemaFieldSharedValidator,
|
|
264
|
-
...basedSchemaNumberDefaultsValidator,
|
|
265
|
-
display: {
|
|
266
|
-
validator: (value, path) => basedSchemaNumberFormats.includes(value) || /^round-\d+$/.test(value)
|
|
267
|
-
? []
|
|
268
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
269
|
-
optional: true,
|
|
270
|
-
},
|
|
271
|
-
};
|
|
272
|
-
export const basedSchemaFieldTimeStampValidator = {
|
|
273
|
-
...basedSchemaFieldSharedValidator,
|
|
274
|
-
...basedSchemaNumberDefaultsValidator,
|
|
275
|
-
display: {
|
|
276
|
-
validator: (value, path) => basedSchemaDateFormats.includes(value)
|
|
277
|
-
? []
|
|
278
|
-
: [{ code: ParseError.incorrectFormat, path }],
|
|
279
|
-
optional: true,
|
|
280
|
-
},
|
|
281
|
-
};
|
|
282
|
-
export const basedSchemaFieldBooleanValidator = {
|
|
283
|
-
...basedSchemaFieldSharedValidator,
|
|
284
|
-
};
|
|
285
|
-
export const basedSchemaFieldJSONValidator = {
|
|
286
|
-
...basedSchemaFieldSharedValidator,
|
|
287
|
-
format: {
|
|
288
|
-
validator: (value, path) => value === 'rich-text' ? [] : [{ code: ParseError.incorrectFormat, path }],
|
|
289
|
-
optional: true,
|
|
290
|
-
},
|
|
291
|
-
};
|
|
292
|
-
export const basedSchemaFieldAnyValidator = {
|
|
293
|
-
...basedSchemaFieldSharedValidator,
|
|
294
|
-
};
|
|
295
|
-
export const basedSchemaFieldTextValidator = {
|
|
296
|
-
...basedSchemaFieldSharedValidator,
|
|
297
|
-
...basedSchemaStringSharedValidator,
|
|
298
|
-
required: {
|
|
299
|
-
validator: (value, path) => Array.isArray(value) &&
|
|
300
|
-
value.every((i) => Object.keys(languages).includes(i))
|
|
301
|
-
? []
|
|
302
|
-
: [{ code: ParseError.languageNotSupported, path }],
|
|
303
|
-
optional: true,
|
|
304
|
-
},
|
|
305
|
-
};
|
|
306
|
-
export const basedSchemaFieldObjectValidator = {
|
|
307
|
-
...basedSchemaFieldSharedValidator,
|
|
308
|
-
properties: {
|
|
309
|
-
validator: mustBeFields,
|
|
310
|
-
},
|
|
311
|
-
required: {
|
|
312
|
-
validator: mustBeStringArray,
|
|
313
|
-
optional: true,
|
|
314
|
-
},
|
|
315
|
-
};
|
|
316
|
-
export const basedSchemaFieldRecordValidator = {
|
|
317
|
-
...basedSchemaFieldSharedValidator,
|
|
318
|
-
values: {
|
|
319
|
-
validator: mustBeField,
|
|
320
|
-
},
|
|
321
|
-
};
|
|
322
|
-
export const basedSchemaFieldArrayValidator = {
|
|
323
|
-
...basedSchemaFieldSharedValidator,
|
|
324
|
-
items: {
|
|
325
|
-
validator: mustBeField,
|
|
326
|
-
},
|
|
327
|
-
};
|
|
328
|
-
export const basedSchemaFieldSetValidator = {
|
|
329
|
-
...basedSchemaFieldSharedValidator,
|
|
330
|
-
items: {
|
|
331
|
-
validator: (value, path, newSchema, oldSchema) => mustBeField(value, path, newSchema, oldSchema, { limitTo: 'primitives' }),
|
|
332
|
-
},
|
|
333
|
-
};
|
|
334
|
-
export const basedSchemaFieldReferenceValidator = {
|
|
335
|
-
...basedSchemaFieldSharedValidator,
|
|
336
|
-
inverseProperty: {
|
|
337
|
-
validator: mustBeBidirectional,
|
|
338
|
-
optional: true,
|
|
339
|
-
},
|
|
340
|
-
allowedType: {
|
|
341
|
-
// TODO: validator
|
|
342
|
-
optional: true,
|
|
343
|
-
},
|
|
344
|
-
};
|
|
345
|
-
export const basedSchemaFieldReferencesValidator = {
|
|
346
|
-
...basedSchemaFieldSharedValidator,
|
|
347
|
-
inverseProperty: {
|
|
348
|
-
validator: mustBeBidirectional,
|
|
349
|
-
optional: true,
|
|
350
|
-
},
|
|
351
|
-
allowedType: {
|
|
352
|
-
// TODO: validator
|
|
353
|
-
optional: true,
|
|
354
|
-
},
|
|
355
|
-
sortable: {
|
|
356
|
-
validator: mustBeBoolean,
|
|
357
|
-
optional: true,
|
|
358
|
-
},
|
|
359
|
-
};
|
|
360
|
-
//# sourceMappingURL=fieldValidators.js.map
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ParseError } from '../error.js';
|
|
2
|
-
import { BasedSchema, BasedSchemaPartial } from '../types.js';
|
|
3
|
-
export type ValidateSchemaError = {
|
|
4
|
-
code: ParseError;
|
|
5
|
-
path?: string[];
|
|
6
|
-
};
|
|
7
|
-
export type Validator<T> = {
|
|
8
|
-
[P in keyof Required<T>]: {
|
|
9
|
-
optional?: boolean;
|
|
10
|
-
validator?: (value: any, path: string[], newSchema: BasedSchemaPartial, oldSchema: BasedSchema) => ValidateSchemaError[];
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
export declare const validate: <T>(validator: Validator<T>, target: T, path: string[], newSchema: BasedSchemaPartial, oldSchema: BasedSchema) => ValidateSchemaError[];
|
|
14
|
-
export declare const validateSchema: (newSchema: BasedSchemaPartial, oldSchema?: BasedSchema) => Promise<{
|
|
15
|
-
valid?: true;
|
|
16
|
-
errors?: ValidateSchemaError[];
|
|
17
|
-
}>;
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { ParseError } from '../error.js';
|
|
2
|
-
import { languages, } from '../types.js';
|
|
3
|
-
import { basedSchemaTypeValidator } from './basedSchemaTypeValidator.js';
|
|
4
|
-
const basedSchemaValidator = {
|
|
5
|
-
language: {
|
|
6
|
-
validator: (value, path) =>
|
|
7
|
-
// language not supported
|
|
8
|
-
languages.includes(value)
|
|
9
|
-
? []
|
|
10
|
-
: [{ code: ParseError.languageNotSupported, path }],
|
|
11
|
-
},
|
|
12
|
-
translations: {
|
|
13
|
-
validator: (value, path, newSchema, oldSchema) => {
|
|
14
|
-
// translations property needs to be an array
|
|
15
|
-
if (!Array.isArray(value)) {
|
|
16
|
-
return [{ code: ParseError.incorrectFormat, path }];
|
|
17
|
-
}
|
|
18
|
-
const language = newSchema.language || oldSchema.language;
|
|
19
|
-
// translations property cannot include language value
|
|
20
|
-
if (language && value.includes(language)) {
|
|
21
|
-
return [{ code: ParseError.invalidProperty, path }];
|
|
22
|
-
}
|
|
23
|
-
// language not supported
|
|
24
|
-
return value.every((l) => languages.includes(l))
|
|
25
|
-
? []
|
|
26
|
-
: [{ code: ParseError.languageNotSupported, path }];
|
|
27
|
-
},
|
|
28
|
-
optional: true,
|
|
29
|
-
},
|
|
30
|
-
languageFallbacks: {
|
|
31
|
-
validator: (value, path, newSchema, oldSchema) => {
|
|
32
|
-
// languageFallbacks needs to be an object
|
|
33
|
-
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
34
|
-
return [{ code: ParseError.incorrectFormat, path }];
|
|
35
|
-
}
|
|
36
|
-
const schemaLangs = [newSchema.language || oldSchema?.language].concat(newSchema.translations || oldSchema?.translations);
|
|
37
|
-
for (const key in value) {
|
|
38
|
-
// languageFallbacks keys must be a language or a translation
|
|
39
|
-
if (!schemaLangs.includes(key)) {
|
|
40
|
-
return [{ code: ParseError.noLanguageFound, path }];
|
|
41
|
-
}
|
|
42
|
-
// languageFallbacks language values need to be array
|
|
43
|
-
if (!Array.isArray(value[key])) {
|
|
44
|
-
return [{ code: ParseError.incorrectFormat, path }];
|
|
45
|
-
}
|
|
46
|
-
if (!value[key].every((l) => schemaLangs.includes(l))) {
|
|
47
|
-
return [{ code: ParseError.noLanguageFound, path }];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return [];
|
|
51
|
-
},
|
|
52
|
-
optional: true,
|
|
53
|
-
},
|
|
54
|
-
root: {
|
|
55
|
-
validator: (value, path, newSchema, oldSchema) => validate(basedSchemaTypeValidator, value, path, newSchema, oldSchema),
|
|
56
|
-
},
|
|
57
|
-
$defs: {},
|
|
58
|
-
types: {
|
|
59
|
-
validator: (value, path, newSchema, oldSchema) => {
|
|
60
|
-
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
61
|
-
return [
|
|
62
|
-
{
|
|
63
|
-
code: ParseError.incorrectFormat,
|
|
64
|
-
path,
|
|
65
|
-
},
|
|
66
|
-
];
|
|
67
|
-
}
|
|
68
|
-
const errors = [];
|
|
69
|
-
for (const key in value) {
|
|
70
|
-
if (value.hasOwnProperty(key)) {
|
|
71
|
-
errors.push(...validate(basedSchemaTypeValidator, value[key], path.concat(key), newSchema, oldSchema));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return errors;
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
// TODO:
|
|
78
|
-
prefixToTypeMapping: {},
|
|
79
|
-
};
|
|
80
|
-
export const validate = (validator, target, path, newSchema, oldSchema) => {
|
|
81
|
-
const errors = [];
|
|
82
|
-
for (const key in target) {
|
|
83
|
-
if (target.hasOwnProperty(key)) {
|
|
84
|
-
if (validator[key]) {
|
|
85
|
-
if (validator[key].validator) {
|
|
86
|
-
const result = validator[key].validator(target[key], path.concat(key), newSchema, oldSchema);
|
|
87
|
-
errors.push(...result);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
errors.push({
|
|
92
|
-
code: ParseError.invalidProperty,
|
|
93
|
-
path: path.concat(key),
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return errors;
|
|
99
|
-
};
|
|
100
|
-
export const validateSchema = async (newSchema, oldSchema) => {
|
|
101
|
-
const errors = [];
|
|
102
|
-
if (newSchema === null || typeof newSchema !== 'object') {
|
|
103
|
-
errors.push({ code: ParseError.invalidSchemaFormat });
|
|
104
|
-
return { errors };
|
|
105
|
-
}
|
|
106
|
-
errors.push(...validate(basedSchemaValidator, newSchema, [], newSchema, oldSchema));
|
|
107
|
-
return errors.length ? { errors } : { valid: true };
|
|
108
|
-
};
|
|
109
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { ParseError } from '../error.js';
|
|
2
|
-
export declare const mustBeString: (value: string, path: string[]) => {
|
|
3
|
-
code: ParseError;
|
|
4
|
-
path: string[];
|
|
5
|
-
}[];
|
|
6
|
-
export declare const mustBeArray: (value: string[], path: string[]) => {
|
|
7
|
-
code: ParseError;
|
|
8
|
-
path: string[];
|
|
9
|
-
}[];
|
|
10
|
-
export declare const mustBeStringArray: (value: string[], path: string[]) => {
|
|
11
|
-
code: ParseError;
|
|
12
|
-
path: string[];
|
|
13
|
-
}[];
|
|
14
|
-
export declare const mustBeBoolean: (value: string, path: string[]) => {
|
|
15
|
-
code: ParseError;
|
|
16
|
-
path: string[];
|
|
17
|
-
}[];
|
|
18
|
-
export declare const mustBeNumber: (value: string, path: string[]) => {
|
|
19
|
-
code: ParseError;
|
|
20
|
-
path: string[];
|
|
21
|
-
}[];
|
|
22
|
-
export declare const mustBeBidirectional: (value: any, path: string[]) => {
|
|
23
|
-
code: ParseError;
|
|
24
|
-
path: string[];
|
|
25
|
-
}[];
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { ParseError } from '../error.js';
|
|
2
|
-
export const mustBeString = (value, path) => typeof value === 'string'
|
|
3
|
-
? []
|
|
4
|
-
: [
|
|
5
|
-
{
|
|
6
|
-
code: ParseError.incorrectFormat,
|
|
7
|
-
path,
|
|
8
|
-
},
|
|
9
|
-
];
|
|
10
|
-
export const mustBeArray = (value, path) => Array.isArray(value)
|
|
11
|
-
? []
|
|
12
|
-
: [
|
|
13
|
-
{
|
|
14
|
-
code: ParseError.incorrectFormat,
|
|
15
|
-
path,
|
|
16
|
-
},
|
|
17
|
-
];
|
|
18
|
-
export const mustBeStringArray = (value, path) => Array.isArray(value) && value.every((i) => typeof i === 'string')
|
|
19
|
-
? []
|
|
20
|
-
: [
|
|
21
|
-
{
|
|
22
|
-
code: ParseError.incorrectFormat,
|
|
23
|
-
path,
|
|
24
|
-
},
|
|
25
|
-
];
|
|
26
|
-
export const mustBeBoolean = (value, path) => typeof value === 'boolean'
|
|
27
|
-
? []
|
|
28
|
-
: [
|
|
29
|
-
{
|
|
30
|
-
code: ParseError.incorrectFormat,
|
|
31
|
-
path,
|
|
32
|
-
},
|
|
33
|
-
];
|
|
34
|
-
export const mustBeNumber = (value, path) => typeof value === 'number'
|
|
35
|
-
? []
|
|
36
|
-
: [
|
|
37
|
-
{
|
|
38
|
-
code: ParseError.incorrectFormat,
|
|
39
|
-
path,
|
|
40
|
-
},
|
|
41
|
-
];
|
|
42
|
-
export const mustBeBidirectional = (value, path) => {
|
|
43
|
-
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
44
|
-
return [
|
|
45
|
-
{
|
|
46
|
-
code: ParseError.incorrectFormat,
|
|
47
|
-
path,
|
|
48
|
-
},
|
|
49
|
-
];
|
|
50
|
-
}
|
|
51
|
-
return value.hasOwnProperty('fromField') &&
|
|
52
|
-
typeof value.fromField === 'string'
|
|
53
|
-
? []
|
|
54
|
-
: [
|
|
55
|
-
{
|
|
56
|
-
code: ParseError.incorrectFormat,
|
|
57
|
-
path: path.concat('fromField'),
|
|
58
|
-
},
|
|
59
|
-
];
|
|
60
|
-
};
|
|
61
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { BasedSchema } from '../types.js';
|
|
2
|
-
import { BasedSchemaType, BasedSchemaFields } from '../types.js';
|
|
3
|
-
import { ArgsOpts, Path, Opts, Stopped, Collect } from './types.js';
|
|
4
|
-
import { ParseError } from '../error.js';
|
|
5
|
-
export declare class ArgsClass<T, K extends keyof BasedSchemaFields = keyof BasedSchemaFields> {
|
|
6
|
-
errors: any[];
|
|
7
|
-
id: number;
|
|
8
|
-
prev: ArgsClass<T, K>;
|
|
9
|
-
root: ArgsClass<T, K>;
|
|
10
|
-
_opts: Opts<T>;
|
|
11
|
-
_target: T;
|
|
12
|
-
_stopObject?: boolean;
|
|
13
|
-
_schema: BasedSchema;
|
|
14
|
-
parseTopLevel?: boolean;
|
|
15
|
-
_collectOverride: Collect<T>;
|
|
16
|
-
fieldSchema?: BasedSchemaFields[K];
|
|
17
|
-
typeSchema?: BasedSchemaType;
|
|
18
|
-
path: Path;
|
|
19
|
-
skipCollection: boolean;
|
|
20
|
-
value: any;
|
|
21
|
-
stopped: Stopped | void;
|
|
22
|
-
fromBackTrack: any[];
|
|
23
|
-
collectedCommands: any[];
|
|
24
|
-
constructor(opts: ArgsOpts<T, K>, prev?: ArgsClass<T, K>);
|
|
25
|
-
get schema(): BasedSchema;
|
|
26
|
-
get key(): number | string;
|
|
27
|
-
get target(): T;
|
|
28
|
-
stop(onllyStopFieldSchemaParser?: boolean): void;
|
|
29
|
-
create(opts?: ArgsOpts<T>): ArgsClass<T>;
|
|
30
|
-
parse(opts?: ArgsOpts<T>): Promise<ArgsClass<T> | void>;
|
|
31
|
-
getTopPaths(): Path[];
|
|
32
|
-
getBackTrackTarget(): ArgsClass<T>;
|
|
33
|
-
getCollectTarget(): void;
|
|
34
|
-
collect(value?: any): void;
|
|
35
|
-
error(code: ParseError): void;
|
|
36
|
-
}
|