@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
|
@@ -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
|
package/dist/test/compat.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
2
|
// TODO: maybe nice to use for validate import { newSchemas } from './data/newSchemas.js'
|
|
3
3
|
import { oldSchemas } from './data/oldSchemas.js';
|
|
4
|
-
import { convertNewToOld, convertOldToNew } from '../src/index.js';
|
|
4
|
+
import { convertNewToOld, convertOldToNew, validateSchema, } from '../src/index.js';
|
|
5
5
|
test('old schema compat mode', async (t) => {
|
|
6
|
-
for (let i = 0; i < oldSchemas.length; i++) {
|
|
6
|
+
for (let i = 0; i < oldSchemas.length - 1; i++) {
|
|
7
|
+
// for (let i = 0; i < 1; i++) {
|
|
7
8
|
const oldSchema = oldSchemas[i];
|
|
8
|
-
|
|
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}`);
|
|
9
13
|
}
|
|
10
14
|
});
|
|
11
15
|
//# sourceMappingURL=compat.js.map
|