@cleverbrush/schema 0.0.17 → 1.0.0-beta.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/README.md +213 -163
- package/package.json +3 -3
- package/src/builders/ArraySchemaBuilder.test.ts +270 -0
- package/src/builders/ArraySchemaBuilder.ts +381 -0
- package/src/builders/BooleanSchemaBuilder.test.ts +196 -0
- package/src/builders/BooleanSchemaBuilder.ts +155 -0
- package/src/builders/FunctionSchemaBuilder.test.ts +134 -0
- package/src/builders/FunctionSchemaBuilder.ts +109 -0
- package/src/builders/NumberSchemaBuilder.test.ts +493 -0
- package/src/builders/NumberSchemaBuilder.ts +789 -0
- package/src/builders/ObjectSchemaBuilder.test.ts +657 -0
- package/src/builders/ObjectSchemaBuilder.ts +794 -0
- package/src/builders/SchemaBuilder.test.ts +73 -0
- package/src/builders/SchemaBuilder.ts +135 -0
- package/src/builders/StringSchemaBuilder.test.ts +318 -0
- package/src/builders/StringSchemaBuilder.ts +392 -0
- package/src/builders/UnionSchemaBuilder.test.ts +162 -0
- package/src/builders/UnionSchemaBuilder.ts +154 -0
- package/src/defaultSchemas.ts +44 -0
- package/src/index.ts +58 -190
- package/src/schema.ts +827 -0
- package/src/schemaRegistry.builders.test.ts +1393 -0
- package/src/schemaRegistry.test.ts +118 -0
- package/src/schemaRegistry.ts +461 -0
- package/src/validators/validateArray.ts +4 -7
- package/src/validators/validateBoolean.ts +2 -11
- package/src/validators/validateFunction.ts +25 -0
- package/src/validators/validateNumber.ts +2 -7
- package/src/validators/validateObject.ts +32 -21
- package/src/validators/validateString.ts +2 -7
- package/src/validators/validateUnion.ts +36 -0
- package/dist/index.d.ts +0 -94
- package/dist/index.js +0 -9
- package/dist/schemaValidator.d.ts +0 -16
- package/dist/schemaValidator.js +0 -234
- package/dist/validators/validateArray.d.ts +0 -2
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -33
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -2
- package/dist/validators/validateObject.js +0 -73
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/src/schemaValidator.test.ts +0 -1656
- package/src/schemaValidator.ts +0 -367
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateNumber = void 0;
|
|
4
|
-
const validateNumber = async (obj, schema, validator) => {
|
|
5
|
-
if (typeof obj === 'undefined' &&
|
|
6
|
-
typeof schema === 'object' &&
|
|
7
|
-
schema.isRequired === false) {
|
|
8
|
-
return {
|
|
9
|
-
valid: true
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
if (typeof obj !== 'number')
|
|
13
|
-
return {
|
|
14
|
-
valid: false,
|
|
15
|
-
errors: [`expected type number, but saw ${typeof obj}`]
|
|
16
|
-
};
|
|
17
|
-
if (typeof schema === 'number') {
|
|
18
|
-
return obj === schema
|
|
19
|
-
? { valid: true }
|
|
20
|
-
: {
|
|
21
|
-
valid: false,
|
|
22
|
-
errors: [`should be equal to ${schema}`]
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const num = obj;
|
|
26
|
-
if (schema.ensureNotNaN && Number.isNaN(num)) {
|
|
27
|
-
return {
|
|
28
|
-
valid: false,
|
|
29
|
-
errors: ['is not expected to be NaN']
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
if (schema.ensureIsFinite &&
|
|
33
|
-
!Number.isFinite(num) &&
|
|
34
|
-
schema.ensureNotNaN &&
|
|
35
|
-
!Number.isNaN(num)) {
|
|
36
|
-
return {
|
|
37
|
-
valid: false,
|
|
38
|
-
errors: ['is expected to be a finite number']
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
if (typeof schema.equals !== 'undefined' && num !== schema.equals) {
|
|
42
|
-
return {
|
|
43
|
-
valid: false,
|
|
44
|
-
errors: [`expected to be equal to ${schema.equals}`]
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
if (typeof schema.min !== 'undefined') {
|
|
48
|
-
if (typeof schema.min !== 'number')
|
|
49
|
-
throw new Error('min constraint should be a number');
|
|
50
|
-
if (num < schema.min)
|
|
51
|
-
return {
|
|
52
|
-
valid: false,
|
|
53
|
-
errors: [`expected to be at least ${schema.min}`]
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
if (typeof schema.max !== 'undefined') {
|
|
57
|
-
if (typeof schema.max !== 'number')
|
|
58
|
-
throw new Error('max constraint should be a number');
|
|
59
|
-
if (num > schema.max)
|
|
60
|
-
return {
|
|
61
|
-
valid: false,
|
|
62
|
-
errors: [`expected to be no more than ${schema.max}`]
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
valid: true
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
exports.validateNumber = validateNumber;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateObject = void 0;
|
|
4
|
-
const validateObject = async (obj, schema, validator) => {
|
|
5
|
-
if (typeof obj === 'undefined' &&
|
|
6
|
-
typeof schema === 'object' &&
|
|
7
|
-
schema.isRequired === false) {
|
|
8
|
-
return {
|
|
9
|
-
valid: true
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
if (typeof obj !== 'object') {
|
|
13
|
-
return {
|
|
14
|
-
valid: false,
|
|
15
|
-
errors: [
|
|
16
|
-
`expected to have type='object', but saw '${typeof obj}' instead`
|
|
17
|
-
]
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
if (typeof schema.preprocessors === 'object' && schema.preprocessors) {
|
|
21
|
-
await Promise.all(Object.keys(schema.preprocessors).map(async (key) => {
|
|
22
|
-
if (key === '*')
|
|
23
|
-
return;
|
|
24
|
-
if (typeof schema.preprocessors[key] === 'function') {
|
|
25
|
-
obj[key] = await Promise.resolve(schema.preprocessors[key](obj[key]));
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
const preprocessor = validator.preprocessors.get(schema.preprocessors[key]);
|
|
29
|
-
if (typeof preprocessor !== 'function') {
|
|
30
|
-
throw new Error(`preprocessor '${schema.preprocessors[key]}' is unknown`);
|
|
31
|
-
}
|
|
32
|
-
obj[key] = await Promise.resolve(preprocessor(obj[key]));
|
|
33
|
-
}
|
|
34
|
-
}));
|
|
35
|
-
if ('*' in schema.preprocessors) {
|
|
36
|
-
const objectPreprocessor = schema.preprocessors['*'];
|
|
37
|
-
if (typeof objectPreprocessor === 'function') {
|
|
38
|
-
await Promise.resolve(objectPreprocessor(obj));
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
const preprocessor = validator.preprocessors.get(objectPreprocessor);
|
|
42
|
-
if (typeof preprocessor !== 'function') {
|
|
43
|
-
throw new Error(`preprocessor '${objectPreprocessor}' is unknown`);
|
|
44
|
-
}
|
|
45
|
-
await Promise.resolve(preprocessor(obj));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (typeof schema.properties === 'object' && schema.properties) {
|
|
50
|
-
const errors = (await Promise.all(Object.entries(schema.properties).map(async ([name, schema]) => {
|
|
51
|
-
const result = await validator.validate(schema, obj[name]);
|
|
52
|
-
if (result.valid)
|
|
53
|
-
return result;
|
|
54
|
-
return {
|
|
55
|
-
valid: false,
|
|
56
|
-
errors: (result.errors || []).map((e) => `->${name} ${e}`)
|
|
57
|
-
};
|
|
58
|
-
})))
|
|
59
|
-
.filter((r) => !r.valid)
|
|
60
|
-
.map((r) => r.errors)
|
|
61
|
-
.flat(Infinity);
|
|
62
|
-
if (errors.length) {
|
|
63
|
-
return {
|
|
64
|
-
valid: false,
|
|
65
|
-
errors
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return {
|
|
70
|
-
valid: true
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
exports.validateObject = validateObject;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateString = void 0;
|
|
4
|
-
const validateString = async (obj, schema, validator) => {
|
|
5
|
-
if (typeof obj === 'undefined' &&
|
|
6
|
-
typeof schema === 'object' &&
|
|
7
|
-
schema.isRequired === false) {
|
|
8
|
-
return {
|
|
9
|
-
valid: true
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
if (typeof obj !== 'string')
|
|
13
|
-
return {
|
|
14
|
-
valid: false,
|
|
15
|
-
errors: [`expected type string, but saw ${typeof obj}`]
|
|
16
|
-
};
|
|
17
|
-
if (typeof schema === 'string') {
|
|
18
|
-
return obj === schema
|
|
19
|
-
? { valid: true }
|
|
20
|
-
: {
|
|
21
|
-
valid: false,
|
|
22
|
-
errors: [`should be equal to ${schema}`]
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const str = obj;
|
|
26
|
-
if (typeof schema.equals === 'string' && str !== schema.equals) {
|
|
27
|
-
return {
|
|
28
|
-
valid: false,
|
|
29
|
-
errors: [
|
|
30
|
-
`expected to be equal to '${schema.equals}' but saw '${str}'`
|
|
31
|
-
]
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
if (typeof schema.minLength === 'number' && str.length < schema.minLength) {
|
|
35
|
-
return {
|
|
36
|
-
valid: false,
|
|
37
|
-
errors: [`expected to be at least ${schema.minLength} chars long`]
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
if (typeof schema.maxLength === 'number' && str.length > schema.maxLength) {
|
|
41
|
-
return {
|
|
42
|
-
valid: false,
|
|
43
|
-
errors: [`expected to be at most ${schema.maxLength} chars long`]
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
valid: true
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
exports.validateString = validateString;
|