@cleverbrush/schema 1.0.0-beta.3 → 1.0.0-beta.5
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/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 +27 -0
- package/dist/builders/FunctionSchemaBuilder.js +69 -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 +95 -0
- package/dist/defaultSchemas.d.ts +4 -0
- package/dist/defaultSchemas.js +45 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +32 -0
- package/dist/schema.d.ts +231 -0
- package/dist/schema.js +2 -0
- package/dist/schemaRegistry.d.ts +49 -0
- package/dist/schemaRegistry.js +278 -0
- package/dist/validators/validateArray.d.ts +3 -0
- package/dist/validators/validateArray.js +60 -0
- package/dist/validators/validateBoolean.d.ts +2 -0
- package/dist/validators/validateBoolean.js +30 -0
- package/dist/validators/validateFunction.d.ts +2 -0
- package/dist/validators/validateFunction.js +21 -0
- package/dist/validators/validateNumber.d.ts +2 -0
- package/dist/validators/validateNumber.js +69 -0
- package/dist/validators/validateObject.d.ts +3 -0
- package/dist/validators/validateObject.js +95 -0
- package/dist/validators/validateString.d.ts +2 -0
- package/dist/validators/validateString.js +50 -0
- package/dist/validators/validateUnion.d.ts +3 -0
- package/dist/validators/validateUnion.js +30 -0
- package/package.json +6 -2
- package/src/builders/FunctionSchemaBuilder.test.ts +11 -0
- package/src/builders/FunctionSchemaBuilder.ts +24 -16
- package/src/schema.ts +36 -14
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ISchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
import { Schema } from '../schema.js';
|
|
3
|
+
export interface IArraySchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TOfType extends Schema | undefined = undefined, TMaxLength extends number | undefined = undefined, TMinLength extends number | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
|
|
4
|
+
optional(): IArraySchemaBuilder<false, TNullable, TOfType, TMaxLength, TMinLength>;
|
|
5
|
+
required(): IArraySchemaBuilder<true, TNullable, TOfType, TMaxLength, TMinLength>;
|
|
6
|
+
ofType<T extends Schema>(schema: T): IArraySchemaBuilder<TRequired, TNullable, T, TMaxLength, TMinLength>;
|
|
7
|
+
nullable(): IArraySchemaBuilder<TRequired, true, TOfType, TMaxLength, TMinLength>;
|
|
8
|
+
notNullable(): IArraySchemaBuilder<TRequired, false, TOfType, TMaxLength, TMinLength>;
|
|
9
|
+
clearOfType(): IArraySchemaBuilder<TRequired, TNullable, undefined, TMaxLength, TMinLength>;
|
|
10
|
+
maxLength<T extends number>(length: T): IArraySchemaBuilder<TRequired, TNullable, TOfType, T, TMinLength>;
|
|
11
|
+
clearMaxLength(): IArraySchemaBuilder<TRequired, TNullable, TOfType, undefined, TMinLength>;
|
|
12
|
+
minLength<T extends number>(length: T): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, T>;
|
|
13
|
+
clearMinLength(): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, TMinLength>;
|
|
14
|
+
}
|
|
15
|
+
export declare const array: () => IArraySchemaBuilder<true, false, undefined, undefined, undefined>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.array = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
+
class ArraySchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
+
clone() {
|
|
8
|
+
return ArraySchemaBuilder.create(this._schema);
|
|
9
|
+
}
|
|
10
|
+
get _schema() {
|
|
11
|
+
return Object.assign({
|
|
12
|
+
type: this.type
|
|
13
|
+
}, this.getCommonSchema(), typeof this._ofType !== 'undefined'
|
|
14
|
+
? {
|
|
15
|
+
ofType: this._ofType
|
|
16
|
+
}
|
|
17
|
+
: {}, typeof this._minLength !== 'undefined'
|
|
18
|
+
? { minLength: this._minLength }
|
|
19
|
+
: {}, typeof this._maxLength !== 'undefined'
|
|
20
|
+
? { maxLength: this._maxLength }
|
|
21
|
+
: {});
|
|
22
|
+
}
|
|
23
|
+
static create(obj) {
|
|
24
|
+
return new ArraySchemaBuilder(obj);
|
|
25
|
+
}
|
|
26
|
+
constructor(obj) {
|
|
27
|
+
super(obj);
|
|
28
|
+
if (typeof obj === 'object' && obj) {
|
|
29
|
+
if (typeof obj.ofType !== 'undefined') {
|
|
30
|
+
this._ofType =
|
|
31
|
+
obj.ofType instanceof SchemaBuilder_js_1.SchemaBuilder
|
|
32
|
+
? obj.ofType.clone()
|
|
33
|
+
: { ...obj.ofType };
|
|
34
|
+
}
|
|
35
|
+
if (typeof obj.maxLength !== 'undefined') {
|
|
36
|
+
this._maxLength = obj.maxLength;
|
|
37
|
+
}
|
|
38
|
+
if (typeof obj.minLength !== 'undefined') {
|
|
39
|
+
this._minLength = obj.minLength;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const defaultSchema = defaultSchemas_js_1.defaultSchemas['array'];
|
|
44
|
+
this.isRequired = defaultSchema.isRequired;
|
|
45
|
+
this.isNullable = defaultSchema.isNullable;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
type = 'array';
|
|
49
|
+
_ofType;
|
|
50
|
+
_maxLength;
|
|
51
|
+
_minLength;
|
|
52
|
+
optional() {
|
|
53
|
+
if (this.isRequired === false) {
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
return ArraySchemaBuilder.create({
|
|
57
|
+
...this._schema,
|
|
58
|
+
isRequired: false
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
required() {
|
|
62
|
+
if (this.isRequired === true) {
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
return ArraySchemaBuilder.create({
|
|
66
|
+
...this._schema,
|
|
67
|
+
isRequired: true
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
nullable() {
|
|
71
|
+
if (this._isNullable === true) {
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
return ArraySchemaBuilder.create({
|
|
75
|
+
...this._schema,
|
|
76
|
+
isNullable: true
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
notNullable() {
|
|
80
|
+
if (this.isNullable === false) {
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
return ArraySchemaBuilder.create({
|
|
84
|
+
...this._schema,
|
|
85
|
+
isNullable: false
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
ofType(schema) {
|
|
89
|
+
return ArraySchemaBuilder.create({
|
|
90
|
+
...this._schema,
|
|
91
|
+
ofType: schema
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
clearOfType() {
|
|
95
|
+
if (typeof this._ofType === 'undefined') {
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
return ArraySchemaBuilder.create({
|
|
99
|
+
...this._schema,
|
|
100
|
+
ofType: undefined
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
maxLength(length) {
|
|
104
|
+
if (this._maxLength === length) {
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
return ArraySchemaBuilder.create({
|
|
108
|
+
...this._schema,
|
|
109
|
+
maxLength: length
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
clearMaxLength() {
|
|
113
|
+
if (typeof this._maxLength === 'undefined') {
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
return ArraySchemaBuilder.create({
|
|
117
|
+
...this._schema,
|
|
118
|
+
maxLength: undefined
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
minLength(length) {
|
|
122
|
+
if (this._minLength === length) {
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
return ArraySchemaBuilder.create({
|
|
126
|
+
...this._schema,
|
|
127
|
+
minLength: length
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
clearMinLength() {
|
|
131
|
+
if (typeof this._minLength === 'undefined') {
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
return ArraySchemaBuilder.create({
|
|
135
|
+
...this._schema,
|
|
136
|
+
minLength: undefined
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const array = () => ArraySchemaBuilder.create();
|
|
141
|
+
exports.array = array;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Schema, Validator } from '../schema.js';
|
|
2
|
+
import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
|
|
3
|
+
export interface IBooleanSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
|
|
4
|
+
equals<T extends boolean>(val: T): IBooleanSchemaBuilder<TRequired, TNullable, T>;
|
|
5
|
+
clearEqualsTo(): IBooleanSchemaBuilder<TRequired, TNullable, undefined>;
|
|
6
|
+
optional(): IBooleanSchemaBuilder<false, TNullable, TEqualsTo>;
|
|
7
|
+
required(): IBooleanSchemaBuilder<true, TNullable, TEqualsTo>;
|
|
8
|
+
nullable(): IBooleanSchemaBuilder<TRequired, true, TEqualsTo>;
|
|
9
|
+
notNullable(): IBooleanSchemaBuilder<TRequired, false, TEqualsTo>;
|
|
10
|
+
}
|
|
11
|
+
export declare class BooleanSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined> extends SchemaBuilder<TRequired, TNullable> implements IBooleanSchemaBuilder<TRequired, TNullable, TEqualsTo> {
|
|
12
|
+
protected readonly type = "boolean";
|
|
13
|
+
get _schema(): Schema;
|
|
14
|
+
clone(): this;
|
|
15
|
+
static create<TRequired extends boolean = true, TNullable extends boolean = false, TEqualsTo extends boolean | undefined = undefined>(obj?: {
|
|
16
|
+
isRequired: TRequired;
|
|
17
|
+
isNullable: TNullable;
|
|
18
|
+
equals?: TEqualsTo;
|
|
19
|
+
validators?: Validator[];
|
|
20
|
+
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
21
|
+
}): IBooleanSchemaBuilder<TRequired, TNullable, TEqualsTo>;
|
|
22
|
+
private constructor();
|
|
23
|
+
protected _equals?: TEqualsTo;
|
|
24
|
+
equals<T extends boolean>(val: T): IBooleanSchemaBuilder<TRequired, TNullable, T>;
|
|
25
|
+
clearEqualsTo(): IBooleanSchemaBuilder<TRequired, TNullable, undefined>;
|
|
26
|
+
optional(): IBooleanSchemaBuilder<false, TNullable, TEqualsTo>;
|
|
27
|
+
required(): IBooleanSchemaBuilder<true, TNullable, TEqualsTo>;
|
|
28
|
+
nullable(): IBooleanSchemaBuilder<TRequired, true, TEqualsTo>;
|
|
29
|
+
notNullable(): IBooleanSchemaBuilder<TRequired, false, TEqualsTo>;
|
|
30
|
+
}
|
|
31
|
+
export declare const boolean: () => IBooleanSchemaBuilder;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.boolean = exports.BooleanSchemaBuilder = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
+
class BooleanSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
+
type = 'boolean';
|
|
8
|
+
get _schema() {
|
|
9
|
+
return Object.assign({
|
|
10
|
+
type: this.type
|
|
11
|
+
}, this.getCommonSchema(), typeof this._equals !== 'undefined' ? { equals: this._equals } : {});
|
|
12
|
+
}
|
|
13
|
+
clone() {
|
|
14
|
+
return BooleanSchemaBuilder.create(this._schema);
|
|
15
|
+
}
|
|
16
|
+
static create(obj) {
|
|
17
|
+
return new BooleanSchemaBuilder(obj);
|
|
18
|
+
}
|
|
19
|
+
constructor(obj) {
|
|
20
|
+
super(obj);
|
|
21
|
+
if (typeof obj === 'object' && obj) {
|
|
22
|
+
if (typeof obj.equals !== 'undefined') {
|
|
23
|
+
this._equals = obj.equals;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const defaultSchema = defaultSchemas_js_1.defaultSchemas['boolean'];
|
|
28
|
+
this.isRequired = defaultSchema.isRequired;
|
|
29
|
+
this.isNullable = defaultSchema.isNullable;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
_equals;
|
|
33
|
+
equals(val) {
|
|
34
|
+
if (this._equals === val) {
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
return BooleanSchemaBuilder.create({
|
|
38
|
+
...this._schema,
|
|
39
|
+
equals: val
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
clearEqualsTo() {
|
|
43
|
+
if (typeof this._equals === 'undefined') {
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
return BooleanSchemaBuilder.create({
|
|
47
|
+
...this._schema,
|
|
48
|
+
equals: undefined
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
optional() {
|
|
52
|
+
if (this.isRequired === false) {
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
return BooleanSchemaBuilder.create({
|
|
56
|
+
...this._schema,
|
|
57
|
+
isRequired: false
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
required() {
|
|
61
|
+
if (this.isRequired === true) {
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
return BooleanSchemaBuilder.create({
|
|
65
|
+
...this._schema,
|
|
66
|
+
isRequired: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
nullable() {
|
|
70
|
+
if (this.isNullable === true) {
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
return BooleanSchemaBuilder.create({
|
|
74
|
+
...this._schema,
|
|
75
|
+
isNullable: true
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
notNullable() {
|
|
79
|
+
if (this.isNullable === false) {
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
return BooleanSchemaBuilder.create({
|
|
83
|
+
...this._schema,
|
|
84
|
+
isNullable: false
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.BooleanSchemaBuilder = BooleanSchemaBuilder;
|
|
89
|
+
const boolean = () => BooleanSchemaBuilder.create();
|
|
90
|
+
exports.boolean = boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Schema, Validator } from '../schema.js';
|
|
2
|
+
import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
|
|
3
|
+
export interface IFunctionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMapToType = undefined> extends ISchemaBuilder<TRequired, TNullable> {
|
|
4
|
+
optional(): IFunctionSchemaBuilder<false, TNullable, TMapToType>;
|
|
5
|
+
required(): IFunctionSchemaBuilder<true, TNullable, TMapToType>;
|
|
6
|
+
nullable(): IFunctionSchemaBuilder<TRequired, true, TMapToType>;
|
|
7
|
+
notNullable(): IFunctionSchemaBuilder<TRequired, false, TMapToType>;
|
|
8
|
+
mapToType<T>(): IFunctionSchemaBuilder<TRequired, TNullable, T>;
|
|
9
|
+
}
|
|
10
|
+
export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMapToType = undefined> extends SchemaBuilder<TRequired, TNullable> implements IFunctionSchemaBuilder<TRequired, TNullable> {
|
|
11
|
+
protected readonly type = "function";
|
|
12
|
+
get _schema(): Schema;
|
|
13
|
+
clone(): this;
|
|
14
|
+
mapToType<T>(): IFunctionSchemaBuilder<TRequired, TNullable, T>;
|
|
15
|
+
static create<TRequired extends boolean = true, TNullable extends boolean = false, TMapToType = undefined>(obj?: {
|
|
16
|
+
isRequired: TRequired;
|
|
17
|
+
isNullable: TNullable;
|
|
18
|
+
validators?: Validator[];
|
|
19
|
+
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
20
|
+
}): IFunctionSchemaBuilder<TRequired, TNullable, TMapToType>;
|
|
21
|
+
private constructor();
|
|
22
|
+
optional(): IFunctionSchemaBuilder<false, TNullable, TMapToType>;
|
|
23
|
+
required(): IFunctionSchemaBuilder<true, TNullable, TMapToType>;
|
|
24
|
+
nullable(): IFunctionSchemaBuilder<TRequired, true, TMapToType>;
|
|
25
|
+
notNullable(): IFunctionSchemaBuilder<TRequired, false, TMapToType>;
|
|
26
|
+
}
|
|
27
|
+
export declare const func: () => IFunctionSchemaBuilder;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.func = exports.FunctionSchemaBuilder = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
+
class FunctionSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
+
type = 'function';
|
|
8
|
+
get _schema() {
|
|
9
|
+
return Object.assign({
|
|
10
|
+
type: this.type
|
|
11
|
+
}, this.getCommonSchema());
|
|
12
|
+
}
|
|
13
|
+
clone() {
|
|
14
|
+
return FunctionSchemaBuilder.create(this._schema);
|
|
15
|
+
}
|
|
16
|
+
mapToType() {
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
static create(obj) {
|
|
20
|
+
return new FunctionSchemaBuilder(obj);
|
|
21
|
+
}
|
|
22
|
+
constructor(obj) {
|
|
23
|
+
super(obj);
|
|
24
|
+
if (typeof obj !== 'object' || !obj) {
|
|
25
|
+
const defaultSchema = defaultSchemas_js_1.defaultSchemas['function'];
|
|
26
|
+
this.isRequired = defaultSchema.isRequired;
|
|
27
|
+
this.isNullable = defaultSchema.isNullable;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
optional() {
|
|
31
|
+
if (this.isRequired === false) {
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
return FunctionSchemaBuilder.create({
|
|
35
|
+
...this._schema,
|
|
36
|
+
isRequired: false
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
required() {
|
|
40
|
+
if (this.isRequired === true) {
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
return FunctionSchemaBuilder.create({
|
|
44
|
+
...this._schema,
|
|
45
|
+
isRequired: true
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
nullable() {
|
|
49
|
+
if (this.isNullable === true) {
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
return FunctionSchemaBuilder.create({
|
|
53
|
+
...this._schema,
|
|
54
|
+
isNullable: true
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
notNullable() {
|
|
58
|
+
if (this.isNullable === false) {
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
return FunctionSchemaBuilder.create({
|
|
62
|
+
...this._schema,
|
|
63
|
+
isNullable: false
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.FunctionSchemaBuilder = FunctionSchemaBuilder;
|
|
68
|
+
const func = () => FunctionSchemaBuilder.create();
|
|
69
|
+
exports.func = func;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
import { Schema, Validator } from '../schema.js';
|
|
3
|
+
export interface INumberSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMin extends number | undefined = undefined, TMax extends number | undefined = undefined, TIsInteger extends boolean | undefined = undefined, TEnsureNotNaN extends boolean = true, TEnsureIsFinite extends boolean = true, TEqualsTo extends number | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
|
|
4
|
+
min<K extends number>(val: K): INumberSchemaBuilder<TRequired, TNullable, K, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
5
|
+
clearMin(): INumberSchemaBuilder<TRequired, TNullable, undefined, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
6
|
+
max<K extends number>(val: K): INumberSchemaBuilder<TRequired, TNullable, TMin, K, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
7
|
+
clearMax(): INumberSchemaBuilder<TRequired, TNullable, TMin, undefined, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
8
|
+
optional(): INumberSchemaBuilder<false, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
9
|
+
required(): INumberSchemaBuilder<true, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
10
|
+
nullable(): INumberSchemaBuilder<TRequired, true, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
11
|
+
notNullable(): INumberSchemaBuilder<TRequired, false, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
12
|
+
equals<T extends number>(val: T): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, T>;
|
|
13
|
+
clearEquals(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, undefined>;
|
|
14
|
+
isInteger(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, true, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
15
|
+
canBeNotInteger(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, undefined, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
16
|
+
notNaN(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, true, TEnsureIsFinite, TEqualsTo>;
|
|
17
|
+
canBeNaN(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, false, TEnsureIsFinite, TEqualsTo>;
|
|
18
|
+
isFinite(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, true, TEqualsTo>;
|
|
19
|
+
canBeInfinite(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, false, TEqualsTo>;
|
|
20
|
+
}
|
|
21
|
+
export declare class NumberSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMin extends number | undefined = undefined, TMax extends number | undefined = undefined, TIsInteger extends boolean | undefined = undefined, TEnsureNotNaN extends boolean = true, TEnsureIsFinite extends boolean = true, TEqualsTo extends number | undefined = undefined> extends SchemaBuilder<TRequired, TNullable> implements INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo> {
|
|
22
|
+
clone(): this;
|
|
23
|
+
get _schema(): Schema;
|
|
24
|
+
static create<TRequired extends boolean = true, TNullable extends boolean = false, TMin extends number | undefined = undefined, TMax extends number | undefined = undefined, TIsInteger extends boolean | undefined = undefined, TEnsureNotNaN extends boolean = true, TEnsureIsFinite extends boolean = true, TEqualsTo extends number | undefined = undefined>(obj?: {
|
|
25
|
+
isRequired: TRequired;
|
|
26
|
+
isNullable: TNullable;
|
|
27
|
+
min: TMin;
|
|
28
|
+
max: TMax;
|
|
29
|
+
isInteger: TIsInteger;
|
|
30
|
+
ensureNotNaN: TEnsureNotNaN;
|
|
31
|
+
ensureIsFinite: TEnsureIsFinite;
|
|
32
|
+
equals: TEqualsTo;
|
|
33
|
+
validators?: Validator[];
|
|
34
|
+
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
35
|
+
}): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
36
|
+
private constructor();
|
|
37
|
+
protected readonly type = "number";
|
|
38
|
+
_min?: TMin;
|
|
39
|
+
_max?: TMax;
|
|
40
|
+
_isInteger?: TIsInteger;
|
|
41
|
+
_ensureNotNaN: TEnsureNotNaN;
|
|
42
|
+
_ensureIsFinite: TEnsureIsFinite;
|
|
43
|
+
_equals?: TEqualsTo;
|
|
44
|
+
min<K extends number>(val: K): INumberSchemaBuilder<TRequired, TNullable, K, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
45
|
+
clearMin(): INumberSchemaBuilder<TRequired, TNullable, undefined, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
46
|
+
max<K extends number>(val: K): INumberSchemaBuilder<TRequired, TNullable, TMin, K, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
47
|
+
clearMax(): INumberSchemaBuilder<TRequired, TNullable, TMin, undefined, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
48
|
+
optional(): INumberSchemaBuilder<false, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
49
|
+
required(): INumberSchemaBuilder<true, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
50
|
+
nullable(): INumberSchemaBuilder<TRequired, true, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
51
|
+
notNullable(): INumberSchemaBuilder<TRequired, false, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
52
|
+
equals<T extends number>(val: T): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, T>;
|
|
53
|
+
clearEquals(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, TEnsureIsFinite, undefined>;
|
|
54
|
+
isInteger(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, true, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
55
|
+
canBeNotInteger(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, undefined, TEnsureNotNaN, TEnsureIsFinite, TEqualsTo>;
|
|
56
|
+
notNaN(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, true, TEnsureIsFinite, TEqualsTo>;
|
|
57
|
+
canBeNaN(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, false, TEnsureIsFinite, TEqualsTo>;
|
|
58
|
+
isFinite(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, true, TEqualsTo>;
|
|
59
|
+
canBeInfinite(): INumberSchemaBuilder<TRequired, TNullable, TMin, TMax, TIsInteger, TEnsureNotNaN, false, TEqualsTo>;
|
|
60
|
+
}
|
|
61
|
+
export declare const number: () => INumberSchemaBuilder<true, false, undefined, undefined, undefined, true, true, undefined>;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.number = exports.NumberSchemaBuilder = void 0;
|
|
4
|
+
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
+
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
+
class NumberSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
+
clone() {
|
|
8
|
+
return NumberSchemaBuilder.create(this._schema);
|
|
9
|
+
}
|
|
10
|
+
get _schema() {
|
|
11
|
+
return Object.assign({
|
|
12
|
+
type: this.type,
|
|
13
|
+
ensureNotNaN: this._ensureNotNaN,
|
|
14
|
+
ensureIsFinite: this._ensureIsFinite
|
|
15
|
+
}, this.getCommonSchema(), typeof this._min !== 'undefined' ? { min: this._min } : {}, typeof this._max !== 'undefined' ? { max: this._max } : {}, typeof this._isInteger !== 'undefined'
|
|
16
|
+
? { isInteger: this._isInteger }
|
|
17
|
+
: {}, typeof this._equals !== 'undefined' ? { equals: this._equals } : {});
|
|
18
|
+
}
|
|
19
|
+
static create(obj) {
|
|
20
|
+
return new NumberSchemaBuilder(obj);
|
|
21
|
+
}
|
|
22
|
+
constructor(obj) {
|
|
23
|
+
super(obj);
|
|
24
|
+
if (typeof obj === 'object' && obj) {
|
|
25
|
+
if (typeof obj.min !== 'undefined') {
|
|
26
|
+
this._min = obj.min;
|
|
27
|
+
}
|
|
28
|
+
if (typeof obj.max !== 'undefined') {
|
|
29
|
+
this._max = obj.max;
|
|
30
|
+
}
|
|
31
|
+
if (typeof obj.isInteger !== 'undefined') {
|
|
32
|
+
this._isInteger = obj.isInteger;
|
|
33
|
+
}
|
|
34
|
+
if (typeof obj.ensureNotNaN !== 'undefined') {
|
|
35
|
+
this._ensureNotNaN = obj.ensureNotNaN;
|
|
36
|
+
}
|
|
37
|
+
if (typeof obj.ensureIsFinite !== 'undefined') {
|
|
38
|
+
this._ensureIsFinite = obj.ensureIsFinite;
|
|
39
|
+
}
|
|
40
|
+
if (typeof obj.equals !== 'undefined') {
|
|
41
|
+
this._equals = obj.equals;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const defaultSchema = defaultSchemas_js_1.defaultSchemas['number'];
|
|
46
|
+
this.isRequired = defaultSchema.isRequired;
|
|
47
|
+
this.isNullable = defaultSchema.isNullable;
|
|
48
|
+
this._ensureNotNaN = defaultSchema.ensureNotNaN;
|
|
49
|
+
this._ensureIsFinite = defaultSchema.ensureIsFinite;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
type = 'number';
|
|
53
|
+
_min;
|
|
54
|
+
_max;
|
|
55
|
+
_isInteger;
|
|
56
|
+
_ensureNotNaN = true;
|
|
57
|
+
_ensureIsFinite = true;
|
|
58
|
+
_equals;
|
|
59
|
+
min(val) {
|
|
60
|
+
if (this._min === val) {
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
return NumberSchemaBuilder.create({
|
|
64
|
+
...this._schema,
|
|
65
|
+
min: val
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
clearMin() {
|
|
69
|
+
if (typeof this._min === 'undefined') {
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
return NumberSchemaBuilder.create({
|
|
73
|
+
...this._schema,
|
|
74
|
+
min: undefined
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
max(val) {
|
|
78
|
+
if (this._max === val) {
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
return NumberSchemaBuilder.create({
|
|
82
|
+
...this._schema,
|
|
83
|
+
max: val
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
clearMax() {
|
|
87
|
+
if (typeof this._max === 'undefined') {
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
return NumberSchemaBuilder.create({
|
|
91
|
+
...this._schema,
|
|
92
|
+
max: undefined
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
optional() {
|
|
96
|
+
if (this.isRequired === false) {
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
return NumberSchemaBuilder.create({
|
|
100
|
+
...this._schema,
|
|
101
|
+
isRequired: false
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
required() {
|
|
105
|
+
if (this.isRequired === true) {
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
return NumberSchemaBuilder.create({
|
|
109
|
+
...this._schema,
|
|
110
|
+
isRequired: true
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
nullable() {
|
|
114
|
+
if (this.isNullable === true) {
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
return NumberSchemaBuilder.create({
|
|
118
|
+
...this._schema,
|
|
119
|
+
isNullable: true
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
notNullable() {
|
|
123
|
+
if (this.isNullable === false) {
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
return NumberSchemaBuilder.create({
|
|
127
|
+
...this._schema,
|
|
128
|
+
isNullable: false
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
equals(val) {
|
|
132
|
+
if (this._equals === val) {
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
return NumberSchemaBuilder.create({
|
|
136
|
+
...this._schema,
|
|
137
|
+
equals: val
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
clearEquals() {
|
|
141
|
+
if (typeof this._equals === 'undefined') {
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
return NumberSchemaBuilder.create({
|
|
145
|
+
...this._schema,
|
|
146
|
+
equals: undefined
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
isInteger() {
|
|
150
|
+
if (this._isInteger === true) {
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
return NumberSchemaBuilder.create({
|
|
154
|
+
...this._schema,
|
|
155
|
+
isInteger: true
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
canBeNotInteger() {
|
|
159
|
+
if (typeof this._isInteger === 'undefined') {
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
return NumberSchemaBuilder.create({
|
|
163
|
+
...this._schema,
|
|
164
|
+
isInteger: undefined
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
notNaN() {
|
|
168
|
+
if (this._ensureNotNaN === true) {
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
return NumberSchemaBuilder.create({
|
|
172
|
+
...this._schema,
|
|
173
|
+
ensureNotNaN: true
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
canBeNaN() {
|
|
177
|
+
if (this._ensureNotNaN === false) {
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
return NumberSchemaBuilder.create({
|
|
181
|
+
...this._schema,
|
|
182
|
+
ensureNotNaN: false
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
isFinite() {
|
|
186
|
+
if (this._ensureIsFinite === true) {
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
return NumberSchemaBuilder.create({
|
|
190
|
+
...this._schema,
|
|
191
|
+
ensureIsFinite: true
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
canBeInfinite() {
|
|
195
|
+
if (this._ensureIsFinite === false) {
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
return NumberSchemaBuilder.create({
|
|
199
|
+
...this._schema,
|
|
200
|
+
ensureIsFinite: false
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.NumberSchemaBuilder = NumberSchemaBuilder;
|
|
205
|
+
const number = () => NumberSchemaBuilder.create();
|
|
206
|
+
exports.number = number;
|