@based/schema 2.5.3 → 2.7.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/dist/src/compat/Untitled-1.d.ts +3 -0
- package/dist/src/compat/Untitled-1.js +205 -0
- package/dist/src/compat/index.d.ts +2 -0
- package/dist/src/compat/index.js +3 -0
- package/dist/src/compat/newToOld.d.ts +3 -0
- package/dist/src/compat/newToOld.js +213 -0
- package/dist/src/compat/oldSchemaType.d.ts +69 -0
- package/dist/src/compat/oldSchemaType.js +2 -0
- package/dist/src/compat/oldToNew.d.ts +3 -0
- package/dist/src/compat/oldToNew.js +210 -0
- package/dist/src/display/number.d.ts +2 -1
- package/dist/src/display/number.js +10 -0
- package/dist/src/display/string.d.ts +3 -1
- package/dist/src/display/string.js +5 -0
- package/dist/src/display/timestamp.d.ts +3 -1
- package/dist/src/display/timestamp.js +9 -0
- package/dist/src/error.d.ts +3 -1
- package/dist/src/error.js +2 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +2 -1
- package/dist/src/types.d.ts +4 -3
- package/dist/src/types.js +74 -0
- package/dist/src/validateSchema/basedSchemaTypeValidator.d.ts +3 -0
- package/dist/src/validateSchema/basedSchemaTypeValidator.js +45 -0
- package/dist/src/validateSchema/fieldValidators.d.ts +27 -0
- package/dist/src/validateSchema/fieldValidators.js +352 -0
- package/dist/src/validateSchema/index.d.ts +17 -0
- package/dist/src/validateSchema/index.js +109 -0
- package/dist/src/validateSchema/utils.d.ts +21 -0
- package/dist/src/validateSchema/utils.js +53 -0
- package/dist/test/compat.js +15 -0
- package/dist/test/data/newSchemas.d.ts +2 -0
- package/dist/test/data/newSchemas.js +254 -0
- package/dist/test/data/oldSchemas.d.ts +2 -0
- package/dist/test/data/oldSchemas.js +5058 -0
- package/dist/test/validateSchema/basic.d.ts +1 -0
- package/dist/test/validateSchema/basic.js +94 -0
- package/dist/test/validateSchema/fields.d.ts +1 -0
- package/dist/test/validateSchema/fields.js +366 -0
- package/dist/test/validateSchema/languages.d.ts +1 -0
- package/dist/test/validateSchema/languages.js +124 -0
- package/package.json +4 -1
- package/dist/src/validateSchema.d.ts +0 -4
- package/dist/src/validateSchema.js +0 -35
- package/dist/test/validateSchema.js +0 -38
- /package/dist/test/{validateSchema.d.ts → compat.d.ts} +0 -0
|
@@ -0,0 +1,352 @@
|
|
|
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 { 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.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
|
+
default:
|
|
79
|
+
validator = basedSchemaFieldSharedValidator;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
return validate(validator, value, path, newSchema, oldSchema);
|
|
83
|
+
};
|
|
84
|
+
export const mustBeFields = (value, path, newSchema, oldSchema) => {
|
|
85
|
+
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
86
|
+
return [
|
|
87
|
+
{
|
|
88
|
+
code: ParseError.incorrectFormat,
|
|
89
|
+
path,
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
const errors = [];
|
|
94
|
+
for (const key in value) {
|
|
95
|
+
if (value.hasOwnProperty(key)) {
|
|
96
|
+
errors.push(...mustBeField(value[key], path.concat(key), newSchema, oldSchema));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return errors;
|
|
100
|
+
};
|
|
101
|
+
export const basedSchemaFieldSharedValidator = {
|
|
102
|
+
type: {
|
|
103
|
+
validator: (value, path) => basedSchemaFieldTypes.includes(value)
|
|
104
|
+
? []
|
|
105
|
+
: [{ code: ParseError.incorrectFieldType, path }],
|
|
106
|
+
},
|
|
107
|
+
hooks: {
|
|
108
|
+
validator: (value, path) => {
|
|
109
|
+
if (typeof value !== 'object') {
|
|
110
|
+
return [{ code: ParseError.incorrectFormat, path }];
|
|
111
|
+
}
|
|
112
|
+
const items = Array.isArray(value) ? value : [{ ...value }];
|
|
113
|
+
const errors = [];
|
|
114
|
+
items.forEach((item, index) => {
|
|
115
|
+
if (item.hook) {
|
|
116
|
+
errors.push(...mustBeString(item.hook, path.concat(Array.isArray(value) ? String(index) : [], 'hook')));
|
|
117
|
+
}
|
|
118
|
+
if (item.interval) {
|
|
119
|
+
errors.push(...mustBeNumber(item.interval, path.concat(Array.isArray(value) ? String(index) : [], 'interval')));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return errors;
|
|
123
|
+
},
|
|
124
|
+
optional: true,
|
|
125
|
+
},
|
|
126
|
+
$id: {
|
|
127
|
+
validator: mustBeString,
|
|
128
|
+
optional: true,
|
|
129
|
+
},
|
|
130
|
+
isRequired: {
|
|
131
|
+
validator: mustBeBoolean,
|
|
132
|
+
optional: true,
|
|
133
|
+
},
|
|
134
|
+
$schema: {
|
|
135
|
+
validator: mustBeString,
|
|
136
|
+
optional: true,
|
|
137
|
+
},
|
|
138
|
+
title: {
|
|
139
|
+
validator: mustBeString,
|
|
140
|
+
optional: true,
|
|
141
|
+
},
|
|
142
|
+
description: {
|
|
143
|
+
validator: mustBeString,
|
|
144
|
+
optional: true,
|
|
145
|
+
},
|
|
146
|
+
index: {
|
|
147
|
+
validator: mustBeNumber,
|
|
148
|
+
optional: true,
|
|
149
|
+
},
|
|
150
|
+
readOnly: {
|
|
151
|
+
validator: mustBeBoolean,
|
|
152
|
+
optional: true,
|
|
153
|
+
},
|
|
154
|
+
writeOnly: {
|
|
155
|
+
validator: mustBeBoolean,
|
|
156
|
+
optional: true,
|
|
157
|
+
},
|
|
158
|
+
$comment: {
|
|
159
|
+
validator: mustBeString,
|
|
160
|
+
optional: true,
|
|
161
|
+
},
|
|
162
|
+
examples: { optional: true },
|
|
163
|
+
default: { optional: true },
|
|
164
|
+
customValidator: { optional: true },
|
|
165
|
+
$defs: { optional: true },
|
|
166
|
+
$delete: {
|
|
167
|
+
validator: mustBeBoolean,
|
|
168
|
+
optional: true,
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
export const basedSchemaStringSharedValidator = {
|
|
172
|
+
minLength: {
|
|
173
|
+
validator: mustBeNumber,
|
|
174
|
+
optional: true,
|
|
175
|
+
},
|
|
176
|
+
maxLength: {
|
|
177
|
+
validator: mustBeNumber,
|
|
178
|
+
optional: true,
|
|
179
|
+
},
|
|
180
|
+
contentMediaEncoding: {
|
|
181
|
+
validator: mustBeString,
|
|
182
|
+
optional: true,
|
|
183
|
+
},
|
|
184
|
+
contentMediaType: {
|
|
185
|
+
validator: (value, path) => /^\w+\/\w+$/.test(value)
|
|
186
|
+
? []
|
|
187
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
188
|
+
optional: true,
|
|
189
|
+
},
|
|
190
|
+
pattern: {
|
|
191
|
+
validator: mustBeString,
|
|
192
|
+
optional: true,
|
|
193
|
+
},
|
|
194
|
+
format: {
|
|
195
|
+
validator: (value, path) => basedSchemaStringFormatValues.includes(value)
|
|
196
|
+
? []
|
|
197
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
198
|
+
optional: true,
|
|
199
|
+
},
|
|
200
|
+
display: {
|
|
201
|
+
validator: (value, path) => basedSchemaDisplayFormats.includes(value)
|
|
202
|
+
? []
|
|
203
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
204
|
+
optional: true,
|
|
205
|
+
},
|
|
206
|
+
multiline: {
|
|
207
|
+
validator: mustBeBoolean,
|
|
208
|
+
optional: true,
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
export const basedSchemaStringValidator = {
|
|
212
|
+
...basedSchemaFieldSharedValidator,
|
|
213
|
+
...basedSchemaStringSharedValidator,
|
|
214
|
+
};
|
|
215
|
+
export const basedSchemaFieldEnumValidator = {
|
|
216
|
+
...basedSchemaFieldSharedValidator,
|
|
217
|
+
enum: {},
|
|
218
|
+
};
|
|
219
|
+
export const basedSchemaFieldCardinalityValidator = {
|
|
220
|
+
...basedSchemaFieldSharedValidator,
|
|
221
|
+
};
|
|
222
|
+
export const basedSchemaNumberDefaultsValidator = {
|
|
223
|
+
multipleOf: {
|
|
224
|
+
validator: mustBeNumber,
|
|
225
|
+
optional: true,
|
|
226
|
+
},
|
|
227
|
+
minimum: {
|
|
228
|
+
validator: mustBeNumber,
|
|
229
|
+
optional: true,
|
|
230
|
+
},
|
|
231
|
+
maximum: {
|
|
232
|
+
validator: mustBeNumber,
|
|
233
|
+
optional: true,
|
|
234
|
+
},
|
|
235
|
+
exclusiveMaximum: {
|
|
236
|
+
validator: mustBeBoolean,
|
|
237
|
+
optional: true,
|
|
238
|
+
},
|
|
239
|
+
exclusiveMinimum: {
|
|
240
|
+
validator: mustBeBoolean,
|
|
241
|
+
optional: true,
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
export const basedSchemaFieldNumberValidator = {
|
|
245
|
+
...basedSchemaFieldSharedValidator,
|
|
246
|
+
...basedSchemaNumberDefaultsValidator,
|
|
247
|
+
display: {
|
|
248
|
+
validator: (value, path) => basedSchemaNumberFormats.includes(value) || /^round-\d+$/.test(value)
|
|
249
|
+
? []
|
|
250
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
251
|
+
optional: true,
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
export const basedSchemaFieldIntegerValidator = {
|
|
255
|
+
...basedSchemaFieldSharedValidator,
|
|
256
|
+
...basedSchemaNumberDefaultsValidator,
|
|
257
|
+
display: {
|
|
258
|
+
validator: (value, path) => basedSchemaNumberFormats.includes(value) || /^round-\d+$/.test(value)
|
|
259
|
+
? []
|
|
260
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
261
|
+
optional: true,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
export const basedSchemaFieldTimeStampValidator = {
|
|
265
|
+
...basedSchemaFieldSharedValidator,
|
|
266
|
+
...basedSchemaNumberDefaultsValidator,
|
|
267
|
+
display: {
|
|
268
|
+
validator: (value, path) => basedSchemaDateFormats.includes(value)
|
|
269
|
+
? []
|
|
270
|
+
: [{ code: ParseError.incorrectFormat, path }],
|
|
271
|
+
optional: true,
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
export const basedSchemaFieldBooleanValidator = {
|
|
275
|
+
...basedSchemaFieldSharedValidator,
|
|
276
|
+
};
|
|
277
|
+
export const basedSchemaFieldJSONValidator = {
|
|
278
|
+
...basedSchemaFieldSharedValidator,
|
|
279
|
+
format: {
|
|
280
|
+
validator: (value, path) => value === 'rich-text' ? [] : [{ code: ParseError.incorrectFormat, path }],
|
|
281
|
+
optional: true,
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
export const basedSchemaFieldAnyValidator = {
|
|
285
|
+
...basedSchemaFieldSharedValidator,
|
|
286
|
+
};
|
|
287
|
+
export const basedSchemaFieldTextValidator = {
|
|
288
|
+
...basedSchemaFieldSharedValidator,
|
|
289
|
+
...basedSchemaStringSharedValidator,
|
|
290
|
+
required: {
|
|
291
|
+
validator: (value, path) => Array.isArray(value) &&
|
|
292
|
+
value.every((i) => Object.keys(languages).includes(i))
|
|
293
|
+
? []
|
|
294
|
+
: [{ code: ParseError.languageNotSupported, path }],
|
|
295
|
+
optional: true,
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
export const basedSchemaFieldObjectValidator = {
|
|
299
|
+
...basedSchemaFieldSharedValidator,
|
|
300
|
+
properties: {
|
|
301
|
+
validator: mustBeFields,
|
|
302
|
+
},
|
|
303
|
+
required: {
|
|
304
|
+
validator: mustBeStringArray,
|
|
305
|
+
optional: true,
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
export const basedSchemaFieldRecordValidator = {
|
|
309
|
+
...basedSchemaFieldSharedValidator,
|
|
310
|
+
values: {
|
|
311
|
+
validator: mustBeField,
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
export const basedSchemaFieldArrayValidator = {
|
|
315
|
+
...basedSchemaFieldSharedValidator,
|
|
316
|
+
values: {
|
|
317
|
+
validator: mustBeField,
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
export const basedSchemaFieldSetValidator = {
|
|
321
|
+
...basedSchemaFieldSharedValidator,
|
|
322
|
+
items: {
|
|
323
|
+
validator: (value, path, newSchema, oldSchema) => mustBeField(value, path, newSchema, oldSchema, { limitTo: 'primitives' }),
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
export const basedSchemaFieldReferenceValidator = {
|
|
327
|
+
...basedSchemaFieldSharedValidator,
|
|
328
|
+
bidirectional: {
|
|
329
|
+
validator: mustBeBidirectional,
|
|
330
|
+
optional: true,
|
|
331
|
+
},
|
|
332
|
+
allowedTypes: {
|
|
333
|
+
// TODO: validator
|
|
334
|
+
optional: true,
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
export const basedSchemaFieldReferencesValidator = {
|
|
338
|
+
...basedSchemaFieldSharedValidator,
|
|
339
|
+
bidirectional: {
|
|
340
|
+
validator: mustBeBidirectional,
|
|
341
|
+
optional: true,
|
|
342
|
+
},
|
|
343
|
+
allowedTypes: {
|
|
344
|
+
// TODO: validator
|
|
345
|
+
optional: true,
|
|
346
|
+
},
|
|
347
|
+
sortable: {
|
|
348
|
+
validator: mustBeBoolean,
|
|
349
|
+
optional: true,
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
//# sourceMappingURL=fieldValidators.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}>;
|
|
@@ -0,0 +1,109 @@
|
|
|
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
|
|
@@ -0,0 +1,21 @@
|
|
|
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 mustBeStringArray: (value: string[], path: string[]) => {
|
|
7
|
+
code: ParseError;
|
|
8
|
+
path: string[];
|
|
9
|
+
}[];
|
|
10
|
+
export declare const mustBeBoolean: (value: string, path: string[]) => {
|
|
11
|
+
code: ParseError;
|
|
12
|
+
path: string[];
|
|
13
|
+
}[];
|
|
14
|
+
export declare const mustBeNumber: (value: string, path: string[]) => {
|
|
15
|
+
code: ParseError;
|
|
16
|
+
path: string[];
|
|
17
|
+
}[];
|
|
18
|
+
export declare const mustBeBidirectional: (value: any, path: string[]) => {
|
|
19
|
+
code: ParseError;
|
|
20
|
+
path: string[];
|
|
21
|
+
}[];
|
|
@@ -0,0 +1,53 @@
|
|
|
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 mustBeStringArray = (value, path) => Array.isArray(value) && value.every((i) => typeof i === 'string')
|
|
11
|
+
? []
|
|
12
|
+
: [
|
|
13
|
+
{
|
|
14
|
+
code: ParseError.incorrectFormat,
|
|
15
|
+
path,
|
|
16
|
+
},
|
|
17
|
+
];
|
|
18
|
+
export const mustBeBoolean = (value, path) => typeof value === 'boolean'
|
|
19
|
+
? []
|
|
20
|
+
: [
|
|
21
|
+
{
|
|
22
|
+
code: ParseError.incorrectFormat,
|
|
23
|
+
path,
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
export const mustBeNumber = (value, path) => typeof value === 'number'
|
|
27
|
+
? []
|
|
28
|
+
: [
|
|
29
|
+
{
|
|
30
|
+
code: ParseError.incorrectFormat,
|
|
31
|
+
path,
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
export const mustBeBidirectional = (value, path) => {
|
|
35
|
+
if (!(typeof value === 'object' && !Array.isArray(value))) {
|
|
36
|
+
return [
|
|
37
|
+
{
|
|
38
|
+
code: ParseError.incorrectFormat,
|
|
39
|
+
path,
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
return value.hasOwnProperty('fromField') &&
|
|
44
|
+
typeof value.fromField === 'string'
|
|
45
|
+
? []
|
|
46
|
+
: [
|
|
47
|
+
{
|
|
48
|
+
code: ParseError.incorrectFormat,
|
|
49
|
+
path: path.concat('fromField'),
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
// TODO: maybe nice to use for validate import { newSchemas } from './data/newSchemas.js'
|
|
3
|
+
import { oldSchemas } from './data/oldSchemas.js';
|
|
4
|
+
import { convertNewToOld, convertOldToNew, validateSchema, } from '../src/index.js';
|
|
5
|
+
test('old schema compat mode', async (t) => {
|
|
6
|
+
for (let i = 0; i < oldSchemas.length - 1; i++) {
|
|
7
|
+
// for (let i = 0; i < 1; i++) {
|
|
8
|
+
const oldSchema = oldSchemas[i];
|
|
9
|
+
const newSchema = convertOldToNew(oldSchema);
|
|
10
|
+
const validation = await validateSchema(newSchema);
|
|
11
|
+
t.true(validation.valid);
|
|
12
|
+
t.deepEqual(oldSchema, convertNewToOld(newSchema), `Schema conversion oldSchemas index ${i}`);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=compat.js.map
|