@cleverbrush/schema 0.0.16 → 1.0.0-beta.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/README.md +224 -149
- package/dist/builders/AliasSchemaBuilder.d.ts +14 -0
- package/dist/builders/AliasSchemaBuilder.js +91 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +15 -0
- package/dist/builders/ArraySchemaBuilder.js +141 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +31 -0
- package/dist/builders/BooleanSchemaBuilder.js +90 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +25 -0
- package/dist/builders/FunctionSchemaBuilder.js +66 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +61 -0
- package/dist/builders/NumberSchemaBuilder.js +206 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +83 -0
- package/dist/builders/ObjectSchemaBuilder.js +344 -0
- package/dist/builders/SchemaBuilder.d.ts +36 -0
- package/dist/builders/SchemaBuilder.js +88 -0
- package/dist/builders/StringSchemaBuilder.d.ts +14 -0
- package/dist/builders/StringSchemaBuilder.js +140 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +10 -0
- package/dist/builders/UnionSchemaBuilder.js +100 -0
- package/dist/defaultSchemas.d.ts +4 -0
- package/dist/defaultSchemas.js +45 -0
- package/dist/index.d.ts +38 -90
- package/dist/index.js +30 -4
- package/dist/schema.d.ts +242 -0
- package/dist/schema.js +2 -0
- package/dist/schemaRegistry.d.ts +54 -0
- package/dist/schemaRegistry.js +301 -0
- package/dist/validators/validateArray.d.ts +3 -2
- package/dist/validators/validateBoolean.d.ts +2 -2
- package/dist/validators/validateBoolean.js +1 -4
- package/dist/validators/validateFunction.d.ts +2 -0
- package/dist/validators/validateFunction.js +21 -0
- package/dist/validators/validateNumber.d.ts +2 -2
- package/dist/validators/validateNumber.js +1 -1
- package/dist/validators/validateObject.d.ts +3 -2
- package/dist/validators/validateObject.js +33 -11
- package/dist/validators/validateString.d.ts +2 -2
- package/dist/validators/validateString.js +1 -1
- package/dist/validators/validateUnion.d.ts +3 -0
- package/dist/validators/validateUnion.js +30 -0
- package/package.json +3 -3
- package/src/builders/AliasSchemaBuilder.test.ts +124 -0
- package/src/builders/AliasSchemaBuilder.ts +250 -0
- 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 +159 -0
- package/src/defaultSchemas.ts +44 -0
- package/src/index.ts +70 -190
- package/src/schema.ts +922 -0
- package/src/schemaRegistry.builders.test.ts +1438 -0
- package/src/{schemaValidator.test.ts → schemaRegistry.test.ts} +561 -185
- package/src/schemaRegistry.ts +532 -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/schemaValidator.d.ts +0 -16
- package/dist/schemaValidator.js +0 -234
- package/src/schemaValidator.ts +0 -367
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateFunction = void 0;
|
|
4
|
+
const validateFunction = async (obj, schema) => {
|
|
5
|
+
if (typeof obj === 'undefined' &&
|
|
6
|
+
typeof schema === 'object' &&
|
|
7
|
+
schema.isRequired === false) {
|
|
8
|
+
return {
|
|
9
|
+
valid: true
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
if (typeof obj !== 'function')
|
|
13
|
+
return {
|
|
14
|
+
valid: false,
|
|
15
|
+
errors: [`expected type function, but saw ${typeof obj}`]
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
valid: true
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.validateFunction = validateFunction;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const validateNumber: (obj: any, schema:
|
|
1
|
+
import { NumberSchema, ValidationResult } from '../schema.js';
|
|
2
|
+
export declare const validateNumber: (obj: any, schema: NumberSchema) => Promise<ValidationResult>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateNumber = void 0;
|
|
4
|
-
const validateNumber = async (obj, schema
|
|
4
|
+
const validateNumber = async (obj, schema) => {
|
|
5
5
|
if (typeof obj === 'undefined' &&
|
|
6
6
|
typeof schema === 'object' &&
|
|
7
7
|
schema.isRequired === false) {
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { ObjectSchema, ValidationResult } from '../schema.js';
|
|
2
|
+
import SchemaRegistry from '../schemaRegistry.js';
|
|
3
|
+
export declare const validateObject: (obj: any, schema: ObjectSchema<any>, validator: SchemaRegistry<any>) => Promise<ValidationResult>;
|
|
@@ -22,14 +22,26 @@ const validateObject = async (obj, schema, validator) => {
|
|
|
22
22
|
if (key === '*')
|
|
23
23
|
return;
|
|
24
24
|
if (typeof schema.preprocessors[key] === 'function') {
|
|
25
|
-
|
|
25
|
+
const res = await Promise.resolve(schema.preprocessors[key](obj[key]));
|
|
26
|
+
if (typeof res !== 'undefined') {
|
|
27
|
+
obj[key] = res;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
delete obj[key];
|
|
31
|
+
}
|
|
26
32
|
}
|
|
27
33
|
else {
|
|
28
34
|
const preprocessor = validator.preprocessors.get(schema.preprocessors[key]);
|
|
29
35
|
if (typeof preprocessor !== 'function') {
|
|
30
36
|
throw new Error(`preprocessor '${schema.preprocessors[key]}' is unknown`);
|
|
31
37
|
}
|
|
32
|
-
|
|
38
|
+
const res = await Promise.resolve(preprocessor(obj[key]));
|
|
39
|
+
if (typeof res !== 'undefined') {
|
|
40
|
+
obj[key] = res;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
delete obj[key];
|
|
44
|
+
}
|
|
33
45
|
}
|
|
34
46
|
}));
|
|
35
47
|
if ('*' in schema.preprocessors) {
|
|
@@ -47,15 +59,25 @@ const validateObject = async (obj, schema, validator) => {
|
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
61
|
if (typeof schema.properties === 'object' && schema.properties) {
|
|
50
|
-
const errors =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
const errors = [
|
|
63
|
+
...(await Promise.all(Object.entries(schema.properties).map(async ([name, schema]) => {
|
|
64
|
+
const result = await validator.validate(schema, obj[name]);
|
|
65
|
+
if (result.valid)
|
|
66
|
+
return result;
|
|
67
|
+
return {
|
|
68
|
+
valid: false,
|
|
69
|
+
errors: (result.errors || []).map((e) => `->${name} ${e}`)
|
|
70
|
+
};
|
|
71
|
+
}))),
|
|
72
|
+
...(schema.noUnknownProperties === false
|
|
73
|
+
? []
|
|
74
|
+
: Object.keys(obj)
|
|
75
|
+
.filter((k) => !(k in schema.properties))
|
|
76
|
+
.map((k) => ({
|
|
77
|
+
valid: false,
|
|
78
|
+
errors: [`-> ${k} - unknown field`]
|
|
79
|
+
})))
|
|
80
|
+
]
|
|
59
81
|
.filter((r) => !r.valid)
|
|
60
82
|
.map((r) => r.errors)
|
|
61
83
|
.flat(Infinity);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const validateString: (obj: any, schema:
|
|
1
|
+
import { StringSchema, ValidationResult } from '../schema.js';
|
|
2
|
+
export declare const validateString: (obj: any, schema: StringSchema) => Promise<ValidationResult>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateString = void 0;
|
|
4
|
-
const validateString = async (obj, schema
|
|
4
|
+
const validateString = async (obj, schema) => {
|
|
5
5
|
if (typeof obj === 'undefined' &&
|
|
6
6
|
typeof schema === 'object' &&
|
|
7
7
|
schema.isRequired === false) {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateUnion = void 0;
|
|
4
|
+
const validateUnion = async (obj, schema, validator) => {
|
|
5
|
+
if (typeof obj === 'undefined' && schema.isRequired === false) {
|
|
6
|
+
return {
|
|
7
|
+
valid: true
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
if (obj === null && schema.isNullable) {
|
|
11
|
+
return {
|
|
12
|
+
valid: true
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(schema.variants) && schema.variants.length > 0) {
|
|
16
|
+
for (let i = 0; i < schema.variants.length; i++) {
|
|
17
|
+
const variant = schema.variants[i];
|
|
18
|
+
const result = await validator.validate(variant, obj);
|
|
19
|
+
if (result.valid) {
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
valid: false,
|
|
25
|
+
errors: ['object does not satisfy any scheme']
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
throw new Error('variants should be non empty array');
|
|
29
|
+
};
|
|
30
|
+
exports.validateUnion = validateUnion;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cleverbrush/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"object schema validator",
|
|
6
6
|
"schema",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"watch": "tsc --watch",
|
|
19
19
|
"build": "tsc"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
22
|
-
"@cleverbrush/deep": "0.0.
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@cleverbrush/deep": "1.0.0-beta.0"
|
|
23
23
|
},
|
|
24
24
|
"types": "./dist/index.d.ts"
|
|
25
25
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Schema } from '../schema.js';
|
|
2
|
+
import { alias } from './AliasSchemaBuilder.js';
|
|
3
|
+
|
|
4
|
+
test('Clean', () => {
|
|
5
|
+
const schema = alias('Something');
|
|
6
|
+
expect(schema).toHaveProperty('type', 'alias');
|
|
7
|
+
expect(schema).toHaveProperty('schemaName', 'Something');
|
|
8
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
9
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('optional', () => {
|
|
13
|
+
const schema = alias('Something').optional();
|
|
14
|
+
expect(schema).toHaveProperty('type', 'alias');
|
|
15
|
+
expect(schema).toHaveProperty('isRequired', false);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('optional - 2', () => {
|
|
19
|
+
const schema = alias('Something').optional().required();
|
|
20
|
+
expect(schema).toHaveProperty('type', 'alias');
|
|
21
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('optional - 3', () => {
|
|
25
|
+
const schema1 = alias('Something');
|
|
26
|
+
const schema2 = schema1.optional();
|
|
27
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
28
|
+
const isRequiredEqual = (schema1 as any).isRequired !== schema2.isRequired;
|
|
29
|
+
expect(e).toEqual(false);
|
|
30
|
+
expect(isRequiredEqual).toEqual(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('optional - 4', () => {
|
|
34
|
+
const schema1 = alias('Something').optional();
|
|
35
|
+
const schema2 = schema1.optional();
|
|
36
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
37
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
38
|
+
expect(e).toEqual(true);
|
|
39
|
+
expect(isRequiredEqual).toEqual(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('optional - 5', () => {
|
|
43
|
+
const schema1 = alias('Something');
|
|
44
|
+
const schema2 = schema1.required();
|
|
45
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
46
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
47
|
+
expect(e).toEqual(true);
|
|
48
|
+
expect(isRequiredEqual).toEqual(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('optional - 6', () => {
|
|
52
|
+
const schema1 = alias('Something').optional();
|
|
53
|
+
const schema2 = schema1.required();
|
|
54
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
55
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
56
|
+
expect(e).toEqual(false);
|
|
57
|
+
expect(isRequiredEqual).toEqual(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('optional - 7', () => {
|
|
61
|
+
const schema1 = alias('Something').optional().required();
|
|
62
|
+
const schema2 = schema1.required();
|
|
63
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
64
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
65
|
+
expect(e).toEqual(true);
|
|
66
|
+
expect(isRequiredEqual).toEqual(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('nullable', () => {
|
|
70
|
+
const schema = alias('Something').nullable();
|
|
71
|
+
expect(schema).toHaveProperty('type', 'alias');
|
|
72
|
+
expect(schema).toHaveProperty('isNullable', true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('nullable - 2', () => {
|
|
76
|
+
const schema = alias('Something').nullable().notNullable();
|
|
77
|
+
expect(schema).toHaveProperty('type', 'alias');
|
|
78
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('nullable - 3', () => {
|
|
82
|
+
const schema1 = alias('Something');
|
|
83
|
+
const schema2 = schema1.nullable();
|
|
84
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
85
|
+
const isNullableEqual = (schema1 as any).isNullable !== schema2.isNullable;
|
|
86
|
+
expect(e).toEqual(false);
|
|
87
|
+
expect(isNullableEqual).toEqual(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('nullable - 4', () => {
|
|
91
|
+
const schema1 = alias('Something').nullable();
|
|
92
|
+
const schema2 = schema1.nullable();
|
|
93
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
94
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
95
|
+
expect(e).toEqual(true);
|
|
96
|
+
expect(isNullableEqual).toEqual(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('nullable - 5', () => {
|
|
100
|
+
const schema1 = alias('Something');
|
|
101
|
+
const schema2 = schema1.nullable();
|
|
102
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
103
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
104
|
+
expect(e).toEqual(false);
|
|
105
|
+
expect(isNullableEqual).toEqual(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('nullable - 6', () => {
|
|
109
|
+
const schema1 = alias('Something');
|
|
110
|
+
const schema2 = schema1.notNullable();
|
|
111
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
112
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
113
|
+
expect(e).toEqual(true);
|
|
114
|
+
expect(isNullableEqual).toEqual(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('nullable - 7', () => {
|
|
118
|
+
const schema1 = alias('Something').nullable();
|
|
119
|
+
const schema2 = schema1.notNullable();
|
|
120
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
121
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
122
|
+
expect(e).toEqual(false);
|
|
123
|
+
expect(isNullableEqual).toEqual(false);
|
|
124
|
+
});
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
import { InferType, Schema } from '../schema.js';
|
|
3
|
+
import { defaultSchemas } from '../defaultSchemas.js';
|
|
4
|
+
import { ISchemaRegistry } from '../index.js';
|
|
5
|
+
|
|
6
|
+
export interface IAliasSchemaBuilder<
|
|
7
|
+
TSchemaName extends keyof TAliases,
|
|
8
|
+
TRequired extends boolean = true,
|
|
9
|
+
TNullable extends boolean = false,
|
|
10
|
+
TAliases extends Record<string, any> = never,
|
|
11
|
+
TAliasesCompiledType = any,
|
|
12
|
+
TExternalRegistryKeys extends Record<string, any> = {}
|
|
13
|
+
> extends ISchemaBuilder<TRequired, TNullable> {
|
|
14
|
+
readonly type: 'alias';
|
|
15
|
+
schemaName: TSchemaName;
|
|
16
|
+
externalRegistry: ISchemaRegistry<TExternalRegistryKeys>;
|
|
17
|
+
|
|
18
|
+
optional(): IAliasSchemaBuilder<
|
|
19
|
+
TSchemaName,
|
|
20
|
+
false,
|
|
21
|
+
TNullable,
|
|
22
|
+
TAliases,
|
|
23
|
+
TAliasesCompiledType,
|
|
24
|
+
TExternalRegistryKeys
|
|
25
|
+
>;
|
|
26
|
+
required(): IAliasSchemaBuilder<
|
|
27
|
+
TSchemaName,
|
|
28
|
+
true,
|
|
29
|
+
TNullable,
|
|
30
|
+
TAliases,
|
|
31
|
+
TAliasesCompiledType,
|
|
32
|
+
TExternalRegistryKeys
|
|
33
|
+
>;
|
|
34
|
+
nullable(): IAliasSchemaBuilder<
|
|
35
|
+
TSchemaName,
|
|
36
|
+
TRequired,
|
|
37
|
+
true,
|
|
38
|
+
TAliases,
|
|
39
|
+
TAliasesCompiledType,
|
|
40
|
+
TExternalRegistryKeys
|
|
41
|
+
>;
|
|
42
|
+
notNullable(): IAliasSchemaBuilder<
|
|
43
|
+
TSchemaName,
|
|
44
|
+
TRequired,
|
|
45
|
+
false,
|
|
46
|
+
TAliases,
|
|
47
|
+
TAliasesCompiledType,
|
|
48
|
+
TExternalRegistryKeys
|
|
49
|
+
>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class AliasSchemaBuilder<
|
|
53
|
+
TSchemaName extends keyof TAliases,
|
|
54
|
+
TRequired extends boolean = true,
|
|
55
|
+
TNullable extends boolean = false,
|
|
56
|
+
TAliases extends Record<string, any> = never,
|
|
57
|
+
TAliasesCompiledType = any,
|
|
58
|
+
TExternalRegistryKeys extends Record<string, any> = {}
|
|
59
|
+
>
|
|
60
|
+
extends SchemaBuilder<TRequired, TNullable>
|
|
61
|
+
implements
|
|
62
|
+
IAliasSchemaBuilder<
|
|
63
|
+
TSchemaName,
|
|
64
|
+
TRequired,
|
|
65
|
+
TNullable,
|
|
66
|
+
TAliases,
|
|
67
|
+
TAliasesCompiledType,
|
|
68
|
+
TExternalRegistryKeys
|
|
69
|
+
>
|
|
70
|
+
{
|
|
71
|
+
public clone(): this {
|
|
72
|
+
return AliasSchemaBuilder.create(this._schema as any) as this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public get _schema(): Schema {
|
|
76
|
+
return Object.assign(
|
|
77
|
+
{
|
|
78
|
+
type: this.type,
|
|
79
|
+
schemaName: this.schemaName,
|
|
80
|
+
externalRegistry: this.externalRegistry
|
|
81
|
+
},
|
|
82
|
+
this.getCommonSchema()
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public static create<
|
|
87
|
+
TSchemaName extends keyof TAliases,
|
|
88
|
+
TRequired extends boolean = true,
|
|
89
|
+
TNullable extends boolean = false,
|
|
90
|
+
TAliases extends Record<string, any> = never,
|
|
91
|
+
TAliasesCompiledType = any,
|
|
92
|
+
TExternalRegistryKeys extends Record<string, any> = {}
|
|
93
|
+
>(obj?: {
|
|
94
|
+
schemaName: TSchemaName;
|
|
95
|
+
isRequired: TRequired;
|
|
96
|
+
isNullable: TNullable;
|
|
97
|
+
externalRegistry?: ISchemaRegistry<TExternalRegistryKeys>;
|
|
98
|
+
}): IAliasSchemaBuilder<
|
|
99
|
+
TSchemaName,
|
|
100
|
+
TRequired,
|
|
101
|
+
TNullable,
|
|
102
|
+
TAliases,
|
|
103
|
+
TAliasesCompiledType,
|
|
104
|
+
TExternalRegistryKeys
|
|
105
|
+
> {
|
|
106
|
+
return new AliasSchemaBuilder(obj as any) as any;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public schemaName: TSchemaName;
|
|
110
|
+
public externalRegistry: ISchemaRegistry<TExternalRegistryKeys>;
|
|
111
|
+
|
|
112
|
+
private constructor(obj: {
|
|
113
|
+
schemaName: TSchemaName;
|
|
114
|
+
isRequired: TRequired;
|
|
115
|
+
isNullable: TNullable;
|
|
116
|
+
externalRegistry?: ISchemaRegistry<any>;
|
|
117
|
+
}) {
|
|
118
|
+
super(obj);
|
|
119
|
+
if (typeof obj === 'object' && obj) {
|
|
120
|
+
if (typeof obj.schemaName === 'string') {
|
|
121
|
+
this.schemaName = obj.schemaName;
|
|
122
|
+
}
|
|
123
|
+
if (
|
|
124
|
+
typeof obj.externalRegistry === 'object' &&
|
|
125
|
+
obj.externalRegistry
|
|
126
|
+
) {
|
|
127
|
+
this.externalRegistry = obj.externalRegistry;
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
const defaultSchema = defaultSchemas['alias'] as any;
|
|
131
|
+
this.isRequired = defaultSchema.isRequired;
|
|
132
|
+
this.isNullable = defaultSchema.isNullable;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get type(): 'alias' {
|
|
137
|
+
return 'alias';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
optional(): IAliasSchemaBuilder<
|
|
141
|
+
TSchemaName,
|
|
142
|
+
false,
|
|
143
|
+
TNullable,
|
|
144
|
+
TAliases,
|
|
145
|
+
TAliasesCompiledType,
|
|
146
|
+
TExternalRegistryKeys
|
|
147
|
+
> {
|
|
148
|
+
if (this.isRequired === false) {
|
|
149
|
+
return this as any;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return AliasSchemaBuilder.create({
|
|
153
|
+
...(this._schema as any),
|
|
154
|
+
isRequired: false
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
required(): IAliasSchemaBuilder<
|
|
159
|
+
TSchemaName,
|
|
160
|
+
true,
|
|
161
|
+
TNullable,
|
|
162
|
+
TAliases,
|
|
163
|
+
TAliasesCompiledType,
|
|
164
|
+
TExternalRegistryKeys
|
|
165
|
+
> {
|
|
166
|
+
if (this.isRequired === true) {
|
|
167
|
+
return this as any;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return AliasSchemaBuilder.create({
|
|
171
|
+
...(this._schema as any),
|
|
172
|
+
isRequired: true
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
nullable(): IAliasSchemaBuilder<
|
|
177
|
+
TSchemaName,
|
|
178
|
+
TRequired,
|
|
179
|
+
true,
|
|
180
|
+
TAliases,
|
|
181
|
+
TAliasesCompiledType,
|
|
182
|
+
TExternalRegistryKeys
|
|
183
|
+
> {
|
|
184
|
+
if (this._isNullable === true) {
|
|
185
|
+
return this as any;
|
|
186
|
+
}
|
|
187
|
+
return AliasSchemaBuilder.create({
|
|
188
|
+
...(this._schema as any),
|
|
189
|
+
isNullable: true
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
notNullable(): IAliasSchemaBuilder<
|
|
194
|
+
TSchemaName,
|
|
195
|
+
TRequired,
|
|
196
|
+
false,
|
|
197
|
+
TAliases,
|
|
198
|
+
TAliasesCompiledType,
|
|
199
|
+
TExternalRegistryKeys
|
|
200
|
+
> {
|
|
201
|
+
if (this.isNullable === false) {
|
|
202
|
+
return this as any;
|
|
203
|
+
}
|
|
204
|
+
return AliasSchemaBuilder.create({
|
|
205
|
+
...(this._schema as any),
|
|
206
|
+
isNullable: false
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export const alias = <
|
|
212
|
+
TSchemaName extends keyof TAliases,
|
|
213
|
+
TAliases extends Record<string, any>,
|
|
214
|
+
TAliasesCompiledType = InferType<TAliases[TSchemaName]>
|
|
215
|
+
>(
|
|
216
|
+
schemaName: TSchemaName
|
|
217
|
+
) =>
|
|
218
|
+
AliasSchemaBuilder.create<
|
|
219
|
+
TSchemaName,
|
|
220
|
+
true,
|
|
221
|
+
false,
|
|
222
|
+
TAliases,
|
|
223
|
+
TAliasesCompiledType
|
|
224
|
+
>({
|
|
225
|
+
schemaName,
|
|
226
|
+
isRequired: true,
|
|
227
|
+
isNullable: false
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
export const externalAlias = <
|
|
231
|
+
TSchemaName extends keyof TAliases,
|
|
232
|
+
TAliases extends Record<string, any>,
|
|
233
|
+
TExternalSchemaRegistry extends ISchemaRegistry<TAliases>,
|
|
234
|
+
TAliasesCompiledType = InferType<TAliases[TSchemaName]>
|
|
235
|
+
>(
|
|
236
|
+
registry: TExternalSchemaRegistry,
|
|
237
|
+
schemaName: TSchemaName
|
|
238
|
+
) =>
|
|
239
|
+
AliasSchemaBuilder.create<
|
|
240
|
+
TSchemaName,
|
|
241
|
+
true,
|
|
242
|
+
false,
|
|
243
|
+
TAliases,
|
|
244
|
+
TAliasesCompiledType
|
|
245
|
+
>({
|
|
246
|
+
schemaName,
|
|
247
|
+
isRequired: true,
|
|
248
|
+
isNullable: false,
|
|
249
|
+
externalRegistry: registry
|
|
250
|
+
});
|