@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,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.string = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
+
class StringSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
+
_minLength;
|
|
8
|
+
_maxLength;
|
|
9
|
+
_equals;
|
|
10
|
+
type = 'string';
|
|
11
|
+
clone() {
|
|
12
|
+
return StringSchemaBuilder.create(this._schema);
|
|
13
|
+
}
|
|
14
|
+
get _schema() {
|
|
15
|
+
return Object.assign({
|
|
16
|
+
type: this.type
|
|
17
|
+
}, this.getCommonSchema(), typeof this._minLength !== 'undefined'
|
|
18
|
+
? { minLength: this._minLength }
|
|
19
|
+
: {}, typeof this._maxLength !== 'undefined'
|
|
20
|
+
? { maxLength: this._maxLength }
|
|
21
|
+
: {}, typeof this._equals !== 'undefined' ? { equals: this._equals } : {});
|
|
22
|
+
}
|
|
23
|
+
optional() {
|
|
24
|
+
if (this.isRequired === false) {
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
return StringSchemaBuilder.create({
|
|
28
|
+
...this._schema,
|
|
29
|
+
isRequired: false
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
required() {
|
|
33
|
+
if (this.isRequired === true) {
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
return StringSchemaBuilder.create({
|
|
37
|
+
...this._schema,
|
|
38
|
+
isRequired: true
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
nullable() {
|
|
42
|
+
if (this.isNullable === true) {
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
return StringSchemaBuilder.create({
|
|
46
|
+
...this._schema,
|
|
47
|
+
isNullable: true
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
notNullable() {
|
|
51
|
+
if (this.isNullable === false) {
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
return StringSchemaBuilder.create({
|
|
55
|
+
...this._schema,
|
|
56
|
+
isNullable: false
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
minLength(val) {
|
|
60
|
+
if (this._minLength === val) {
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
return StringSchemaBuilder.create({
|
|
64
|
+
...this._schema,
|
|
65
|
+
minLength: val
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
clearMinLength() {
|
|
69
|
+
if (typeof this._minLength === 'undefined') {
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
return StringSchemaBuilder.create({
|
|
73
|
+
...this._schema,
|
|
74
|
+
minLength: undefined
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
maxLength(val) {
|
|
78
|
+
if (this._maxLength === val) {
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
return StringSchemaBuilder.create({
|
|
82
|
+
...this._schema,
|
|
83
|
+
maxLength: val
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
clearMaxLength() {
|
|
87
|
+
if (typeof this._maxLength === 'undefined') {
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
return StringSchemaBuilder.create({
|
|
91
|
+
...this._schema,
|
|
92
|
+
maxLength: undefined
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
equals(val) {
|
|
96
|
+
if (this._equals === val) {
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
return StringSchemaBuilder.create({
|
|
100
|
+
...this._schema,
|
|
101
|
+
equals: val
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
clearEquals() {
|
|
105
|
+
if (typeof this._equals === 'undefined') {
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
return StringSchemaBuilder.create({
|
|
109
|
+
...this._schema,
|
|
110
|
+
equals: undefined
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
static create(obj) {
|
|
114
|
+
return new StringSchemaBuilder(obj);
|
|
115
|
+
}
|
|
116
|
+
constructor(obj) {
|
|
117
|
+
super(obj);
|
|
118
|
+
if (typeof obj === 'object' && obj) {
|
|
119
|
+
if (typeof obj.minLength !== 'undefined') {
|
|
120
|
+
this._minLength = obj.minLength;
|
|
121
|
+
}
|
|
122
|
+
if (typeof obj.maxLength !== 'undefined') {
|
|
123
|
+
this._maxLength = obj.maxLength;
|
|
124
|
+
}
|
|
125
|
+
if (typeof obj.equals !== 'undefined') {
|
|
126
|
+
this._equals = obj.equals;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const defaultSchema = defaultSchemas_js_1.defaultSchemas['string'];
|
|
131
|
+
this.isRequired = defaultSchema.isRequired;
|
|
132
|
+
this.isNullable = defaultSchema.isNullable;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const string = (equals) => {
|
|
137
|
+
const res = StringSchemaBuilder.create();
|
|
138
|
+
return typeof equals === 'string' ? res.equals(equals) : res;
|
|
139
|
+
};
|
|
140
|
+
exports.string = string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ISchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
export interface IUnionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TVariants extends any[] = []> extends ISchemaBuilder<TRequired, TNullable> {
|
|
3
|
+
optional(): IUnionSchemaBuilder<false, TNullable, TVariants>;
|
|
4
|
+
required(): IUnionSchemaBuilder<true, TNullable, TVariants>;
|
|
5
|
+
nullable(): IUnionSchemaBuilder<TRequired, true, TVariants>;
|
|
6
|
+
notNullable(): IUnionSchemaBuilder<TRequired, false, TVariants>;
|
|
7
|
+
or<T>(schema: T): IUnionSchemaBuilder<TRequired, TNullable, [...TVariants, T]>;
|
|
8
|
+
clearVariants(): IUnionSchemaBuilder<TRequired, TNullable, []>;
|
|
9
|
+
}
|
|
10
|
+
export declare const union: <T extends ISchemaBuilder<true, false>, K extends T[], TRequired extends boolean = true, TNullable extends boolean = false>(...schemas: K) => IUnionSchemaBuilder<TRequired, TNullable, K>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.union = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
class UnionSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
6
|
+
type = 'union';
|
|
7
|
+
get _schema() {
|
|
8
|
+
return Object.assign({
|
|
9
|
+
type: this.type
|
|
10
|
+
}, this.getCommonSchema(), {
|
|
11
|
+
variants: this._variants.map((variant) => {
|
|
12
|
+
if (variant instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
13
|
+
return variant._schema;
|
|
14
|
+
}
|
|
15
|
+
return JSON.parse(JSON.stringify(variant));
|
|
16
|
+
})
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
clone() {
|
|
20
|
+
return UnionSchemaBuilder.create(this._schema);
|
|
21
|
+
}
|
|
22
|
+
_variants;
|
|
23
|
+
static create(obj) {
|
|
24
|
+
return new UnionSchemaBuilder(obj);
|
|
25
|
+
}
|
|
26
|
+
constructor(obj) {
|
|
27
|
+
super(obj);
|
|
28
|
+
this._variants = [];
|
|
29
|
+
if (Array.isArray(obj.variants)) {
|
|
30
|
+
for (let i = 0; i < obj.variants.length; i++) {
|
|
31
|
+
this._variants.push(obj.variants[i]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
optional() {
|
|
36
|
+
if (this.isRequired === false) {
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
return UnionSchemaBuilder.create({
|
|
40
|
+
...this._schema,
|
|
41
|
+
isRequired: false
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
required() {
|
|
45
|
+
if (this.isRequired === true) {
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
return UnionSchemaBuilder.create({
|
|
49
|
+
...this._schema,
|
|
50
|
+
isRequired: true
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
nullable() {
|
|
54
|
+
if (this.isNullable === true) {
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
return UnionSchemaBuilder.create({
|
|
58
|
+
...this._schema,
|
|
59
|
+
isNullable: true
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
notNullable() {
|
|
63
|
+
if (this.isNullable === false) {
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
return UnionSchemaBuilder.create({
|
|
67
|
+
...this._schema,
|
|
68
|
+
isNullable: false
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
or(schema) {
|
|
72
|
+
return UnionSchemaBuilder.create({
|
|
73
|
+
...this._schema,
|
|
74
|
+
variants: [...this._variants, schema]
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
clearVariants() {
|
|
78
|
+
if (this._variants.length === 0)
|
|
79
|
+
return this;
|
|
80
|
+
return UnionSchemaBuilder.create({
|
|
81
|
+
...this._schema,
|
|
82
|
+
variants: []
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const union = (...schemas) => {
|
|
87
|
+
if (schemas.length < 1) {
|
|
88
|
+
throw new Error('should provide at least one variant');
|
|
89
|
+
}
|
|
90
|
+
let res = UnionSchemaBuilder.create({
|
|
91
|
+
isRequired: true,
|
|
92
|
+
isNullable: false,
|
|
93
|
+
variants: [schemas[0]]
|
|
94
|
+
});
|
|
95
|
+
for (let i = 1; i < schemas.length; i++) {
|
|
96
|
+
res = res.or(schemas[i]);
|
|
97
|
+
}
|
|
98
|
+
return res;
|
|
99
|
+
};
|
|
100
|
+
exports.union = union;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultSchemas = void 0;
|
|
4
|
+
exports.defaultSchemas = {
|
|
5
|
+
alias: {
|
|
6
|
+
type: 'alias',
|
|
7
|
+
// TODO: remove
|
|
8
|
+
schemaName: '',
|
|
9
|
+
isRequired: true,
|
|
10
|
+
isNullable: false
|
|
11
|
+
},
|
|
12
|
+
number: {
|
|
13
|
+
type: 'number',
|
|
14
|
+
isRequired: true,
|
|
15
|
+
isNullable: false,
|
|
16
|
+
ensureNotNaN: true,
|
|
17
|
+
ensureIsFinite: true
|
|
18
|
+
},
|
|
19
|
+
boolean: {
|
|
20
|
+
type: 'boolean',
|
|
21
|
+
isRequired: true,
|
|
22
|
+
isNullable: false
|
|
23
|
+
},
|
|
24
|
+
string: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
isRequired: true,
|
|
27
|
+
isNullable: false
|
|
28
|
+
},
|
|
29
|
+
array: {
|
|
30
|
+
type: 'array',
|
|
31
|
+
isRequired: true,
|
|
32
|
+
isNullable: false
|
|
33
|
+
},
|
|
34
|
+
object: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
noUnknownProperties: true,
|
|
37
|
+
isNullable: false,
|
|
38
|
+
isRequired: true
|
|
39
|
+
},
|
|
40
|
+
function: {
|
|
41
|
+
type: 'function',
|
|
42
|
+
isRequired: true,
|
|
43
|
+
isNullable: false
|
|
44
|
+
}
|
|
45
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,94 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { InferType, ExpandSchemaBuilder, ValidationResultWithObject } from './schema.js';
|
|
2
|
+
import SchemaRegistry from './schemaRegistry.js';
|
|
3
|
+
import { alias } from './builders/AliasSchemaBuilder.js';
|
|
4
|
+
import { array } from './builders/ArraySchemaBuilder.js';
|
|
5
|
+
import { boolean } from './builders/BooleanSchemaBuilder.js';
|
|
6
|
+
import { number } from './builders/NumberSchemaBuilder.js';
|
|
7
|
+
import { object } from './builders/ObjectSchemaBuilder.js';
|
|
8
|
+
import { string } from './builders/StringSchemaBuilder.js';
|
|
9
|
+
import { union } from './builders/UnionSchemaBuilder.js';
|
|
10
|
+
import { func } from './builders/FunctionSchemaBuilder.js';
|
|
11
|
+
export declare type GetNodes<T extends Record<string, any>, TPrefix extends string = ''> = {
|
|
12
|
+
[k in keyof T]: k extends `${TPrefix}${infer F}.${infer S}` ? F : never;
|
|
13
|
+
}[keyof T];
|
|
14
|
+
export declare type GetLeafs<T extends Record<string, any>, TPrefix extends string = ''> = {
|
|
15
|
+
[k in keyof T]: k extends `${TPrefix}${infer F}` ? F extends `${infer S}.${infer G}` ? never : F : never;
|
|
16
|
+
}[keyof T];
|
|
17
|
+
export declare type Unfold<TBag extends Record<string, any>, TPrefix extends string = ''> = {
|
|
18
|
+
[k in GetLeafs<TBag, TPrefix>]: ISchemaActions<TBag[`${TPrefix}${k}`]>;
|
|
19
|
+
} & {
|
|
20
|
+
[k in GetNodes<TBag, TPrefix>]: Unfold<TBag, `${TPrefix}${k}.`>;
|
|
8
21
|
};
|
|
9
|
-
export declare type ValidationResultRaw = {
|
|
10
|
-
valid: boolean;
|
|
11
|
-
errors?: Array<string>;
|
|
12
|
-
};
|
|
13
|
-
export declare type ValidationResult = ValidationResultRaw | Promise<ValidationResultRaw>;
|
|
14
|
-
export declare type Validator<TObj> = (value: TObj) => ValidationResult;
|
|
15
|
-
export declare type SchemaDefintion<TObj> = {
|
|
16
|
-
type: DefaultSchemaType;
|
|
17
|
-
isRequired?: boolean;
|
|
18
|
-
isNullable?: boolean;
|
|
19
|
-
validators?: Array<Validator<TObj>>;
|
|
20
|
-
};
|
|
21
|
-
export declare type ObjectSchemaDefinition<T> = Omit<SchemaDefintion<T>, 'type'> & {
|
|
22
|
-
type: 'object';
|
|
23
|
-
extends?: string;
|
|
24
|
-
properties?: Partial<{
|
|
25
|
-
[S in keyof T]: Schema<PropType<T, S>>;
|
|
26
|
-
}>;
|
|
27
|
-
preprocessors?: Partial<{
|
|
28
|
-
[S in keyof T | '*']: S extends keyof T ? ((value: unknown) => undefined | PropType<T, S> | Promise<PropType<T, S>>) | string : (value: T) => void | Promise<void>;
|
|
29
|
-
}>;
|
|
30
|
-
};
|
|
31
|
-
export declare type AliasSchemaDefinition<T> = Omit<SchemaDefintion<T>, 'type'> & {
|
|
32
|
-
type: 'alias';
|
|
33
|
-
schemaName: string;
|
|
34
|
-
};
|
|
35
|
-
export declare type ObjectSchemaDefinitionParam<T> = Omit<ObjectSchemaDefinition<T>, 'type'>;
|
|
36
|
-
export declare type ParamsValidators<TFunc extends (...args: any) => any> = {
|
|
37
|
-
[S in keyof Parameters<TFunc>]: SingleSchema<Parameters<TFunc>[S]>;
|
|
38
|
-
};
|
|
39
|
-
export declare type FunctionSchemaDefinition<T extends (...args: any) => any> = Omit<SchemaDefintion<T>, 'type'> & {
|
|
40
|
-
type: 'function';
|
|
41
|
-
params?: ParamsValidators<T>;
|
|
42
|
-
};
|
|
43
|
-
export declare type BooleanSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
44
|
-
type: 'boolean';
|
|
45
|
-
equals?: boolean;
|
|
46
|
-
};
|
|
47
|
-
export declare type NumberSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
48
|
-
type: 'number';
|
|
49
|
-
min?: number;
|
|
50
|
-
max?: number;
|
|
51
|
-
equals?: number;
|
|
52
|
-
isInteger?: boolean;
|
|
53
|
-
ensureNotNaN?: boolean;
|
|
54
|
-
ensureIsFinite?: boolean;
|
|
55
|
-
};
|
|
56
|
-
export declare type StringSchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
57
|
-
type: 'string';
|
|
58
|
-
equals?: string;
|
|
59
|
-
minLength?: number;
|
|
60
|
-
maxLength?: number;
|
|
61
|
-
};
|
|
62
|
-
export declare type ArraySchemaDefinition<TObj> = Omit<SchemaDefintion<TObj>, 'type'> & {
|
|
63
|
-
type: 'array';
|
|
64
|
-
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
65
|
-
ofType?: Schema<any>;
|
|
66
|
-
minLength?: number;
|
|
67
|
-
maxLength?: number;
|
|
68
|
-
};
|
|
69
|
-
export declare type CompositeSchema<TObj> = TObj extends (...args: any) => any ? FunctionSchemaDefinition<TObj> : TObj extends number ? NumberSchemaDefinition<TObj> : TObj extends string ? StringSchemaDefinition<TObj> | 'string' : BooleanSchemaDefinition<TObj> | NumberSchemaDefinition<TObj> | StringSchemaDefinition<TObj> | ArraySchemaDefinition<TObj> | ObjectSchemaDefinition<TObj> | AliasSchemaDefinition<TObj>;
|
|
70
|
-
export declare type SingleSchema<TObj = Record<string, never>> = number | string | DefaultSchemaType | CompositeSchema<TObj>;
|
|
71
|
-
export declare type Schema<TObj> = SingleSchema<TObj> | Array<SingleSchema<TObj>>;
|
|
72
|
-
export declare type Cons<H, T extends unknown[] = []> = T['length'] extends 0 ? [H] : ((h: H, ...t: T) => void) extends (...r: infer R) => void ? R : never;
|
|
73
22
|
export interface ISchemaActions<S> {
|
|
74
|
-
validate(value: any): Promise<
|
|
75
|
-
schema: S
|
|
76
|
-
}
|
|
77
|
-
export interface ISchemasProvider<T extends unknown[]> {
|
|
78
|
-
schemas: Merge<T>;
|
|
23
|
+
validate(value: any): Promise<ValidationResultWithObject<InferType<ExpandSchemaBuilder<S>>>>;
|
|
24
|
+
schema: ExpandSchemaBuilder<S>;
|
|
79
25
|
}
|
|
80
|
-
export interface
|
|
81
|
-
|
|
82
|
-
addPreprocessor(name: string, preprocessor: (value: unknown) => unknown | Promise<unknown>): SchemaValidator<T, SchemaTypesStructures>;
|
|
83
|
-
addSchemaType<K, L extends ObjectSchemaDefinitionParam<M> | Array<Schema<any>>, M = any>(name: keyof K, schema: L): SchemaValidator<T & {
|
|
84
|
-
[key in keyof K]: typeof schema;
|
|
85
|
-
}, Cons<Unfold<keyof K, ISchemaActions<L>>, SchemaTypesStructures>>;
|
|
86
|
-
validate(schema: keyof T | DefaultSchemaType | Schema<any>, obj: any): Promise<ValidationResult>;
|
|
26
|
+
export interface ISchemaRegistry<T extends Record<string, any>> {
|
|
27
|
+
schemas: Unfold<T>;
|
|
87
28
|
}
|
|
88
|
-
export
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
29
|
+
export { SchemaRegistry };
|
|
30
|
+
declare const _default: {
|
|
31
|
+
SchemaRegistry: typeof SchemaRegistry;
|
|
32
|
+
alias: <TSchemaName extends keyof TAliases, TAliases extends Record<string, any>, TAliasesCompiledType = InferType<TAliases[TSchemaName]>>(schemaName: TSchemaName) => import("./builders/AliasSchemaBuilder.js").IAliasSchemaBuilder<TSchemaName, true, false, TAliases, TAliasesCompiledType, {}>;
|
|
33
|
+
array: () => import("./builders/ArraySchemaBuilder.js").IArraySchemaBuilder<true, false, undefined, undefined, undefined>;
|
|
34
|
+
boolean: () => import("./builders/BooleanSchemaBuilder.js").IBooleanSchemaBuilder<true, false, undefined>;
|
|
35
|
+
number: () => import("./builders/NumberSchemaBuilder.js").INumberSchemaBuilder<true, false, undefined, undefined, undefined, true, true, undefined>;
|
|
36
|
+
object: <T>(properties?: T) => import("./builders/ObjectSchemaBuilder.js").IObjectSchemaBuilder<true, false, true, T extends undefined ? {} : T, undefined>;
|
|
37
|
+
string: <T_1 extends string = undefined>(equals?: T_1) => import("./builders/StringSchemaBuilder.js").IStringSchemaBuilder<true, false, undefined, undefined, T_1 extends undefined ? undefined : T_1>;
|
|
38
|
+
union: <T_2 extends import("./builders/SchemaBuilder.js").ISchemaBuilder<true, false>, K extends T_2[], TRequired extends boolean = true, TNullable extends boolean = false>(...schemas: K) => import("./builders/UnionSchemaBuilder.js").IUnionSchemaBuilder<TRequired, TNullable, K>;
|
|
39
|
+
func: () => import("./builders/FunctionSchemaBuilder.js").IFunctionSchemaBuilder<true, false>;
|
|
40
|
+
};
|
|
41
|
+
export default _default;
|
|
42
|
+
export { alias, array, boolean, number, object, string, union, func, InferType };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const
|
|
8
|
-
exports.
|
|
9
|
-
|
|
6
|
+
exports.func = exports.union = exports.string = exports.object = exports.number = exports.boolean = exports.array = exports.alias = exports.SchemaRegistry = void 0;
|
|
7
|
+
const schemaRegistry_js_1 = __importDefault(require("./schemaRegistry.js"));
|
|
8
|
+
exports.SchemaRegistry = schemaRegistry_js_1.default;
|
|
9
|
+
const AliasSchemaBuilder_js_1 = require("./builders/AliasSchemaBuilder.js");
|
|
10
|
+
Object.defineProperty(exports, "alias", { enumerable: true, get: function () { return AliasSchemaBuilder_js_1.alias; } });
|
|
11
|
+
const ArraySchemaBuilder_js_1 = require("./builders/ArraySchemaBuilder.js");
|
|
12
|
+
Object.defineProperty(exports, "array", { enumerable: true, get: function () { return ArraySchemaBuilder_js_1.array; } });
|
|
13
|
+
const BooleanSchemaBuilder_js_1 = require("./builders/BooleanSchemaBuilder.js");
|
|
14
|
+
Object.defineProperty(exports, "boolean", { enumerable: true, get: function () { return BooleanSchemaBuilder_js_1.boolean; } });
|
|
15
|
+
const NumberSchemaBuilder_js_1 = require("./builders/NumberSchemaBuilder.js");
|
|
16
|
+
Object.defineProperty(exports, "number", { enumerable: true, get: function () { return NumberSchemaBuilder_js_1.number; } });
|
|
17
|
+
const ObjectSchemaBuilder_js_1 = require("./builders/ObjectSchemaBuilder.js");
|
|
18
|
+
Object.defineProperty(exports, "object", { enumerable: true, get: function () { return ObjectSchemaBuilder_js_1.object; } });
|
|
19
|
+
const StringSchemaBuilder_js_1 = require("./builders/StringSchemaBuilder.js");
|
|
20
|
+
Object.defineProperty(exports, "string", { enumerable: true, get: function () { return StringSchemaBuilder_js_1.string; } });
|
|
21
|
+
const UnionSchemaBuilder_js_1 = require("./builders/UnionSchemaBuilder.js");
|
|
22
|
+
Object.defineProperty(exports, "union", { enumerable: true, get: function () { return UnionSchemaBuilder_js_1.union; } });
|
|
23
|
+
const FunctionSchemaBuilder_js_1 = require("./builders/FunctionSchemaBuilder.js");
|
|
24
|
+
Object.defineProperty(exports, "func", { enumerable: true, get: function () { return FunctionSchemaBuilder_js_1.func; } });
|
|
25
|
+
exports.default = {
|
|
26
|
+
SchemaRegistry: schemaRegistry_js_1.default,
|
|
27
|
+
alias: AliasSchemaBuilder_js_1.alias,
|
|
28
|
+
array: ArraySchemaBuilder_js_1.array,
|
|
29
|
+
boolean: BooleanSchemaBuilder_js_1.boolean,
|
|
30
|
+
number: NumberSchemaBuilder_js_1.number,
|
|
31
|
+
object: ObjectSchemaBuilder_js_1.object,
|
|
32
|
+
string: StringSchemaBuilder_js_1.string,
|
|
33
|
+
union: UnionSchemaBuilder_js_1.union,
|
|
34
|
+
func: FunctionSchemaBuilder_js_1.func
|
|
35
|
+
};
|