@cleverbrush/schema 1.0.0-beta.0 → 1.0.0-beta.2
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/package.json +2 -2
- package/src/builders/UnionSchemaBuilder.ts +1 -6
- package/src/index.ts +1 -13
- package/src/schema.ts +9 -104
- package/src/schemaRegistry.builders.test.ts +35 -80
- package/src/schemaRegistry.test.ts +2 -1916
- package/src/schemaRegistry.ts +55 -126
- package/dist/builders/AliasSchemaBuilder.d.ts +0 -14
- package/dist/builders/AliasSchemaBuilder.js +0 -91
- package/dist/builders/ArraySchemaBuilder.d.ts +0 -15
- package/dist/builders/ArraySchemaBuilder.js +0 -141
- package/dist/builders/BooleanSchemaBuilder.d.ts +0 -31
- package/dist/builders/BooleanSchemaBuilder.js +0 -90
- package/dist/builders/FunctionSchemaBuilder.d.ts +0 -25
- package/dist/builders/FunctionSchemaBuilder.js +0 -66
- package/dist/builders/NumberSchemaBuilder.d.ts +0 -61
- package/dist/builders/NumberSchemaBuilder.js +0 -206
- package/dist/builders/ObjectSchemaBuilder.d.ts +0 -83
- package/dist/builders/ObjectSchemaBuilder.js +0 -344
- package/dist/builders/SchemaBuilder.d.ts +0 -36
- package/dist/builders/SchemaBuilder.js +0 -88
- package/dist/builders/StringSchemaBuilder.d.ts +0 -14
- package/dist/builders/StringSchemaBuilder.js +0 -140
- package/dist/builders/UnionSchemaBuilder.d.ts +0 -10
- package/dist/builders/UnionSchemaBuilder.js +0 -100
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/index.d.ts +0 -42
- package/dist/index.js +0 -35
- package/dist/schema.d.ts +0 -242
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -54
- package/dist/schemaRegistry.js +0 -301
- 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/AliasSchemaBuilder.test.ts +0 -124
- package/src/builders/AliasSchemaBuilder.ts +0 -250
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.object = exports.ObjectSchemaBuilder = void 0;
|
|
4
|
-
const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
|
|
5
|
-
const defaultSchemas_js_1 = require("../defaultSchemas.js");
|
|
6
|
-
class ObjectSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
|
|
7
|
-
type = 'object';
|
|
8
|
-
noUnknownProperties;
|
|
9
|
-
properties;
|
|
10
|
-
preprocessors;
|
|
11
|
-
clone() {
|
|
12
|
-
return ObjectSchemaBuilder.create({
|
|
13
|
-
isNullable: this._isNullable,
|
|
14
|
-
isRequired: this._isRequired,
|
|
15
|
-
noUnknownProperties: this.noUnknownProperties,
|
|
16
|
-
properties: this.properties,
|
|
17
|
-
preprocessor: this.preprocessor,
|
|
18
|
-
preprocessors: this.preprocessors,
|
|
19
|
-
validators: this.validators
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
get _schema() {
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
24
|
-
const that = this;
|
|
25
|
-
return Object.assign({
|
|
26
|
-
type: this.type
|
|
27
|
-
}, this.getCommonSchema(), typeof that.properties !== 'undefined' && that.properties
|
|
28
|
-
? {
|
|
29
|
-
properties: Object.keys(that.properties).reduce((acc, curr) => {
|
|
30
|
-
if (!that.properties)
|
|
31
|
-
return;
|
|
32
|
-
if (that.properties[curr] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
33
|
-
acc[curr] = that.properties[curr].clone();
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
acc[curr] = that.properties[curr];
|
|
37
|
-
}
|
|
38
|
-
return acc;
|
|
39
|
-
}, {})
|
|
40
|
-
}
|
|
41
|
-
: {}, typeof that.noUnknownProperties !== 'undefined'
|
|
42
|
-
? { noUnknownProperties: that.noUnknownProperties }
|
|
43
|
-
: {}, typeof that.preprocessors !== 'undefined' && that.preprocessors
|
|
44
|
-
? {
|
|
45
|
-
preprocessors: {
|
|
46
|
-
...that.preprocessors
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
: {});
|
|
50
|
-
}
|
|
51
|
-
constructor(obj) {
|
|
52
|
-
super(obj);
|
|
53
|
-
if (typeof obj === 'object' && obj) {
|
|
54
|
-
if (typeof obj.properties !== 'undefined') {
|
|
55
|
-
this.properties = Object.keys(obj.properties).reduce((acc, curr) => {
|
|
56
|
-
if (obj.properties[curr] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
57
|
-
acc[curr] = obj.properties[curr].clone();
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
acc[curr] = {
|
|
61
|
-
...obj.properties[curr]
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
return acc;
|
|
65
|
-
}, {});
|
|
66
|
-
}
|
|
67
|
-
if (typeof obj.preprocessors !== 'undefined') {
|
|
68
|
-
this.preprocessors = { ...obj.preprocessors };
|
|
69
|
-
}
|
|
70
|
-
if (typeof obj.noUnknownProperties !== 'undefined') {
|
|
71
|
-
this.noUnknownProperties = obj.noUnknownProperties;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
const defaultSchema = defaultSchemas_js_1.defaultSchemas['object'];
|
|
76
|
-
this.isRequired = defaultSchema.isRequired;
|
|
77
|
-
this.isNullable = defaultSchema.isNullable;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
static create(obj) {
|
|
81
|
-
return new ObjectSchemaBuilder(obj);
|
|
82
|
-
}
|
|
83
|
-
canHaveUnknownProps() {
|
|
84
|
-
if (this.noUnknownProperties === false) {
|
|
85
|
-
return this;
|
|
86
|
-
}
|
|
87
|
-
return ObjectSchemaBuilder.create({
|
|
88
|
-
...this._schema,
|
|
89
|
-
noUnknownProperties: false
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
noUnknownProps() {
|
|
93
|
-
if (this.noUnknownProperties === true) {
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
return ObjectSchemaBuilder.create({
|
|
97
|
-
...this._schema,
|
|
98
|
-
noUnknownProperties: true
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
optional() {
|
|
102
|
-
if (this.isRequired === false) {
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
return ObjectSchemaBuilder.create({
|
|
106
|
-
...this._schema,
|
|
107
|
-
isRequired: false
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
required() {
|
|
111
|
-
if (this.isRequired === true) {
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
return ObjectSchemaBuilder.create({
|
|
115
|
-
...this._schema,
|
|
116
|
-
isRequired: true
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
nullable() {
|
|
120
|
-
if (this.isNullable === true) {
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
return ObjectSchemaBuilder.create({
|
|
124
|
-
...this._schema,
|
|
125
|
-
isNullable: true
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
notNullable() {
|
|
129
|
-
if (this.isNullable === false) {
|
|
130
|
-
return this;
|
|
131
|
-
}
|
|
132
|
-
return ObjectSchemaBuilder.create({
|
|
133
|
-
...this._schema,
|
|
134
|
-
isNullable: false
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
addProps(val) {
|
|
138
|
-
if (typeof val === 'undefined')
|
|
139
|
-
return this;
|
|
140
|
-
return ObjectSchemaBuilder.create({
|
|
141
|
-
...this._schema,
|
|
142
|
-
properties: {
|
|
143
|
-
...this.properties,
|
|
144
|
-
...val
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
removeProp(property) {
|
|
149
|
-
if (typeof this.properties === 'undefined') {
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
if (!(property in this.properties)) {
|
|
153
|
-
return this;
|
|
154
|
-
}
|
|
155
|
-
const newProperties = {
|
|
156
|
-
...this.properties
|
|
157
|
-
};
|
|
158
|
-
delete newProperties[property];
|
|
159
|
-
let newPreprocessors = this.preprocessors;
|
|
160
|
-
if (typeof newPreprocessors !== 'undefined' &&
|
|
161
|
-
property in newPreprocessors) {
|
|
162
|
-
if (Object.keys(newPreprocessors).length === 1) {
|
|
163
|
-
newPreprocessors = undefined;
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
newPreprocessors = {
|
|
167
|
-
...this.preprocessors
|
|
168
|
-
};
|
|
169
|
-
delete newPreprocessors[property];
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return ObjectSchemaBuilder.create({
|
|
173
|
-
...this._schema,
|
|
174
|
-
preprocessors: newPreprocessors,
|
|
175
|
-
properties: newProperties
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
setPropPreprocessor(property, validator) {
|
|
179
|
-
if (typeof this.preprocessors === 'undefined') {
|
|
180
|
-
this.preprocessors = {
|
|
181
|
-
[property]: validator
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
this.preprocessors = {
|
|
186
|
-
...this.preprocessors,
|
|
187
|
-
[property]: validator
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
return ObjectSchemaBuilder.create({
|
|
191
|
-
...this._schema,
|
|
192
|
-
preprocessors: {
|
|
193
|
-
...this.preprocessors
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
unsetPropPreprocessor(property) {
|
|
198
|
-
if (typeof this.preprocessors === 'undefined' ||
|
|
199
|
-
!(property in this.preprocessors)) {
|
|
200
|
-
return this;
|
|
201
|
-
}
|
|
202
|
-
let newPreprocessors;
|
|
203
|
-
if (Object.keys(this.preprocessors).length === 1) {
|
|
204
|
-
newPreprocessors = undefined;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
newPreprocessors = {
|
|
208
|
-
...this.preprocessors
|
|
209
|
-
};
|
|
210
|
-
delete newPreprocessors[property];
|
|
211
|
-
}
|
|
212
|
-
return ObjectSchemaBuilder.create({
|
|
213
|
-
...this._schema,
|
|
214
|
-
preprocessors: newPreprocessors
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
clearPropsPreprocessors() {
|
|
218
|
-
if (typeof this.preprocessors === 'undefined') {
|
|
219
|
-
return this;
|
|
220
|
-
}
|
|
221
|
-
return ObjectSchemaBuilder.create({
|
|
222
|
-
...this._schema,
|
|
223
|
-
preprocessors: undefined
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
mapToType() {
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
clearMapToType() {
|
|
230
|
-
return this;
|
|
231
|
-
}
|
|
232
|
-
makeAllPropsOptional() {
|
|
233
|
-
let res = this;
|
|
234
|
-
for (const prop in this.properties) {
|
|
235
|
-
res = res.makePropOptional(prop);
|
|
236
|
-
}
|
|
237
|
-
return res;
|
|
238
|
-
}
|
|
239
|
-
makePropOptional(property) {
|
|
240
|
-
if (typeof this.properties[property] === 'undefined')
|
|
241
|
-
throw new Error(`Property ${property} does not exists`);
|
|
242
|
-
if (this.properties[property] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
243
|
-
return ObjectSchemaBuilder.create({
|
|
244
|
-
...this._schema,
|
|
245
|
-
properties: {
|
|
246
|
-
...this.properties,
|
|
247
|
-
[property]: this.properties[property].optional()
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
return ObjectSchemaBuilder.create({
|
|
253
|
-
...this._schema,
|
|
254
|
-
properties: {
|
|
255
|
-
...this.properties,
|
|
256
|
-
[property]: {
|
|
257
|
-
...this.properties[property],
|
|
258
|
-
isRequired: false
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
makePropRequired(property) {
|
|
265
|
-
if (typeof this.properties[property] === 'undefined')
|
|
266
|
-
throw new Error(`Property ${property} does not exists`);
|
|
267
|
-
if (this.properties[property] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
268
|
-
return ObjectSchemaBuilder.create({
|
|
269
|
-
...this._schema,
|
|
270
|
-
properties: {
|
|
271
|
-
...this.properties,
|
|
272
|
-
[property]: this.properties[property].required()
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
return ObjectSchemaBuilder.create({
|
|
278
|
-
...this._schema,
|
|
279
|
-
properties: {
|
|
280
|
-
...this.properties,
|
|
281
|
-
[property]: {
|
|
282
|
-
...this.properties[property],
|
|
283
|
-
isRequired: true
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
makePropNullable(property) {
|
|
290
|
-
if (typeof this.properties[property] === 'undefined')
|
|
291
|
-
throw new Error(`Property ${property} does not exists`);
|
|
292
|
-
if (this.properties[property] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
293
|
-
return ObjectSchemaBuilder.create({
|
|
294
|
-
...this._schema,
|
|
295
|
-
properties: {
|
|
296
|
-
...this.properties,
|
|
297
|
-
[property]: this.properties[property].nullable()
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
return ObjectSchemaBuilder.create({
|
|
303
|
-
...this._schema,
|
|
304
|
-
properties: {
|
|
305
|
-
...this.properties,
|
|
306
|
-
[property]: {
|
|
307
|
-
...this.properties[property],
|
|
308
|
-
isNullable: true
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
makePropNotNullable(property) {
|
|
315
|
-
if (typeof this.properties[property] === 'undefined')
|
|
316
|
-
throw new Error(`Property ${property} does not exists`);
|
|
317
|
-
if (this.properties[property] instanceof SchemaBuilder_js_1.SchemaBuilder) {
|
|
318
|
-
return ObjectSchemaBuilder.create({
|
|
319
|
-
...this._schema,
|
|
320
|
-
properties: {
|
|
321
|
-
...this.properties,
|
|
322
|
-
[property]: this.properties[property].notNullable()
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
return ObjectSchemaBuilder.create({
|
|
328
|
-
...this._schema,
|
|
329
|
-
properties: {
|
|
330
|
-
...this.properties,
|
|
331
|
-
[property]: {
|
|
332
|
-
...this.properties[property],
|
|
333
|
-
isNullable: false
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
exports.ObjectSchemaBuilder = ObjectSchemaBuilder;
|
|
341
|
-
const object = (properties) => typeof properties === 'undefined'
|
|
342
|
-
? ObjectSchemaBuilder.create()
|
|
343
|
-
: ObjectSchemaBuilder.create().addProps(properties);
|
|
344
|
-
exports.object = object;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Schema, Validator } from '../schema.js';
|
|
2
|
-
export interface ISchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false> {
|
|
3
|
-
readonly _schema: Schema;
|
|
4
|
-
isRequired: TRequired;
|
|
5
|
-
isNullable: TNullable;
|
|
6
|
-
validators?: Validator[];
|
|
7
|
-
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
8
|
-
addValidator(validator: Validator): this;
|
|
9
|
-
clearValidators(): this;
|
|
10
|
-
addPreprocessor(preprocessor: ((value: unknown) => unknown | Promise<unknown>) | string): this;
|
|
11
|
-
clearPreprocessor(): this;
|
|
12
|
-
clone(): this;
|
|
13
|
-
}
|
|
14
|
-
export declare abstract class SchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false> implements ISchemaBuilder<TRequired, TNullable> {
|
|
15
|
-
protected _isRequired: TRequired;
|
|
16
|
-
protected _isNullable: TNullable;
|
|
17
|
-
validators?: Validator[];
|
|
18
|
-
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
19
|
-
abstract clone(): this;
|
|
20
|
-
abstract get _schema(): Schema;
|
|
21
|
-
protected constructor(obj: {
|
|
22
|
-
isRequired?: boolean;
|
|
23
|
-
isNullable?: boolean;
|
|
24
|
-
preprocessor?: ((value: unknown) => unknown | Promise<unknown>) | string;
|
|
25
|
-
validators?: Validator[];
|
|
26
|
-
});
|
|
27
|
-
protected getCommonSchema(): any;
|
|
28
|
-
addPreprocessor(preprocessor: ((value: unknown) => unknown | Promise<unknown>) | string): this;
|
|
29
|
-
clearPreprocessor(): this;
|
|
30
|
-
addValidator(validator: Validator): this;
|
|
31
|
-
clearValidators(): this;
|
|
32
|
-
get isRequired(): TRequired;
|
|
33
|
-
protected set isRequired(val: boolean);
|
|
34
|
-
get isNullable(): TNullable;
|
|
35
|
-
protected set isNullable(val: boolean);
|
|
36
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SchemaBuilder = void 0;
|
|
4
|
-
class SchemaBuilder {
|
|
5
|
-
_isRequired = true;
|
|
6
|
-
_isNullable = false;
|
|
7
|
-
validators;
|
|
8
|
-
preprocessor;
|
|
9
|
-
constructor(obj) {
|
|
10
|
-
if (typeof obj === 'object') {
|
|
11
|
-
if (typeof obj.isRequired !== 'undefined') {
|
|
12
|
-
this.isRequired = obj.isRequired;
|
|
13
|
-
}
|
|
14
|
-
if (typeof obj.isNullable !== 'undefined') {
|
|
15
|
-
this.isNullable = obj.isNullable;
|
|
16
|
-
}
|
|
17
|
-
if (Array.isArray(obj.validators)) {
|
|
18
|
-
this.validators = [...obj.validators];
|
|
19
|
-
}
|
|
20
|
-
if (typeof obj.preprocessor !== 'undefined') {
|
|
21
|
-
this.preprocessor = obj.preprocessor;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
getCommonSchema() {
|
|
26
|
-
const schemas = [];
|
|
27
|
-
if (typeof this.preprocessor !== 'undefined') {
|
|
28
|
-
schemas.push({
|
|
29
|
-
preprocessor: this.preprocessor
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (typeof this.isRequired !== 'undefined') {
|
|
33
|
-
schemas.push({ isRequired: this.isRequired });
|
|
34
|
-
}
|
|
35
|
-
if (typeof this.isNullable !== 'undefined') {
|
|
36
|
-
schemas.push({ isNullable: this.isNullable });
|
|
37
|
-
}
|
|
38
|
-
if (typeof this.validators !== 'undefined') {
|
|
39
|
-
schemas.push({ validators: [...this.validators] });
|
|
40
|
-
}
|
|
41
|
-
return Object.assign({}, ...schemas);
|
|
42
|
-
}
|
|
43
|
-
addPreprocessor(preprocessor) {
|
|
44
|
-
const result = this.clone();
|
|
45
|
-
result.preprocessor = preprocessor;
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
clearPreprocessor() {
|
|
49
|
-
if (typeof this.preprocessor === 'undefined') {
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
const result = this.clone();
|
|
53
|
-
delete result.preprocessor;
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
addValidator(validator) {
|
|
57
|
-
const result = this.clone();
|
|
58
|
-
if (typeof validator !== 'function')
|
|
59
|
-
throw new Error('validator should be a function');
|
|
60
|
-
if (Array.isArray(result.validators)) {
|
|
61
|
-
result.validators.push(validator);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
result.validators = [validator];
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
clearValidators() {
|
|
69
|
-
if (typeof this.validators === 'undefined')
|
|
70
|
-
return this;
|
|
71
|
-
const result = this.clone();
|
|
72
|
-
delete result.validators;
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
get isRequired() {
|
|
76
|
-
return this._isRequired;
|
|
77
|
-
}
|
|
78
|
-
set isRequired(val) {
|
|
79
|
-
this._isRequired = val;
|
|
80
|
-
}
|
|
81
|
-
get isNullable() {
|
|
82
|
-
return this._isNullable;
|
|
83
|
-
}
|
|
84
|
-
set isNullable(val) {
|
|
85
|
-
this._isNullable = val;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.SchemaBuilder = SchemaBuilder;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ISchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
-
export interface IStringSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMinLength extends number | undefined = undefined, TMaxLength extends number | undefined = undefined, TEqualsTo extends string | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
|
|
3
|
-
optional(): IStringSchemaBuilder<false, TNullable, TMinLength, TMaxLength, TEqualsTo>;
|
|
4
|
-
required(): IStringSchemaBuilder<true, TNullable, TMinLength, TMaxLength, TEqualsTo>;
|
|
5
|
-
nullable(): IStringSchemaBuilder<TRequired, true, TMinLength, TMaxLength, TEqualsTo>;
|
|
6
|
-
notNullable(): IStringSchemaBuilder<TRequired, false, TMinLength, TMaxLength, TEqualsTo>;
|
|
7
|
-
minLength<K extends number>(val: K): IStringSchemaBuilder<TRequired, TNullable, K, TMaxLength, TEqualsTo>;
|
|
8
|
-
clearMinLength(): IStringSchemaBuilder<TRequired, TNullable, undefined, TMaxLength, TEqualsTo>;
|
|
9
|
-
maxLength<K extends number>(val: K): IStringSchemaBuilder<TRequired, TNullable, TMinLength, K, TEqualsTo>;
|
|
10
|
-
clearMaxLength(): IStringSchemaBuilder<TRequired, TNullable, TMinLength, undefined, TEqualsTo>;
|
|
11
|
-
equals<T extends string>(val: T): IStringSchemaBuilder<TRequired, TNullable, TMinLength, TMaxLength, T>;
|
|
12
|
-
clearEquals(): IStringSchemaBuilder<TRequired, TNullable, TMinLength, TMaxLength, undefined>;
|
|
13
|
-
}
|
|
14
|
-
export declare const string: <T extends string = undefined>(equals?: T) => IStringSchemaBuilder<true, false, undefined, undefined, T extends undefined ? undefined : T>;
|
|
@@ -1,140 +0,0 @@
|
|
|
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;
|
|
@@ -1,10 +0,0 @@
|
|
|
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>;
|