@based/schema 2.6.0 → 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/newToOld.js +165 -28
- 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 +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 +7 -3
- 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 +366 -0
- package/dist/test/validateSchema/languages.d.ts +1 -0
- package/dist/test/validateSchema/languages.js +124 -0
- package/package.json +1 -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 → validateSchema/basic.d.ts} +0 -0
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare const basedSchemaDateFormats: readonly [any, "date", "date-time", "date-time-text", "human", "time", "time-precise"];
|
|
2
|
+
export type BasedSchemaDateFormat = (typeof basedSchemaDateFormats)[number];
|
|
3
|
+
export type DateFormat = BasedSchemaDateFormat;
|
|
2
4
|
declare const _default: (nr: number | string, format?: DateFormat) => string | number;
|
|
3
5
|
export default _default;
|
package/dist/src/error.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ export declare enum ParseError {
|
|
|
15
15
|
'noLanguageFound' = 13,
|
|
16
16
|
'cannotDeleteNodeFromModify' = 14,
|
|
17
17
|
'nestedModifyObjectNotAllowed' = 15,
|
|
18
|
-
'infinityNotSupported' = 16
|
|
18
|
+
'infinityNotSupported' = 16,
|
|
19
|
+
'invalidSchemaFormat' = 17,
|
|
20
|
+
'invalidProperty' = 18
|
|
19
21
|
}
|
package/dist/src/error.js
CHANGED
|
@@ -17,5 +17,7 @@ export var ParseError;
|
|
|
17
17
|
ParseError[ParseError["cannotDeleteNodeFromModify"] = 14] = "cannotDeleteNodeFromModify";
|
|
18
18
|
ParseError[ParseError["nestedModifyObjectNotAllowed"] = 15] = "nestedModifyObjectNotAllowed";
|
|
19
19
|
ParseError[ParseError["infinityNotSupported"] = 16] = "infinityNotSupported";
|
|
20
|
+
ParseError[ParseError["invalidSchemaFormat"] = 17] = "invalidSchemaFormat";
|
|
21
|
+
ParseError[ParseError["invalidProperty"] = 18] = "invalidProperty";
|
|
20
22
|
})(ParseError || (ParseError = {}));
|
|
21
23
|
//# sourceMappingURL=error.js.map
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
package/dist/src/types.d.ts
CHANGED
|
@@ -43,23 +43,25 @@ export type BasedSchemaFieldShared = {
|
|
|
43
43
|
};
|
|
44
44
|
$delete?: boolean;
|
|
45
45
|
};
|
|
46
|
+
export declare const basedSchemaStringFormatValues: readonly ["email", "URL", "MACAddress", "IP", "IPRange", "FQDN", "IBAN", "BIC", "alpha", "alphaLocales", "alphanumeric", "alphanumericLocales", "passportNumber", "port", "lowercase", "uppercase", "ascii", "semVer", "surrogatePair", "IMEI", "hexadecimal", "octal", "hexColor", "rgbColor", "HSL", "ISRC", "MD5", "JWT", "UUID", "luhnNumber", "creditCard", "identityCard", "EAN", "ISIN", "ISBN", "ISSN", "mobilePhone", "mobilePhoneLocales", "postalCode", "postalCodeLocales", "ethereumAddress", "currency", "btcAddress", "ISO6391", "ISO8601", "RFC3339", "ISO31661Alpha2", "ISO31661Alpha3", "ISO4217", "base32", "base58", "base64", "dataURI", "magnetURI", "mimeType", "latLong", "slug", "strongPassword", "taxID", "licensePlate", "VAT", "code", "typescript", "javascript", "python", "rust", "css", "html", "json", "markdown", "clike", "basedId"];
|
|
46
47
|
export type BasedSchemaStringShared = {
|
|
47
48
|
minLength?: number;
|
|
48
49
|
maxLength?: number;
|
|
49
50
|
contentMediaEncoding?: string;
|
|
50
51
|
contentMediaType?: BasedSchemaContentMediaType;
|
|
51
52
|
pattern?: BasedSchemaPattern;
|
|
52
|
-
format?:
|
|
53
|
+
format?: (typeof basedSchemaStringFormatValues)[number];
|
|
53
54
|
display?: StringFormat;
|
|
54
55
|
multiline?: boolean;
|
|
55
56
|
};
|
|
56
|
-
type
|
|
57
|
+
export type BasedSchemaNumberDefaults = {
|
|
57
58
|
multipleOf?: number;
|
|
58
59
|
minimum?: number;
|
|
59
60
|
maximum?: number;
|
|
60
61
|
exclusiveMaximum?: boolean;
|
|
61
62
|
exclusiveMinimum?: boolean;
|
|
62
63
|
};
|
|
64
|
+
export type NumberDefaults = BasedSchemaNumberDefaults;
|
|
63
65
|
export type BasedNumberDisplay = NumberFormat;
|
|
64
66
|
export type BasedTimestampDisplay = DateFormat;
|
|
65
67
|
export type BasedSchemaFieldString = {
|
|
@@ -209,4 +211,3 @@ export type BasedSchemaCollectProps = ArgsClass<BasedSetTarget> & {
|
|
|
209
211
|
typeSchema: BasedSchemaType;
|
|
210
212
|
};
|
|
211
213
|
};
|
|
212
|
-
export {};
|
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,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
|
+
}>;
|