@cleverbrush/schema 1.0.0-beta.4 → 1.0.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 +118 -320
- package/dist/builders/AnySchemaBuilder.d.ts +76 -0
- package/dist/builders/AnySchemaBuilder.js +112 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
- package/dist/builders/ArraySchemaBuilder.js +254 -111
- package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
- package/dist/builders/BooleanSchemaBuilder.js +134 -74
- package/dist/builders/DateSchemaBuilder.d.ts +177 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -23
- package/dist/builders/FunctionSchemaBuilder.js +103 -56
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +357 -81
- package/dist/builders/ObjectSchemaBuilder.js +519 -283
- package/dist/builders/SchemaBuilder.d.ts +158 -34
- package/dist/builders/SchemaBuilder.js +258 -71
- package/dist/builders/StringSchemaBuilder.d.ts +138 -13
- package/dist/builders/StringSchemaBuilder.js +261 -115
- package/dist/builders/UnionSchemaBuilder.d.ts +129 -9
- package/dist/builders/UnionSchemaBuilder.js +196 -75
- package/dist/index.d.ts +20 -40
- package/dist/index.js +19 -32
- package/dist/utils/transaction.d.ts +46 -0
- package/dist/utils/transaction.js +178 -0
- package/package.json +27 -14
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/schema.d.ts +0 -230
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -49
- package/dist/schemaRegistry.js +0 -278
- package/dist/validators/validateArray.d.ts +0 -3
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -30
- package/dist/validators/validateFunction.d.ts +0 -2
- package/dist/validators/validateFunction.js +0 -21
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -3
- package/dist/validators/validateObject.js +0 -95
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/dist/validators/validateUnion.d.ts +0 -3
- package/dist/validators/validateUnion.js +0 -30
- package/src/builders/ArraySchemaBuilder.test.ts +0 -270
- package/src/builders/ArraySchemaBuilder.ts +0 -381
- package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
- package/src/builders/BooleanSchemaBuilder.ts +0 -155
- package/src/builders/FunctionSchemaBuilder.test.ts +0 -134
- package/src/builders/FunctionSchemaBuilder.ts +0 -109
- package/src/builders/NumberSchemaBuilder.test.ts +0 -493
- package/src/builders/NumberSchemaBuilder.ts +0 -789
- package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
- package/src/builders/ObjectSchemaBuilder.ts +0 -794
- package/src/builders/SchemaBuilder.test.ts +0 -73
- package/src/builders/SchemaBuilder.ts +0 -135
- package/src/builders/StringSchemaBuilder.test.ts +0 -318
- package/src/builders/StringSchemaBuilder.ts +0 -392
- package/src/builders/UnionSchemaBuilder.test.ts +0 -162
- package/src/builders/UnionSchemaBuilder.ts +0 -154
- package/src/defaultSchemas.ts +0 -44
- package/src/index.ts +0 -66
- package/src/schema.ts +0 -827
- package/src/schemaRegistry.builders.test.ts +0 -1393
- package/src/schemaRegistry.test.ts +0 -118
- package/src/schemaRegistry.ts +0 -461
- package/src/validators/validateArray.ts +0 -76
- package/src/validators/validateBoolean.ts +0 -36
- package/src/validators/validateFunction.ts +0 -25
- package/src/validators/validateNumber.ts +0 -82
- package/src/validators/validateObject.ts +0 -119
- package/src/validators/validateString.ts +0 -59
- package/src/validators/validateUnion.ts +0 -36
- package/tsconfig.json +0 -16
|
@@ -1,14 +1,139 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { Preprocessor, SchemaBuilder, ValidationResult, ValidationContext, Validator } from './SchemaBuilder.js';
|
|
2
|
+
type StringSchemaBuilderCreateProps<T = string, R extends boolean = true> = Partial<ReturnType<StringSchemaBuilder<T, R>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Allows to define a schema for a string. It can be: required or optional,
|
|
5
|
+
* restricted to be equal to a certain value, restricted to have a certain
|
|
6
|
+
* length.
|
|
7
|
+
*
|
|
8
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
9
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
10
|
+
* directly. Use {@link string | string()} function instead.
|
|
11
|
+
*
|
|
12
|
+
* @example ```ts
|
|
13
|
+
* const schema = string().equals('hello');
|
|
14
|
+
* const result = await schema.validate('hello');
|
|
15
|
+
* // result.valid === true
|
|
16
|
+
* // result.object === 'hello'
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example ```ts
|
|
20
|
+
* const schema = string().equals('hello');
|
|
21
|
+
* const result = await schema.validate('world');
|
|
22
|
+
* // result.valid === false
|
|
23
|
+
* // result.errors[0].message === "is expected to be equal to 'hello'"
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example ```ts
|
|
27
|
+
* const schema = string().minLength(5);
|
|
28
|
+
* const result = await schema.validate('hello');
|
|
29
|
+
* // result.valid === true
|
|
30
|
+
* // result.object === 'hello'
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example ```ts
|
|
34
|
+
* const schema = string().minLength(5);
|
|
35
|
+
* const result = await schema.validate('hi');
|
|
36
|
+
* // result.valid === false
|
|
37
|
+
* // result.errors[0].message === 'is expected to have a length of at least 5'
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example ```ts
|
|
41
|
+
* const schema = string().minLength(2).maxLength(5);
|
|
42
|
+
* const result = await schema.validate('yes');
|
|
43
|
+
* // result.valid === true
|
|
44
|
+
* // result.object === 'yes'
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example ```ts
|
|
48
|
+
* const schema = string('no');
|
|
49
|
+
* const result = await schema.validate('yes');
|
|
50
|
+
* // result.valid === false
|
|
51
|
+
* // result.errors[0].message === "is expected to be equal to 'no'"
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @see {@link string}
|
|
55
|
+
*/
|
|
56
|
+
export declare class StringSchemaBuilder<TResult = string, TRequired extends boolean = true> extends SchemaBuilder<TResult, TRequired> {
|
|
57
|
+
#private;
|
|
58
|
+
static create(props: StringSchemaBuilderCreateProps): StringSchemaBuilder<string, true>;
|
|
59
|
+
private constructor();
|
|
60
|
+
introspect(): {
|
|
61
|
+
/**
|
|
62
|
+
* Min length of the string (if defined).
|
|
63
|
+
*/
|
|
64
|
+
minLength: number | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Max length of the string (if defined).
|
|
67
|
+
*/
|
|
68
|
+
maxLength: number | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* If set, restrict object to be equal to a certain value.
|
|
71
|
+
*/
|
|
72
|
+
equalsTo: string | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Array of preprocessor functions
|
|
75
|
+
*/
|
|
76
|
+
preprocessors: Preprocessor<TResult>[];
|
|
77
|
+
/**
|
|
78
|
+
* Array of validator functions
|
|
79
|
+
*/
|
|
80
|
+
validators: Validator<TResult>[];
|
|
81
|
+
type: string;
|
|
82
|
+
isRequired: boolean;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* @hidden
|
|
86
|
+
*/
|
|
87
|
+
hasType<T>(notUsed?: T): StringSchemaBuilder<T, true>;
|
|
88
|
+
/**
|
|
89
|
+
* @hidden
|
|
90
|
+
*/
|
|
91
|
+
clearHasType(): StringSchemaBuilder<string, TRequired>;
|
|
92
|
+
/**
|
|
93
|
+
* Performs validion of string schema over `object`.
|
|
94
|
+
* @param context Optional `ValidationContext` settings.
|
|
95
|
+
*/
|
|
96
|
+
validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
97
|
+
protected createFromProps<T, TReq extends boolean>(props: StringSchemaBuilderCreateProps<T, TReq>): this;
|
|
98
|
+
/**
|
|
99
|
+
* Restricts object to be equal to `value`.
|
|
100
|
+
*/
|
|
101
|
+
equals<T extends string>(value: T): StringSchemaBuilder<T, TRequired>;
|
|
102
|
+
/**
|
|
103
|
+
* Cancels `equals()` call.
|
|
104
|
+
*/
|
|
105
|
+
clearEquals(): StringSchemaBuilder<string, TRequired>;
|
|
106
|
+
/**
|
|
107
|
+
* @hidden
|
|
108
|
+
*/
|
|
109
|
+
required(): StringSchemaBuilder<TResult, true>;
|
|
110
|
+
/**
|
|
111
|
+
* @hidden
|
|
112
|
+
*/
|
|
113
|
+
optional(): StringSchemaBuilder<TResult, false>;
|
|
114
|
+
/**
|
|
115
|
+
* Set minimal length of the valid value for schema.
|
|
116
|
+
* @param {number} length
|
|
117
|
+
*/
|
|
118
|
+
minLength(length: number): StringSchemaBuilder<TResult, TRequired>;
|
|
119
|
+
/**
|
|
120
|
+
* Cancel `minLength()` call.
|
|
121
|
+
*/
|
|
122
|
+
clearMinLength(): StringSchemaBuilder<TResult, TRequired>;
|
|
123
|
+
/**
|
|
124
|
+
* Set maximal length of the valid value for schema.
|
|
125
|
+
* @length {number} length
|
|
126
|
+
*/
|
|
127
|
+
maxLength(length: number): StringSchemaBuilder<TResult, TRequired>;
|
|
128
|
+
/**
|
|
129
|
+
* cancel `maxLength()` call.
|
|
130
|
+
*/
|
|
131
|
+
clearMaxLength(): StringSchemaBuilder<TResult, TRequired>;
|
|
13
132
|
}
|
|
14
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Creates a string schema restricted to be equal to `equals`.
|
|
135
|
+
* @param equals number value
|
|
136
|
+
*/
|
|
137
|
+
export declare function string<T extends string>(equals: T): StringSchemaBuilder<T, true>;
|
|
138
|
+
export declare function string(): StringSchemaBuilder<string, true>;
|
|
139
|
+
export {};
|
|
@@ -1,140 +1,286 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
/**
|
|
3
|
+
* Allows to define a schema for a string. It can be: required or optional,
|
|
4
|
+
* restricted to be equal to a certain value, restricted to have a certain
|
|
5
|
+
* length.
|
|
6
|
+
*
|
|
7
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
8
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
9
|
+
* directly. Use {@link string | string()} function instead.
|
|
10
|
+
*
|
|
11
|
+
* @example ```ts
|
|
12
|
+
* const schema = string().equals('hello');
|
|
13
|
+
* const result = await schema.validate('hello');
|
|
14
|
+
* // result.valid === true
|
|
15
|
+
* // result.object === 'hello'
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example ```ts
|
|
19
|
+
* const schema = string().equals('hello');
|
|
20
|
+
* const result = await schema.validate('world');
|
|
21
|
+
* // result.valid === false
|
|
22
|
+
* // result.errors[0].message === "is expected to be equal to 'hello'"
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example ```ts
|
|
26
|
+
* const schema = string().minLength(5);
|
|
27
|
+
* const result = await schema.validate('hello');
|
|
28
|
+
* // result.valid === true
|
|
29
|
+
* // result.object === 'hello'
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example ```ts
|
|
33
|
+
* const schema = string().minLength(5);
|
|
34
|
+
* const result = await schema.validate('hi');
|
|
35
|
+
* // result.valid === false
|
|
36
|
+
* // result.errors[0].message === 'is expected to have a length of at least 5'
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example ```ts
|
|
40
|
+
* const schema = string().minLength(2).maxLength(5);
|
|
41
|
+
* const result = await schema.validate('yes');
|
|
42
|
+
* // result.valid === true
|
|
43
|
+
* // result.object === 'yes'
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example ```ts
|
|
47
|
+
* const schema = string('no');
|
|
48
|
+
* const result = await schema.validate('yes');
|
|
49
|
+
* // result.valid === false
|
|
50
|
+
* // result.errors[0].message === "is expected to be equal to 'no'"
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see {@link string}
|
|
54
|
+
*/
|
|
55
|
+
export class StringSchemaBuilder extends SchemaBuilder {
|
|
56
|
+
#minLength;
|
|
57
|
+
#maxLength;
|
|
58
|
+
#equalsTo;
|
|
59
|
+
static create(props) {
|
|
60
|
+
return new StringSchemaBuilder({
|
|
61
|
+
type: 'string',
|
|
62
|
+
...props
|
|
63
|
+
});
|
|
22
64
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
65
|
+
constructor(props) {
|
|
66
|
+
super(props);
|
|
67
|
+
if (typeof props.minLength === 'number') {
|
|
68
|
+
this.#minLength = props.minLength;
|
|
26
69
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
70
|
+
if (typeof props.maxLength === 'number') {
|
|
71
|
+
this.#maxLength = props.maxLength;
|
|
72
|
+
}
|
|
73
|
+
if (typeof props.equalsTo === 'string' ||
|
|
74
|
+
typeof props.equalsTo === 'undefined') {
|
|
75
|
+
this.#equalsTo = props.equalsTo;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
introspect() {
|
|
79
|
+
return {
|
|
80
|
+
...super.introspect(),
|
|
81
|
+
/**
|
|
82
|
+
* Min length of the string (if defined).
|
|
83
|
+
*/
|
|
84
|
+
minLength: this.#minLength,
|
|
85
|
+
/**
|
|
86
|
+
* Max length of the string (if defined).
|
|
87
|
+
*/
|
|
88
|
+
maxLength: this.#maxLength,
|
|
89
|
+
/**
|
|
90
|
+
* If set, restrict object to be equal to a certain value.
|
|
91
|
+
*/
|
|
92
|
+
equalsTo: this.#equalsTo,
|
|
93
|
+
/**
|
|
94
|
+
* Array of preprocessor functions
|
|
95
|
+
*/
|
|
96
|
+
preprocessors: this.preprocessors,
|
|
97
|
+
/**
|
|
98
|
+
* Array of validator functions
|
|
99
|
+
*/
|
|
100
|
+
validators: this.validators
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @hidden
|
|
105
|
+
*/
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
107
|
+
hasType(notUsed) {
|
|
108
|
+
return this.createFromProps({
|
|
109
|
+
...this.introspect()
|
|
30
110
|
});
|
|
31
111
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return
|
|
37
|
-
...this.
|
|
38
|
-
isRequired: true
|
|
112
|
+
/**
|
|
113
|
+
* @hidden
|
|
114
|
+
*/
|
|
115
|
+
clearHasType() {
|
|
116
|
+
return this.createFromProps({
|
|
117
|
+
...this.introspect()
|
|
39
118
|
});
|
|
40
119
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Performs validion of string schema over `object`.
|
|
122
|
+
* @param context Optional `ValidationContext` settings.
|
|
123
|
+
*/
|
|
124
|
+
async validate(object, context) {
|
|
125
|
+
const superResult = await super.preValidate(object, context);
|
|
126
|
+
const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
|
|
127
|
+
const { path } = prevalidationContext;
|
|
128
|
+
if (!valid) {
|
|
129
|
+
return {
|
|
130
|
+
valid,
|
|
131
|
+
errors
|
|
132
|
+
};
|
|
44
133
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
134
|
+
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
135
|
+
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
136
|
+
this.isRequired === false) {
|
|
137
|
+
return {
|
|
138
|
+
valid: true,
|
|
139
|
+
object: objToValidate
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (typeof objToValidate !== 'string')
|
|
143
|
+
return {
|
|
144
|
+
valid: false,
|
|
145
|
+
errors: [
|
|
146
|
+
{
|
|
147
|
+
message: `expected type string, but saw ${typeof objToValidate}`,
|
|
148
|
+
path: path
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
};
|
|
152
|
+
if (typeof this.#equalsTo !== 'undefined' &&
|
|
153
|
+
objToValidate !== this.#equalsTo) {
|
|
154
|
+
return {
|
|
155
|
+
valid: false,
|
|
156
|
+
errors: [
|
|
157
|
+
{
|
|
158
|
+
message: `is expected to be equal to ${this.#equalsTo}`,
|
|
159
|
+
path: path
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (typeof this.#minLength !== 'undefined') {
|
|
165
|
+
if (objToValidate.length < this.#minLength)
|
|
166
|
+
return {
|
|
167
|
+
valid: false,
|
|
168
|
+
errors: [
|
|
169
|
+
{
|
|
170
|
+
message: `expected to has at least ${this.#minLength} characters length`,
|
|
171
|
+
path: path
|
|
172
|
+
}
|
|
173
|
+
]
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (typeof this.#maxLength !== 'undefined') {
|
|
177
|
+
if (objToValidate.length > this.#maxLength)
|
|
178
|
+
return {
|
|
179
|
+
valid: false,
|
|
180
|
+
errors: [
|
|
181
|
+
{
|
|
182
|
+
message: `must not exceed ${this.#maxLength} characters`,
|
|
183
|
+
path: path
|
|
184
|
+
}
|
|
185
|
+
]
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
valid: true,
|
|
190
|
+
object: objToValidate
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
createFromProps(props) {
|
|
194
|
+
return StringSchemaBuilder.create(props);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Restricts object to be equal to `value`.
|
|
198
|
+
*/
|
|
199
|
+
equals(value) {
|
|
200
|
+
if (typeof value !== 'string')
|
|
201
|
+
throw new Error('string expected');
|
|
202
|
+
return this.createFromProps({
|
|
203
|
+
...this.introspect(),
|
|
204
|
+
equalsTo: value
|
|
48
205
|
});
|
|
49
206
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return
|
|
55
|
-
...this.
|
|
56
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Cancels `equals()` call.
|
|
209
|
+
*/
|
|
210
|
+
clearEquals() {
|
|
211
|
+
return this.createFromProps({
|
|
212
|
+
...this.introspect(),
|
|
213
|
+
equalsTo: undefined
|
|
57
214
|
});
|
|
58
215
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
216
|
+
/**
|
|
217
|
+
* @hidden
|
|
218
|
+
*/
|
|
219
|
+
required() {
|
|
220
|
+
return super.required();
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* @hidden
|
|
224
|
+
*/
|
|
225
|
+
optional() {
|
|
226
|
+
return super.optional();
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Set minimal length of the valid value for schema.
|
|
230
|
+
* @param {number} length
|
|
231
|
+
*/
|
|
232
|
+
minLength(length) {
|
|
233
|
+
if (typeof length !== 'number')
|
|
234
|
+
throw new Error('length must be a number');
|
|
235
|
+
return this.createFromProps({
|
|
236
|
+
...this.introspect(),
|
|
237
|
+
minLength: length
|
|
66
238
|
});
|
|
67
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Cancel `minLength()` call.
|
|
242
|
+
*/
|
|
68
243
|
clearMinLength() {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
...this._schema,
|
|
74
|
-
minLength: undefined
|
|
244
|
+
const schema = this.introspect();
|
|
245
|
+
delete schema.minLength;
|
|
246
|
+
return this.createFromProps({
|
|
247
|
+
...schema
|
|
75
248
|
});
|
|
76
249
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Set maximal length of the valid value for schema.
|
|
252
|
+
* @length {number} length
|
|
253
|
+
*/
|
|
254
|
+
maxLength(length) {
|
|
255
|
+
if (typeof length !== 'number')
|
|
256
|
+
throw new Error('length must be a number');
|
|
257
|
+
return this.createFromProps({
|
|
258
|
+
...this.introspect(),
|
|
259
|
+
maxLength: length
|
|
84
260
|
});
|
|
85
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* cancel `maxLength()` call.
|
|
264
|
+
*/
|
|
86
265
|
clearMaxLength() {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
...this._schema,
|
|
92
|
-
maxLength: undefined
|
|
266
|
+
const schema = this.introspect();
|
|
267
|
+
delete schema.maxLength;
|
|
268
|
+
return this.createFromProps({
|
|
269
|
+
...schema
|
|
93
270
|
});
|
|
94
271
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
equals: val
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
clearEquals() {
|
|
105
|
-
if (typeof this._equals === 'undefined') {
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Creates a string schema.
|
|
275
|
+
*/
|
|
276
|
+
export function string(equals) {
|
|
277
|
+
if (typeof equals === 'string') {
|
|
108
278
|
return StringSchemaBuilder.create({
|
|
109
|
-
|
|
110
|
-
|
|
279
|
+
isRequired: true,
|
|
280
|
+
equalsTo: equals
|
|
111
281
|
});
|
|
112
282
|
}
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
}
|
|
283
|
+
return StringSchemaBuilder.create({
|
|
284
|
+
isRequired: true
|
|
285
|
+
});
|
|
135
286
|
}
|
|
136
|
-
const string = (equals) => {
|
|
137
|
-
const res = StringSchemaBuilder.create();
|
|
138
|
-
return typeof equals === 'string' ? res.equals(equals) : res;
|
|
139
|
-
};
|
|
140
|
-
exports.string = string;
|