@based/schema 2.6.0 → 2.7.1
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/newToOld.d.ts +2 -2
- package/dist/src/compat/newToOld.js +172 -29
- package/dist/src/compat/oldToNew.d.ts +1 -1
- package/dist/src/compat/oldToNew.js +200 -25
- 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 +1 -1
- package/dist/src/index.js +1 -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 +360 -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 +25 -0
- package/dist/src/validateSchema/utils.js +61 -0
- package/dist/test/compat.js +19 -3
- package/dist/test/data/newSchemas.d.ts +2 -2
- package/dist/test/data/newSchemas.js +241 -0
- package/dist/test/data/oldSchemas.js +122 -122
- package/dist/test/validateSchema/basic.js +94 -0
- package/dist/test/validateSchema/fields.d.ts +1 -0
- package/dist/test/validateSchema/fields.js +436 -0
- package/dist/test/validateSchema/languages.d.ts +1 -0
- package/dist/test/validateSchema/languages.js +124 -0
- package/dist/test/validateSchema/realWorld.d.ts +1 -0
- package/dist/test/validateSchema/realWorld.js +13 -0
- package/package.json +4 -3
- 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 → validateSchema/basic.d.ts} +0 -0
package/dist/src/types.js
CHANGED
|
@@ -21,4 +21,78 @@ export const isCollection = (type) => {
|
|
|
21
21
|
return type === 'array' || type === 'object' || type === 'record';
|
|
22
22
|
};
|
|
23
23
|
export const languages = Object.keys(allLanguages);
|
|
24
|
+
export const basedSchemaStringFormatValues = [
|
|
25
|
+
'email',
|
|
26
|
+
'URL',
|
|
27
|
+
'MACAddress',
|
|
28
|
+
'IP',
|
|
29
|
+
'IPRange',
|
|
30
|
+
'FQDN',
|
|
31
|
+
'IBAN',
|
|
32
|
+
'BIC',
|
|
33
|
+
'alpha',
|
|
34
|
+
'alphaLocales',
|
|
35
|
+
'alphanumeric',
|
|
36
|
+
'alphanumericLocales',
|
|
37
|
+
'passportNumber',
|
|
38
|
+
'port',
|
|
39
|
+
'lowercase',
|
|
40
|
+
'uppercase',
|
|
41
|
+
'ascii',
|
|
42
|
+
'semVer',
|
|
43
|
+
'surrogatePair',
|
|
44
|
+
'IMEI',
|
|
45
|
+
'hexadecimal',
|
|
46
|
+
'octal',
|
|
47
|
+
'hexColor',
|
|
48
|
+
'rgbColor',
|
|
49
|
+
'HSL',
|
|
50
|
+
'ISRC',
|
|
51
|
+
'MD5',
|
|
52
|
+
'JWT',
|
|
53
|
+
'UUID',
|
|
54
|
+
'luhnNumber',
|
|
55
|
+
'creditCard',
|
|
56
|
+
'identityCard',
|
|
57
|
+
'EAN',
|
|
58
|
+
'ISIN',
|
|
59
|
+
'ISBN',
|
|
60
|
+
'ISSN',
|
|
61
|
+
'mobilePhone',
|
|
62
|
+
'mobilePhoneLocales',
|
|
63
|
+
'postalCode',
|
|
64
|
+
'postalCodeLocales',
|
|
65
|
+
'ethereumAddress',
|
|
66
|
+
'currency',
|
|
67
|
+
'btcAddress',
|
|
68
|
+
'ISO6391',
|
|
69
|
+
'ISO8601',
|
|
70
|
+
'RFC3339',
|
|
71
|
+
'ISO31661Alpha2',
|
|
72
|
+
'ISO31661Alpha3',
|
|
73
|
+
'ISO4217',
|
|
74
|
+
'base32',
|
|
75
|
+
'base58',
|
|
76
|
+
'base64',
|
|
77
|
+
'dataURI',
|
|
78
|
+
'magnetURI',
|
|
79
|
+
'mimeType',
|
|
80
|
+
'latLong',
|
|
81
|
+
'slug',
|
|
82
|
+
'strongPassword',
|
|
83
|
+
'taxID',
|
|
84
|
+
'licensePlate',
|
|
85
|
+
'VAT',
|
|
86
|
+
'code',
|
|
87
|
+
'typescript',
|
|
88
|
+
'javascript',
|
|
89
|
+
'python',
|
|
90
|
+
'rust',
|
|
91
|
+
'css',
|
|
92
|
+
'html',
|
|
93
|
+
'json',
|
|
94
|
+
'markdown',
|
|
95
|
+
'clike',
|
|
96
|
+
'basedId',
|
|
97
|
+
];
|
|
24
98
|
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { deepEqual } from '@saulx/utils';
|
|
2
|
+
import { ParseError } from '../error.js';
|
|
3
|
+
import { mustBeBoolean, mustBeString, mustBeStringArray } from './utils.js';
|
|
4
|
+
import { mustBeFields } from './fieldValidators.js';
|
|
5
|
+
export const basedSchemaTypeValidator = {
|
|
6
|
+
directory: {
|
|
7
|
+
validator: mustBeString,
|
|
8
|
+
optional: true,
|
|
9
|
+
},
|
|
10
|
+
fields: {
|
|
11
|
+
validator: mustBeFields,
|
|
12
|
+
},
|
|
13
|
+
title: {
|
|
14
|
+
validator: mustBeString,
|
|
15
|
+
optional: true,
|
|
16
|
+
},
|
|
17
|
+
description: {
|
|
18
|
+
validator: mustBeString,
|
|
19
|
+
optional: true,
|
|
20
|
+
},
|
|
21
|
+
prefix: {
|
|
22
|
+
validator: (value, path) => {
|
|
23
|
+
if (deepEqual(path, ['root', 'prefix'])) {
|
|
24
|
+
return value === 'ro'
|
|
25
|
+
? []
|
|
26
|
+
: [{ code: ParseError.incorrectFormat, path }];
|
|
27
|
+
}
|
|
28
|
+
return /^[a-z]{2}$/.test(value)
|
|
29
|
+
? []
|
|
30
|
+
: [{ code: ParseError.incorrectFormat, path }];
|
|
31
|
+
},
|
|
32
|
+
optional: true,
|
|
33
|
+
},
|
|
34
|
+
examples: { optional: true },
|
|
35
|
+
required: {
|
|
36
|
+
validator: mustBeStringArray,
|
|
37
|
+
optional: true,
|
|
38
|
+
},
|
|
39
|
+
$defs: { optional: true },
|
|
40
|
+
$delete: {
|
|
41
|
+
validator: mustBeBoolean,
|
|
42
|
+
optional: true,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=basedSchemaTypeValidator.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BasedSchema, BasedSchemaFieldAny, BasedSchemaFieldArray, BasedSchemaFieldBoolean, BasedSchemaFieldCardinality, BasedSchemaFieldEnum, BasedSchemaFieldInteger, BasedSchemaFieldJSON, BasedSchemaFieldNumber, BasedSchemaFieldObject, BasedSchemaFieldRecord, BasedSchemaFieldReference, BasedSchemaFieldReferences, BasedSchemaFieldSet, BasedSchemaFieldShared, BasedSchemaFieldString, BasedSchemaFieldText, BasedSchemaFieldTimeStamp, BasedSchemaNumberDefaults, BasedSchemaPartial, BasedSchemaStringShared } from '../types.js';
|
|
2
|
+
import { ValidateSchemaError, Validator } from './index.js';
|
|
3
|
+
type MustBeFieldOptions = {
|
|
4
|
+
limitTo?: 'primitives' | 'enumerables';
|
|
5
|
+
};
|
|
6
|
+
export declare const mustBeField: (value: any, path: string[], newSchema: BasedSchemaPartial, oldSchema: BasedSchema, options?: MustBeFieldOptions) => ValidateSchemaError[];
|
|
7
|
+
export declare const mustBeFields: (value: any, path: string[], newSchema: BasedSchemaPartial, oldSchema: BasedSchema) => ValidateSchemaError[];
|
|
8
|
+
export declare const basedSchemaFieldSharedValidator: Validator<BasedSchemaFieldShared>;
|
|
9
|
+
export declare const basedSchemaStringSharedValidator: Validator<BasedSchemaStringShared>;
|
|
10
|
+
export declare const basedSchemaStringValidator: Validator<BasedSchemaFieldString>;
|
|
11
|
+
export declare const basedSchemaFieldEnumValidator: Validator<BasedSchemaFieldEnum>;
|
|
12
|
+
export declare const basedSchemaFieldCardinalityValidator: Validator<BasedSchemaFieldCardinality>;
|
|
13
|
+
export declare const basedSchemaNumberDefaultsValidator: Validator<BasedSchemaNumberDefaults>;
|
|
14
|
+
export declare const basedSchemaFieldNumberValidator: Validator<BasedSchemaFieldNumber>;
|
|
15
|
+
export declare const basedSchemaFieldIntegerValidator: Validator<BasedSchemaFieldInteger>;
|
|
16
|
+
export declare const basedSchemaFieldTimeStampValidator: Validator<BasedSchemaFieldTimeStamp>;
|
|
17
|
+
export declare const basedSchemaFieldBooleanValidator: Validator<BasedSchemaFieldBoolean>;
|
|
18
|
+
export declare const basedSchemaFieldJSONValidator: Validator<BasedSchemaFieldJSON>;
|
|
19
|
+
export declare const basedSchemaFieldAnyValidator: Validator<BasedSchemaFieldAny>;
|
|
20
|
+
export declare const basedSchemaFieldTextValidator: Validator<BasedSchemaFieldText>;
|
|
21
|
+
export declare const basedSchemaFieldObjectValidator: Validator<BasedSchemaFieldObject>;
|
|
22
|
+
export declare const basedSchemaFieldRecordValidator: Validator<BasedSchemaFieldRecord>;
|
|
23
|
+
export declare const basedSchemaFieldArrayValidator: Validator<BasedSchemaFieldArray>;
|
|
24
|
+
export declare const basedSchemaFieldSetValidator: Validator<BasedSchemaFieldSet>;
|
|
25
|
+
export declare const basedSchemaFieldReferenceValidator: Validator<BasedSchemaFieldReference>;
|
|
26
|
+
export declare const basedSchemaFieldReferencesValidator: Validator<BasedSchemaFieldReferences>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,360 @@
|
|
|
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
|
+
values: {
|
|
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
|
+
bidirectional: {
|
|
337
|
+
validator: mustBeBidirectional,
|
|
338
|
+
optional: true,
|
|
339
|
+
},
|
|
340
|
+
allowedTypes: {
|
|
341
|
+
// TODO: validator
|
|
342
|
+
optional: true,
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
export const basedSchemaFieldReferencesValidator = {
|
|
346
|
+
...basedSchemaFieldSharedValidator,
|
|
347
|
+
bidirectional: {
|
|
348
|
+
validator: mustBeBidirectional,
|
|
349
|
+
optional: true,
|
|
350
|
+
},
|
|
351
|
+
allowedTypes: {
|
|
352
|
+
// TODO: validator
|
|
353
|
+
optional: true,
|
|
354
|
+
},
|
|
355
|
+
sortable: {
|
|
356
|
+
validator: mustBeBoolean,
|
|
357
|
+
optional: true,
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
//# 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,25 @@
|
|
|
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
|
+
}[];
|