@cleverbrush/schema 0.0.17 → 1.0.0-beta.1
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 +213 -163
- package/package.json +3 -3
- 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 +154 -0
- package/src/defaultSchemas.ts +44 -0
- package/src/index.ts +58 -190
- package/src/schema.ts +827 -0
- package/src/schemaRegistry.builders.test.ts +1393 -0
- package/src/schemaRegistry.test.ts +118 -0
- package/src/schemaRegistry.ts +461 -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/index.d.ts +0 -94
- package/dist/index.js +0 -9
- package/dist/schemaValidator.d.ts +0 -16
- package/dist/schemaValidator.js +0 -234
- package/dist/validators/validateArray.d.ts +0 -2
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -33
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -2
- package/dist/validators/validateObject.js +0 -73
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/src/schemaValidator.test.ts +0 -1656
- package/src/schemaValidator.ts +0 -367
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Schema } from '../schema.js';
|
|
2
|
+
import { array } from './ArraySchemaBuilder.js';
|
|
3
|
+
import { number } from './NumberSchemaBuilder.js';
|
|
4
|
+
|
|
5
|
+
test('Clean', () => {
|
|
6
|
+
const schema = array();
|
|
7
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
8
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
9
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('optional', () => {
|
|
13
|
+
const schema = array().optional();
|
|
14
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
15
|
+
expect(schema).toHaveProperty('isRequired', false);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('optional - 2', () => {
|
|
19
|
+
const schema = array().optional().required();
|
|
20
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
21
|
+
expect(schema).toHaveProperty('isRequired', true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('optional - 3', () => {
|
|
25
|
+
const schema1 = array();
|
|
26
|
+
const schema2 = schema1.optional();
|
|
27
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
28
|
+
const isRequiredEqual = (schema1 as any).isRequired !== schema2.isRequired;
|
|
29
|
+
expect(e).toEqual(false);
|
|
30
|
+
expect(isRequiredEqual).toEqual(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('optional - 4', () => {
|
|
34
|
+
const schema1 = array().optional();
|
|
35
|
+
const schema2 = schema1.optional();
|
|
36
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
37
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
38
|
+
expect(e).toEqual(true);
|
|
39
|
+
expect(isRequiredEqual).toEqual(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('optional - 5', () => {
|
|
43
|
+
const schema1 = array();
|
|
44
|
+
const schema2 = schema1.required();
|
|
45
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
46
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
47
|
+
expect(e).toEqual(true);
|
|
48
|
+
expect(isRequiredEqual).toEqual(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('optional - 6', () => {
|
|
52
|
+
const schema1 = array().optional();
|
|
53
|
+
const schema2 = schema1.required();
|
|
54
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
55
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
56
|
+
expect(e).toEqual(false);
|
|
57
|
+
expect(isRequiredEqual).toEqual(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('optional - 7', () => {
|
|
61
|
+
const schema1 = array().optional().required();
|
|
62
|
+
const schema2 = schema1.required();
|
|
63
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
64
|
+
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
65
|
+
expect(e).toEqual(true);
|
|
66
|
+
expect(isRequiredEqual).toEqual(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('nullable', () => {
|
|
70
|
+
const schema = array().nullable();
|
|
71
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
72
|
+
expect(schema).toHaveProperty('isNullable', true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('nullable - 2', () => {
|
|
76
|
+
const schema = array().nullable().notNullable();
|
|
77
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
78
|
+
expect(schema).toHaveProperty('isNullable', false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('nullable - 3', () => {
|
|
82
|
+
const schema1 = array();
|
|
83
|
+
const schema2 = schema1.nullable();
|
|
84
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
85
|
+
const isNullableEqual = (schema1 as any).isNullable !== schema2.isNullable;
|
|
86
|
+
expect(e).toEqual(false);
|
|
87
|
+
expect(isNullableEqual).toEqual(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('nullable - 4', () => {
|
|
91
|
+
const schema1 = array().nullable();
|
|
92
|
+
const schema2 = schema1.nullable();
|
|
93
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
94
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
95
|
+
expect(e).toEqual(true);
|
|
96
|
+
expect(isNullableEqual).toEqual(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('nullable - 5', () => {
|
|
100
|
+
const schema1 = array();
|
|
101
|
+
const schema2 = schema1.nullable();
|
|
102
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
103
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
104
|
+
expect(e).toEqual(false);
|
|
105
|
+
expect(isNullableEqual).toEqual(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('nullable - 6', () => {
|
|
109
|
+
const schema1 = array();
|
|
110
|
+
const schema2 = schema1.notNullable();
|
|
111
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
112
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
113
|
+
expect(e).toEqual(true);
|
|
114
|
+
expect(isNullableEqual).toEqual(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('nullable - 7', () => {
|
|
118
|
+
const schema1 = array().nullable();
|
|
119
|
+
const schema2 = schema1.notNullable();
|
|
120
|
+
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
121
|
+
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
122
|
+
expect(e).toEqual(false);
|
|
123
|
+
expect(isNullableEqual).toEqual(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('ofType - 1', () => {
|
|
127
|
+
const s: Schema = {
|
|
128
|
+
type: 'number'
|
|
129
|
+
};
|
|
130
|
+
const schema = array().ofType(s)._schema;
|
|
131
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
132
|
+
expect(schema).toHaveProperty('ofType', s);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('ofType - 2', () => {
|
|
136
|
+
const s: Schema = {
|
|
137
|
+
type: 'array'
|
|
138
|
+
};
|
|
139
|
+
const schema = array().ofType(s).clearOfType()._schema;
|
|
140
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
141
|
+
expect(schema).not.toHaveProperty('ofType');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('ofType - 3', () => {
|
|
145
|
+
const schema1 = array();
|
|
146
|
+
const schema2 = schema1.ofType(number());
|
|
147
|
+
const equal = (schema1 as any) === schema2;
|
|
148
|
+
expect(equal).toEqual(false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('ofType - 4', () => {
|
|
152
|
+
const schema1 = array().ofType(number());
|
|
153
|
+
const schema2 = schema1.ofType(number());
|
|
154
|
+
const equal = (schema1 as any) === schema2;
|
|
155
|
+
expect(equal).toEqual(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('ofType - 5', () => {
|
|
159
|
+
const schema1 = array().ofType(number());
|
|
160
|
+
const schema2 = schema1.clone();
|
|
161
|
+
const equal = schema1._schema.ofType === schema2._schema.ofType;
|
|
162
|
+
expect(equal).toEqual(false);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('ofType - 6', () => {
|
|
166
|
+
const schema1 = array().ofType(number());
|
|
167
|
+
const schema2 = schema1.clearOfType();
|
|
168
|
+
const equal = (schema1 as any) === schema2;
|
|
169
|
+
expect(equal).toEqual(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('ofType - 7', () => {
|
|
173
|
+
const schema1 = array().ofType(number()).clearOfType();
|
|
174
|
+
const schema2 = schema1.clearOfType();
|
|
175
|
+
const equal = (schema1 as any) === schema2;
|
|
176
|
+
expect(equal).toEqual(true);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('maxLength - 1', () => {
|
|
180
|
+
const schema = array()._schema;
|
|
181
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
182
|
+
expect(schema).not.toHaveProperty('maxLength');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('maxLength - 2', () => {
|
|
186
|
+
const s: Schema = {
|
|
187
|
+
type: 'array'
|
|
188
|
+
};
|
|
189
|
+
const schema = array().ofType(s).maxLength(10)._schema;
|
|
190
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
191
|
+
expect(schema).toHaveProperty('maxLength', 10);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('maxLength - 3', () => {
|
|
195
|
+
const s: Schema = {
|
|
196
|
+
type: 'array'
|
|
197
|
+
};
|
|
198
|
+
const schema = array().ofType(s).maxLength(10).clearMaxLength()._schema;
|
|
199
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
200
|
+
expect(schema).not.toHaveProperty('maxLength');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('maxLength - 4', () => {
|
|
204
|
+
const schema1 = array().maxLength(10).clearMaxLength();
|
|
205
|
+
const schema2 = schema1.clearMaxLength();
|
|
206
|
+
const equal = (schema1 as any) === schema2;
|
|
207
|
+
expect(equal).toEqual(true);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('maxLength - 5', () => {
|
|
211
|
+
const schema1 = array().maxLength(20);
|
|
212
|
+
const schema2 = schema1.clearMaxLength();
|
|
213
|
+
const equal = (schema1 as any) === schema2;
|
|
214
|
+
expect(equal).toEqual(false);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('minLength - 1', () => {
|
|
218
|
+
const schema = array()._schema;
|
|
219
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
220
|
+
expect(schema).not.toHaveProperty('minLength');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('minLength - 2', () => {
|
|
224
|
+
const s: Schema = {
|
|
225
|
+
type: 'array'
|
|
226
|
+
};
|
|
227
|
+
const schema = array().ofType(s).minLength(10)._schema;
|
|
228
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
229
|
+
expect(schema).toHaveProperty('minLength', 10);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('minLength - 3', () => {
|
|
233
|
+
const s: Schema = {
|
|
234
|
+
type: 'array'
|
|
235
|
+
};
|
|
236
|
+
const schema = array().ofType(s).minLength(10).clearMinLength()._schema;
|
|
237
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
238
|
+
expect(schema).not.toHaveProperty('minLength');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test('minLength - 4', () => {
|
|
242
|
+
const schema1 = array().minLength(10).clearMinLength();
|
|
243
|
+
const schema2 = schema1.clearMinLength();
|
|
244
|
+
const equal = (schema1 as any) === schema2;
|
|
245
|
+
expect(equal).toEqual(true);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test('minLength - 5', () => {
|
|
249
|
+
const schema1 = array().minLength(20);
|
|
250
|
+
const schema2 = schema1.clearMinLength();
|
|
251
|
+
const equal = (schema1 as any) === schema2;
|
|
252
|
+
expect(equal).toEqual(false);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test('minLength - 6', () => {
|
|
256
|
+
const schema1 = array().minLength(20).clearMinLength();
|
|
257
|
+
const schema2 = schema1.clearMinLength();
|
|
258
|
+
const equal = (schema1 as any) === schema2;
|
|
259
|
+
expect(equal).toEqual(true);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('min/max lengths - 1', () => {
|
|
263
|
+
const s: Schema = {
|
|
264
|
+
type: 'array'
|
|
265
|
+
};
|
|
266
|
+
const schema = array().ofType(s).minLength(10).maxLength(100)._schema;
|
|
267
|
+
expect(schema).toHaveProperty('type', 'array');
|
|
268
|
+
expect(schema).toHaveProperty('minLength', 10);
|
|
269
|
+
expect(schema).toHaveProperty('maxLength', 100);
|
|
270
|
+
});
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
import { Schema, Validator } from '../schema.js';
|
|
3
|
+
import { defaultSchemas } from '../defaultSchemas.js';
|
|
4
|
+
|
|
5
|
+
export interface IArraySchemaBuilder<
|
|
6
|
+
TRequired extends boolean = true,
|
|
7
|
+
TNullable extends boolean = false,
|
|
8
|
+
TOfType extends Schema | undefined = undefined,
|
|
9
|
+
TMaxLength extends number | undefined = undefined,
|
|
10
|
+
TMinLength extends number | undefined = undefined
|
|
11
|
+
> extends ISchemaBuilder<TRequired, TNullable> {
|
|
12
|
+
optional(): IArraySchemaBuilder<
|
|
13
|
+
false,
|
|
14
|
+
TNullable,
|
|
15
|
+
TOfType,
|
|
16
|
+
TMaxLength,
|
|
17
|
+
TMinLength
|
|
18
|
+
>;
|
|
19
|
+
required(): IArraySchemaBuilder<
|
|
20
|
+
true,
|
|
21
|
+
TNullable,
|
|
22
|
+
TOfType,
|
|
23
|
+
TMaxLength,
|
|
24
|
+
TMinLength
|
|
25
|
+
>;
|
|
26
|
+
ofType<T extends Schema>(
|
|
27
|
+
schema: T
|
|
28
|
+
): IArraySchemaBuilder<TRequired, TNullable, T, TMaxLength, TMinLength>;
|
|
29
|
+
nullable(): IArraySchemaBuilder<
|
|
30
|
+
TRequired,
|
|
31
|
+
true,
|
|
32
|
+
TOfType,
|
|
33
|
+
TMaxLength,
|
|
34
|
+
TMinLength
|
|
35
|
+
>;
|
|
36
|
+
notNullable(): IArraySchemaBuilder<
|
|
37
|
+
TRequired,
|
|
38
|
+
false,
|
|
39
|
+
TOfType,
|
|
40
|
+
TMaxLength,
|
|
41
|
+
TMinLength
|
|
42
|
+
>;
|
|
43
|
+
clearOfType(): IArraySchemaBuilder<
|
|
44
|
+
TRequired,
|
|
45
|
+
TNullable,
|
|
46
|
+
undefined,
|
|
47
|
+
TMaxLength,
|
|
48
|
+
TMinLength
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
maxLength<T extends number>(
|
|
52
|
+
length: T
|
|
53
|
+
): IArraySchemaBuilder<TRequired, TNullable, TOfType, T, TMinLength>;
|
|
54
|
+
clearMaxLength(): IArraySchemaBuilder<
|
|
55
|
+
TRequired,
|
|
56
|
+
TNullable,
|
|
57
|
+
TOfType,
|
|
58
|
+
undefined,
|
|
59
|
+
TMinLength
|
|
60
|
+
>;
|
|
61
|
+
minLength<T extends number>(
|
|
62
|
+
length: T
|
|
63
|
+
): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, T>;
|
|
64
|
+
|
|
65
|
+
clearMinLength(): IArraySchemaBuilder<
|
|
66
|
+
TRequired,
|
|
67
|
+
TNullable,
|
|
68
|
+
TOfType,
|
|
69
|
+
TMaxLength,
|
|
70
|
+
TMinLength
|
|
71
|
+
>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
class ArraySchemaBuilder<
|
|
75
|
+
TRequired extends boolean = true,
|
|
76
|
+
TNullable extends boolean = false,
|
|
77
|
+
TOfType extends Schema | undefined = undefined,
|
|
78
|
+
TMaxLength extends number | undefined = undefined,
|
|
79
|
+
TMinLength extends number | undefined = undefined
|
|
80
|
+
>
|
|
81
|
+
extends SchemaBuilder<TRequired, TNullable>
|
|
82
|
+
implements
|
|
83
|
+
IArraySchemaBuilder<
|
|
84
|
+
TRequired,
|
|
85
|
+
TNullable,
|
|
86
|
+
TOfType,
|
|
87
|
+
TMaxLength,
|
|
88
|
+
TMinLength
|
|
89
|
+
>
|
|
90
|
+
{
|
|
91
|
+
public clone(): this {
|
|
92
|
+
return ArraySchemaBuilder.create(this._schema as any) as this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public get _schema(): Schema {
|
|
96
|
+
return Object.assign(
|
|
97
|
+
{
|
|
98
|
+
type: this.type
|
|
99
|
+
},
|
|
100
|
+
this.getCommonSchema(),
|
|
101
|
+
typeof this._ofType !== 'undefined'
|
|
102
|
+
? {
|
|
103
|
+
ofType: this._ofType as any
|
|
104
|
+
}
|
|
105
|
+
: {},
|
|
106
|
+
typeof this._minLength !== 'undefined'
|
|
107
|
+
? { minLength: this._minLength }
|
|
108
|
+
: {},
|
|
109
|
+
typeof this._maxLength !== 'undefined'
|
|
110
|
+
? { maxLength: this._maxLength }
|
|
111
|
+
: {}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public static create<
|
|
116
|
+
TRequired extends boolean = true,
|
|
117
|
+
TNullable extends boolean = false,
|
|
118
|
+
TOfType extends Schema | undefined = undefined,
|
|
119
|
+
TMaxLength extends number | undefined = undefined,
|
|
120
|
+
TMinLength extends number | undefined = undefined
|
|
121
|
+
>(obj?: {
|
|
122
|
+
isRequired: TRequired;
|
|
123
|
+
isNullable: TNullable;
|
|
124
|
+
ofType?: TOfType;
|
|
125
|
+
maxLength?: TMaxLength;
|
|
126
|
+
minLength?: TMinLength;
|
|
127
|
+
validators?: Validator[];
|
|
128
|
+
preprocessor?:
|
|
129
|
+
| ((value: unknown) => unknown | Promise<unknown>)
|
|
130
|
+
| string;
|
|
131
|
+
}): IArraySchemaBuilder<
|
|
132
|
+
TRequired,
|
|
133
|
+
TNullable,
|
|
134
|
+
TOfType,
|
|
135
|
+
TMaxLength,
|
|
136
|
+
TMinLength
|
|
137
|
+
> {
|
|
138
|
+
return new ArraySchemaBuilder(obj as any);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private constructor(obj: {
|
|
142
|
+
isRequired: TRequired;
|
|
143
|
+
isNullable: TNullable;
|
|
144
|
+
ofType?: TOfType;
|
|
145
|
+
maxLength?: TMaxLength;
|
|
146
|
+
minLength?: TMinLength;
|
|
147
|
+
validators?: Validator[];
|
|
148
|
+
preprocessor?:
|
|
149
|
+
| ((value: unknown) => unknown | Promise<unknown>)
|
|
150
|
+
| string;
|
|
151
|
+
}) {
|
|
152
|
+
super(obj);
|
|
153
|
+
if (typeof obj === 'object' && obj) {
|
|
154
|
+
if (typeof obj.ofType !== 'undefined') {
|
|
155
|
+
this._ofType =
|
|
156
|
+
obj.ofType instanceof SchemaBuilder
|
|
157
|
+
? obj.ofType.clone()
|
|
158
|
+
: { ...(obj.ofType as any) };
|
|
159
|
+
}
|
|
160
|
+
if (typeof obj.maxLength !== 'undefined') {
|
|
161
|
+
this._maxLength = obj.maxLength;
|
|
162
|
+
}
|
|
163
|
+
if (typeof obj.minLength !== 'undefined') {
|
|
164
|
+
this._minLength = obj.minLength;
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
const defaultSchema = defaultSchemas['array'] as any;
|
|
168
|
+
this.isRequired = defaultSchema.isRequired;
|
|
169
|
+
this.isNullable = defaultSchema.isNullable;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected readonly type = 'array';
|
|
174
|
+
|
|
175
|
+
protected _ofType?: TOfType;
|
|
176
|
+
protected _maxLength?: TMaxLength;
|
|
177
|
+
protected _minLength?: TMinLength;
|
|
178
|
+
|
|
179
|
+
optional(): IArraySchemaBuilder<
|
|
180
|
+
false,
|
|
181
|
+
TNullable,
|
|
182
|
+
TOfType,
|
|
183
|
+
TMaxLength,
|
|
184
|
+
TMinLength
|
|
185
|
+
> {
|
|
186
|
+
if (this.isRequired === false) {
|
|
187
|
+
return this as IArraySchemaBuilder<
|
|
188
|
+
false,
|
|
189
|
+
TNullable,
|
|
190
|
+
TOfType,
|
|
191
|
+
TMaxLength,
|
|
192
|
+
TMinLength
|
|
193
|
+
>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return ArraySchemaBuilder.create({
|
|
197
|
+
...(this._schema as any),
|
|
198
|
+
isRequired: false
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
required(): IArraySchemaBuilder<
|
|
203
|
+
true,
|
|
204
|
+
TNullable,
|
|
205
|
+
TOfType,
|
|
206
|
+
TMaxLength,
|
|
207
|
+
TMinLength
|
|
208
|
+
> {
|
|
209
|
+
if (this.isRequired === true) {
|
|
210
|
+
return this as IArraySchemaBuilder<
|
|
211
|
+
true,
|
|
212
|
+
TNullable,
|
|
213
|
+
TOfType,
|
|
214
|
+
TMaxLength,
|
|
215
|
+
TMinLength
|
|
216
|
+
>;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return ArraySchemaBuilder.create({
|
|
220
|
+
...(this._schema as any),
|
|
221
|
+
isRequired: true
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
nullable(): IArraySchemaBuilder<
|
|
226
|
+
TRequired,
|
|
227
|
+
true,
|
|
228
|
+
TOfType,
|
|
229
|
+
TMaxLength,
|
|
230
|
+
TMinLength
|
|
231
|
+
> {
|
|
232
|
+
if (this._isNullable === true) {
|
|
233
|
+
return this as IArraySchemaBuilder<
|
|
234
|
+
TRequired,
|
|
235
|
+
true,
|
|
236
|
+
TOfType,
|
|
237
|
+
TMaxLength,
|
|
238
|
+
TMinLength
|
|
239
|
+
>;
|
|
240
|
+
}
|
|
241
|
+
return ArraySchemaBuilder.create({
|
|
242
|
+
...(this._schema as any),
|
|
243
|
+
isNullable: true
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
notNullable(): IArraySchemaBuilder<
|
|
248
|
+
TRequired,
|
|
249
|
+
false,
|
|
250
|
+
TOfType,
|
|
251
|
+
TMaxLength,
|
|
252
|
+
TMinLength
|
|
253
|
+
> {
|
|
254
|
+
if (this.isNullable === false) {
|
|
255
|
+
return this as IArraySchemaBuilder<
|
|
256
|
+
TRequired,
|
|
257
|
+
false,
|
|
258
|
+
TOfType,
|
|
259
|
+
TMaxLength,
|
|
260
|
+
TMinLength
|
|
261
|
+
>;
|
|
262
|
+
}
|
|
263
|
+
return ArraySchemaBuilder.create({
|
|
264
|
+
...(this._schema as any),
|
|
265
|
+
isNullable: false
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
ofType<T extends Schema>(
|
|
270
|
+
schema: T
|
|
271
|
+
): IArraySchemaBuilder<TRequired, TNullable, T, TMaxLength, TMinLength> {
|
|
272
|
+
return ArraySchemaBuilder.create({
|
|
273
|
+
...(this._schema as any),
|
|
274
|
+
ofType: schema
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
clearOfType(): IArraySchemaBuilder<
|
|
279
|
+
TRequired,
|
|
280
|
+
TNullable,
|
|
281
|
+
undefined,
|
|
282
|
+
TMaxLength,
|
|
283
|
+
TMinLength
|
|
284
|
+
> {
|
|
285
|
+
if (typeof this._ofType === 'undefined') {
|
|
286
|
+
return this as IArraySchemaBuilder<
|
|
287
|
+
TRequired,
|
|
288
|
+
TNullable,
|
|
289
|
+
undefined,
|
|
290
|
+
TMaxLength,
|
|
291
|
+
TMinLength
|
|
292
|
+
>;
|
|
293
|
+
}
|
|
294
|
+
return ArraySchemaBuilder.create({
|
|
295
|
+
...(this._schema as any),
|
|
296
|
+
ofType: undefined
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
maxLength<T extends number>(
|
|
301
|
+
length: T
|
|
302
|
+
): IArraySchemaBuilder<TRequired, TNullable, TOfType, T, TMinLength> {
|
|
303
|
+
if ((this._maxLength as any) === length) {
|
|
304
|
+
return this as any as IArraySchemaBuilder<
|
|
305
|
+
TRequired,
|
|
306
|
+
TNullable,
|
|
307
|
+
TOfType,
|
|
308
|
+
T,
|
|
309
|
+
TMinLength
|
|
310
|
+
>;
|
|
311
|
+
}
|
|
312
|
+
return ArraySchemaBuilder.create({
|
|
313
|
+
...(this._schema as any),
|
|
314
|
+
maxLength: length
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
clearMaxLength(): IArraySchemaBuilder<
|
|
319
|
+
TRequired,
|
|
320
|
+
TNullable,
|
|
321
|
+
TOfType,
|
|
322
|
+
undefined,
|
|
323
|
+
TMinLength
|
|
324
|
+
> {
|
|
325
|
+
if (typeof this._maxLength === 'undefined') {
|
|
326
|
+
return this as IArraySchemaBuilder<
|
|
327
|
+
TRequired,
|
|
328
|
+
TNullable,
|
|
329
|
+
TOfType,
|
|
330
|
+
undefined,
|
|
331
|
+
TMinLength
|
|
332
|
+
>;
|
|
333
|
+
}
|
|
334
|
+
return ArraySchemaBuilder.create({
|
|
335
|
+
...(this._schema as any),
|
|
336
|
+
maxLength: undefined
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
minLength<T extends number>(
|
|
341
|
+
length: T
|
|
342
|
+
): IArraySchemaBuilder<TRequired, TNullable, TOfType, TMaxLength, T> {
|
|
343
|
+
if ((this._minLength as any) === length) {
|
|
344
|
+
return this as any as IArraySchemaBuilder<
|
|
345
|
+
TRequired,
|
|
346
|
+
TNullable,
|
|
347
|
+
TOfType,
|
|
348
|
+
TMaxLength,
|
|
349
|
+
T
|
|
350
|
+
>;
|
|
351
|
+
}
|
|
352
|
+
return ArraySchemaBuilder.create({
|
|
353
|
+
...(this._schema as any),
|
|
354
|
+
minLength: length
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
clearMinLength(): IArraySchemaBuilder<
|
|
359
|
+
TRequired,
|
|
360
|
+
TNullable,
|
|
361
|
+
TOfType,
|
|
362
|
+
TMaxLength,
|
|
363
|
+
TMinLength
|
|
364
|
+
> {
|
|
365
|
+
if (typeof this._minLength === 'undefined') {
|
|
366
|
+
return this as IArraySchemaBuilder<
|
|
367
|
+
TRequired,
|
|
368
|
+
TNullable,
|
|
369
|
+
TOfType,
|
|
370
|
+
TMaxLength,
|
|
371
|
+
TMinLength
|
|
372
|
+
>;
|
|
373
|
+
}
|
|
374
|
+
return ArraySchemaBuilder.create({
|
|
375
|
+
...(this._schema as any),
|
|
376
|
+
minLength: undefined
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export const array = () => ArraySchemaBuilder.create();
|