@cleverbrush/schema 1.0.0-beta.5 → 1.1.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 +179 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
- package/dist/builders/FunctionSchemaBuilder.js +102 -58
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +310 -81
- package/dist/builders/ObjectSchemaBuilder.js +528 -283
- package/dist/builders/SchemaBuilder.d.ts +157 -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 +132 -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 -231
- 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 -145
- package/src/builders/FunctionSchemaBuilder.ts +0 -117
- 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 -849
- 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,657 +0,0 @@
|
|
|
1
|
-
import { Schema } from '../schema.js';
|
|
2
|
-
import { number } from './NumberSchemaBuilder.js';
|
|
3
|
-
import { object } from './ObjectSchemaBuilder.js';
|
|
4
|
-
|
|
5
|
-
test('Clean', () => {
|
|
6
|
-
const schema = object();
|
|
7
|
-
expect(schema).toHaveProperty('type', 'object');
|
|
8
|
-
expect(schema).not.toHaveProperty('properties');
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test('Immutable - 1', () => {
|
|
12
|
-
const schema1 = object();
|
|
13
|
-
const schema2 = schema1.optional();
|
|
14
|
-
const k = (schema1 as any) === (schema2 as any);
|
|
15
|
-
expect(k).toEqual(false);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('Immutable - 2', () => {
|
|
19
|
-
const schema1 = object().addProps({
|
|
20
|
-
first: number()
|
|
21
|
-
});
|
|
22
|
-
const schema2 = schema1.optional();
|
|
23
|
-
const k = (schema1 as any).properties === (schema2 as any).properties;
|
|
24
|
-
expect(k).toEqual(false);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('optional', () => {
|
|
28
|
-
const schema = object().optional();
|
|
29
|
-
expect(schema).toHaveProperty('type', 'object');
|
|
30
|
-
expect(schema).toHaveProperty('isRequired', false);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('optional - 2', () => {
|
|
34
|
-
const schema = object().optional().required();
|
|
35
|
-
expect(schema).toHaveProperty('type', 'object');
|
|
36
|
-
expect(schema).toHaveProperty('isRequired', true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('optional - 3', () => {
|
|
40
|
-
const schema1 = object();
|
|
41
|
-
const schema2 = schema1.optional();
|
|
42
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
43
|
-
const isRequiredEqual = (schema1 as any).isRequired !== schema2.isRequired;
|
|
44
|
-
expect(e).toEqual(false);
|
|
45
|
-
expect(isRequiredEqual).toEqual(true);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('optional - 4', () => {
|
|
49
|
-
const schema1 = object().optional();
|
|
50
|
-
const schema2 = schema1.optional();
|
|
51
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
52
|
-
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
53
|
-
expect(e).toEqual(true);
|
|
54
|
-
expect(isRequiredEqual).toEqual(true);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('optional - 5', () => {
|
|
58
|
-
const schema1 = object();
|
|
59
|
-
const schema2 = schema1.required();
|
|
60
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
61
|
-
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
62
|
-
expect(e).toEqual(true);
|
|
63
|
-
expect(isRequiredEqual).toEqual(true);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('optional - 6', () => {
|
|
67
|
-
const schema1 = object().optional();
|
|
68
|
-
const schema2 = schema1.required();
|
|
69
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
70
|
-
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
71
|
-
expect(e).toEqual(false);
|
|
72
|
-
expect(isRequiredEqual).toEqual(false);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('optional - 7', () => {
|
|
76
|
-
const schema1 = object().optional().required();
|
|
77
|
-
const schema2 = schema1.required();
|
|
78
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
79
|
-
const isRequiredEqual = (schema1 as any).isRequired === schema2.isRequired;
|
|
80
|
-
expect(e).toEqual(true);
|
|
81
|
-
expect(isRequiredEqual).toEqual(true);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('nullable - 1', () => {
|
|
85
|
-
const schema = object().nullable();
|
|
86
|
-
expect(schema).toHaveProperty('type', 'object');
|
|
87
|
-
expect(schema).toHaveProperty('isNullable', true);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test('nullable - 2', () => {
|
|
91
|
-
const schema = object().nullable().notNullable();
|
|
92
|
-
expect(schema).toHaveProperty('type', 'object');
|
|
93
|
-
expect(schema).toHaveProperty('isNullable', false);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('nullable - 3', () => {
|
|
97
|
-
const schema1 = object();
|
|
98
|
-
const schema2 = schema1.nullable();
|
|
99
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
100
|
-
const isNullableEqual = (schema1 as any).isNullable !== schema2.isNullable;
|
|
101
|
-
expect(e).toEqual(false);
|
|
102
|
-
expect(isNullableEqual).toEqual(true);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('nullable - 4', () => {
|
|
106
|
-
const schema1 = object().nullable();
|
|
107
|
-
const schema2 = schema1.nullable();
|
|
108
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
109
|
-
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
110
|
-
expect(e).toEqual(true);
|
|
111
|
-
expect(isNullableEqual).toEqual(true);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('nullable - 5', () => {
|
|
115
|
-
const schema1 = object();
|
|
116
|
-
const schema2 = schema1.nullable();
|
|
117
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
118
|
-
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
119
|
-
expect(e).toEqual(false);
|
|
120
|
-
expect(isNullableEqual).toEqual(false);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test('nullable - 6', () => {
|
|
124
|
-
const schema1 = object();
|
|
125
|
-
const schema2 = schema1.notNullable();
|
|
126
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
127
|
-
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
128
|
-
expect(e).toEqual(true);
|
|
129
|
-
expect(isNullableEqual).toEqual(true);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test('nullable - 7', () => {
|
|
133
|
-
const schema1 = object().nullable();
|
|
134
|
-
const schema2 = schema1.notNullable();
|
|
135
|
-
const e = (schema1 as Schema) === (schema2 as Schema);
|
|
136
|
-
const isNullableEqual = (schema1 as any).isNullable === schema2.isNullable;
|
|
137
|
-
expect(e).toEqual(false);
|
|
138
|
-
expect(isNullableEqual).toEqual(false);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test('hasProperties - 1', () => {
|
|
142
|
-
const schema1 = object().optional();
|
|
143
|
-
const schema2 = schema1.addProps({
|
|
144
|
-
first: number(),
|
|
145
|
-
second: {
|
|
146
|
-
type: 'number'
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
const equal = (schema1 as Schema) === schema2;
|
|
151
|
-
expect(equal).toEqual(false);
|
|
152
|
-
expect(schema2).toHaveProperty('properties.second.type', 'number');
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('hasProperties - 2', () => {
|
|
156
|
-
const schema1 = object()
|
|
157
|
-
.optional()
|
|
158
|
-
.addProps({
|
|
159
|
-
first: number(),
|
|
160
|
-
second: {
|
|
161
|
-
type: 'number'
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
const schema2 = schema1.addProps({
|
|
165
|
-
third: number()
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const equal = (schema1 as Schema) === schema2;
|
|
169
|
-
expect(equal).toEqual(false);
|
|
170
|
-
expect(schema2).toHaveProperty('properties.third.type', 'number');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test('removeProp - 1', () => {
|
|
174
|
-
const schema1 = object()
|
|
175
|
-
.addProps({
|
|
176
|
-
first: number(),
|
|
177
|
-
second: number()
|
|
178
|
-
})
|
|
179
|
-
.removeProp('second');
|
|
180
|
-
expect(schema1).toHaveProperty('properties.first');
|
|
181
|
-
expect(schema1).not.toHaveProperty('properties.second');
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test('removeProp - 2', () => {
|
|
185
|
-
const schema1 = object().addProps({
|
|
186
|
-
first: number(),
|
|
187
|
-
second: number()
|
|
188
|
-
});
|
|
189
|
-
const schema2 = schema1.removeProp('second');
|
|
190
|
-
const equal = (schema1 as any) === schema2;
|
|
191
|
-
expect(equal).toEqual(false);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test('removeProp - 3', () => {
|
|
195
|
-
const schema1 = object()
|
|
196
|
-
.addProps({
|
|
197
|
-
first: number(),
|
|
198
|
-
second: number()
|
|
199
|
-
})
|
|
200
|
-
.setPropPreprocessor('second', () => 321);
|
|
201
|
-
const schema2 = schema1.removeProp('second');
|
|
202
|
-
const equal = (schema1 as any) === schema2;
|
|
203
|
-
expect(equal).toEqual(false);
|
|
204
|
-
expect(schema2).not.toHaveProperty('properties.second');
|
|
205
|
-
expect(schema2).not.toHaveProperty('preprocessors.second');
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test('preprocessors - 1', () => {
|
|
209
|
-
const schema1 = object().addProps({
|
|
210
|
-
first: number(),
|
|
211
|
-
second: number().optional()
|
|
212
|
-
});
|
|
213
|
-
expect(schema1).not.toHaveProperty('preprocessors');
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
test('preprocessors - 2', () => {
|
|
217
|
-
const schema1 = object()
|
|
218
|
-
.addProps({
|
|
219
|
-
first: number(),
|
|
220
|
-
second: number().optional()
|
|
221
|
-
})
|
|
222
|
-
.setPropPreprocessor('first', () => 123);
|
|
223
|
-
expect(schema1).toHaveProperty('preprocessors.first');
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
test('preprocessors - 3', () => {
|
|
227
|
-
const schema1 = object().addProps({
|
|
228
|
-
first: number(),
|
|
229
|
-
second: number().optional()
|
|
230
|
-
});
|
|
231
|
-
const schema2 = schema1.setPropPreprocessor('first', () => 123);
|
|
232
|
-
const equals = (schema1 as any) === schema2;
|
|
233
|
-
expect(equals).toEqual(false);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
test('preprocessors - 4', () => {
|
|
237
|
-
const schema1 = object()
|
|
238
|
-
.addProps({
|
|
239
|
-
first: number(),
|
|
240
|
-
second: number().optional()
|
|
241
|
-
})
|
|
242
|
-
.setPropPreprocessor('first', () => 123);
|
|
243
|
-
const schema2 = schema1.unsetPropPreprocessor('first');
|
|
244
|
-
const equals = (schema1 as any) === schema2;
|
|
245
|
-
expect(equals).toEqual(false);
|
|
246
|
-
expect(schema2).not.toHaveProperty('preprocessors');
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test('preprocessors - 5', () => {
|
|
250
|
-
const schema1 = object()
|
|
251
|
-
.addProps({
|
|
252
|
-
first: number(),
|
|
253
|
-
second: number().optional()
|
|
254
|
-
})
|
|
255
|
-
.setPropPreprocessor('first', () => 123);
|
|
256
|
-
const schema2 = schema1
|
|
257
|
-
.setPropPreprocessor('second', () => 321)
|
|
258
|
-
.unsetPropPreprocessor('first');
|
|
259
|
-
const equals = (schema1 as any) === schema2;
|
|
260
|
-
expect(equals).toEqual(false);
|
|
261
|
-
expect(schema2).not.toHaveProperty('preprocessors.first');
|
|
262
|
-
expect(schema2).toHaveProperty('preprocessors.second');
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
test('preprocessors - 6', () => {
|
|
266
|
-
const schema1 = object().addProps({
|
|
267
|
-
first: number(),
|
|
268
|
-
second: number().optional()
|
|
269
|
-
});
|
|
270
|
-
const schema2 = schema1.clearPropsPreprocessors();
|
|
271
|
-
const equals = (schema1 as any) === schema2;
|
|
272
|
-
expect(equals).toEqual(true);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
test('preprocessors - 7', () => {
|
|
276
|
-
const schema1 = object()
|
|
277
|
-
.addProps({
|
|
278
|
-
first: number(),
|
|
279
|
-
second: number().optional()
|
|
280
|
-
})
|
|
281
|
-
.setPropPreprocessor('first', () => 123);
|
|
282
|
-
const schema2 = schema1.clearPropsPreprocessors();
|
|
283
|
-
const equals = (schema1 as any) === schema2;
|
|
284
|
-
expect(equals).toEqual(false);
|
|
285
|
-
expect(schema2).not.toHaveProperty('preprocessors');
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
test('preprocessors - 8', () => {
|
|
289
|
-
const schema1 = object()
|
|
290
|
-
.addProps({
|
|
291
|
-
first: number(),
|
|
292
|
-
second: number().optional()
|
|
293
|
-
})
|
|
294
|
-
.setPropPreprocessor('first', () => 123)
|
|
295
|
-
.setPropPreprocessor('*', () => 123);
|
|
296
|
-
const schema2 = schema1.unsetPropPreprocessor('first');
|
|
297
|
-
const equals = (schema1 as any) === schema2;
|
|
298
|
-
expect(equals).toEqual(false);
|
|
299
|
-
expect(schema2).not.toHaveProperty('preprocessors.first');
|
|
300
|
-
expect(schema2).toHaveProperty('preprocessors.*');
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
test('preprocessors - 9', () => {
|
|
304
|
-
const schema1 = object()
|
|
305
|
-
.addProps({
|
|
306
|
-
first: number(),
|
|
307
|
-
second: number().optional()
|
|
308
|
-
})
|
|
309
|
-
.setPropPreprocessor('first', () => 123)
|
|
310
|
-
.unsetPropPreprocessor('first');
|
|
311
|
-
const schema2 = schema1.unsetPropPreprocessor('first');
|
|
312
|
-
const equals = (schema1 as any) === schema2;
|
|
313
|
-
expect(equals).toEqual(true);
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
test('canHaveUnknownProps - 1', () => {
|
|
317
|
-
const schema1 = object()
|
|
318
|
-
.addProps({
|
|
319
|
-
first: number(),
|
|
320
|
-
second: number().optional()
|
|
321
|
-
})
|
|
322
|
-
.canHaveUnknownProps();
|
|
323
|
-
expect(schema1).toHaveProperty('noUnknownProperties', false);
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
test('canHaveUnknownProps - 2', () => {
|
|
327
|
-
const schema1 = object().addProps({
|
|
328
|
-
first: number(),
|
|
329
|
-
second: number().optional()
|
|
330
|
-
});
|
|
331
|
-
const schema2 = schema1.canHaveUnknownProps();
|
|
332
|
-
const equal = (schema1 as any) === schema2;
|
|
333
|
-
expect(equal).toEqual(false);
|
|
334
|
-
expect(schema2).toHaveProperty('noUnknownProperties', false);
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
test('canHaveUnknownProps - 3', () => {
|
|
338
|
-
const schema1 = object()
|
|
339
|
-
.addProps({
|
|
340
|
-
first: number(),
|
|
341
|
-
second: number().optional()
|
|
342
|
-
})
|
|
343
|
-
.canHaveUnknownProps();
|
|
344
|
-
const schema2 = schema1.canHaveUnknownProps();
|
|
345
|
-
const equal = (schema1 as any) === schema2;
|
|
346
|
-
expect(equal).toEqual(true);
|
|
347
|
-
expect(schema1).toHaveProperty('noUnknownProperties', false);
|
|
348
|
-
expect(schema2).toHaveProperty('noUnknownProperties', false);
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
test('noUnknownProps - 1', () => {
|
|
352
|
-
const schema1 = object()
|
|
353
|
-
.addProps({
|
|
354
|
-
first: number(),
|
|
355
|
-
second: number().optional()
|
|
356
|
-
})
|
|
357
|
-
.noUnknownProps();
|
|
358
|
-
expect(schema1).toHaveProperty('noUnknownProperties', true);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
test('canHaveUnknownProps - 2', () => {
|
|
362
|
-
const schema1 = object().addProps({
|
|
363
|
-
first: number(),
|
|
364
|
-
second: number().optional()
|
|
365
|
-
});
|
|
366
|
-
const schema2 = schema1.noUnknownProps();
|
|
367
|
-
const equal = (schema1 as any) === schema2;
|
|
368
|
-
expect(equal).toEqual(false);
|
|
369
|
-
expect(schema2).toHaveProperty('noUnknownProperties', true);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
test('canHaveUnknownProps - 3', () => {
|
|
373
|
-
const schema1 = object()
|
|
374
|
-
.addProps({
|
|
375
|
-
first: number(),
|
|
376
|
-
second: number().optional()
|
|
377
|
-
})
|
|
378
|
-
.noUnknownProps();
|
|
379
|
-
const schema2 = schema1.noUnknownProps();
|
|
380
|
-
const equal = (schema1 as any) === schema2;
|
|
381
|
-
expect(equal).toEqual(true);
|
|
382
|
-
expect(schema1).toHaveProperty('noUnknownProperties', true);
|
|
383
|
-
expect(schema2).toHaveProperty('noUnknownProperties', true);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
test('makePropOptional - 1', () => {
|
|
387
|
-
const schema1 = object().addProps({
|
|
388
|
-
first: number(),
|
|
389
|
-
second: number()
|
|
390
|
-
});
|
|
391
|
-
const schema2 = schema1.makePropOptional('second');
|
|
392
|
-
const equal = (schema1 as any) === schema2;
|
|
393
|
-
expect(equal).toEqual(false);
|
|
394
|
-
const propsEqual =
|
|
395
|
-
(schema1 as any).properties.second ===
|
|
396
|
-
(schema2 as any).properties.second;
|
|
397
|
-
expect(propsEqual).toEqual(false);
|
|
398
|
-
const propsIsRequired1 =
|
|
399
|
-
(schema1 as any).properties.second.isRequired === true;
|
|
400
|
-
expect(propsIsRequired1).toEqual(true);
|
|
401
|
-
const propsIsRequired2 =
|
|
402
|
-
(schema2 as any).properties.second.isRequired === false;
|
|
403
|
-
expect(propsIsRequired2).toEqual(true);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
test('makePropOptional - 2', () => {
|
|
407
|
-
expect(() => {
|
|
408
|
-
const schema1 = object().addProps({
|
|
409
|
-
first: number(),
|
|
410
|
-
second: number()
|
|
411
|
-
});
|
|
412
|
-
schema1.makePropOptional('second123' as any);
|
|
413
|
-
}).toThrow();
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
test('makePropOptional - 3', () => {
|
|
417
|
-
const schema1 = object().addProps({
|
|
418
|
-
first: number(),
|
|
419
|
-
second: {
|
|
420
|
-
type: 'number',
|
|
421
|
-
isRequired: true
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
const schema2 = schema1.makePropOptional('second');
|
|
425
|
-
const equal = (schema1 as any) === schema2;
|
|
426
|
-
expect(equal).toEqual(false);
|
|
427
|
-
const propsEqual =
|
|
428
|
-
(schema1 as any).properties.second ===
|
|
429
|
-
(schema2 as any).properties.second;
|
|
430
|
-
expect(propsEqual).toEqual(false);
|
|
431
|
-
const propsIsRequired1 =
|
|
432
|
-
(schema1 as any).properties.second.isRequired === true;
|
|
433
|
-
expect(propsIsRequired1).toEqual(true);
|
|
434
|
-
const propsIsRequired2 =
|
|
435
|
-
(schema2 as any).properties.second.isRequired === false;
|
|
436
|
-
expect(propsIsRequired2).toEqual(true);
|
|
437
|
-
});
|
|
438
|
-
|
|
439
|
-
test('makePropRequired - 1', () => {
|
|
440
|
-
const schema1 = object().addProps({
|
|
441
|
-
first: number().optional(),
|
|
442
|
-
second: number().optional()
|
|
443
|
-
});
|
|
444
|
-
const schema2 = schema1.makePropRequired('second');
|
|
445
|
-
const equal = (schema1 as any) === schema2;
|
|
446
|
-
expect(equal).toEqual(false);
|
|
447
|
-
const propsEqual =
|
|
448
|
-
(schema1 as any).properties.second ===
|
|
449
|
-
(schema2 as any).properties.second;
|
|
450
|
-
expect(propsEqual).toEqual(false);
|
|
451
|
-
const propsIsRequired1 =
|
|
452
|
-
(schema1 as any).properties.second.isRequired === false;
|
|
453
|
-
expect(propsIsRequired1).toEqual(true);
|
|
454
|
-
const propsIsRequired2 =
|
|
455
|
-
(schema2 as any).properties.second.isRequired === true;
|
|
456
|
-
expect(propsIsRequired2).toEqual(true);
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
test('makePropRequired - 2', () => {
|
|
460
|
-
expect(() => {
|
|
461
|
-
const schema1 = object().addProps({
|
|
462
|
-
first: number(),
|
|
463
|
-
second: number()
|
|
464
|
-
});
|
|
465
|
-
schema1.makePropRequired('second123' as any);
|
|
466
|
-
}).toThrow();
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
test('makePropRequired - 3', () => {
|
|
470
|
-
const schema1 = object().addProps({
|
|
471
|
-
first: number().optional(),
|
|
472
|
-
second: {
|
|
473
|
-
type: 'number',
|
|
474
|
-
isRequired: false
|
|
475
|
-
}
|
|
476
|
-
});
|
|
477
|
-
const schema2 = schema1.makePropRequired('second');
|
|
478
|
-
const equal = (schema1 as any) === schema2;
|
|
479
|
-
expect(equal).toEqual(false);
|
|
480
|
-
const propsEqual =
|
|
481
|
-
(schema1 as any).properties.second ===
|
|
482
|
-
(schema2 as any).properties.second;
|
|
483
|
-
expect(propsEqual).toEqual(false);
|
|
484
|
-
const propsIsRequired1 =
|
|
485
|
-
(schema1 as any).properties.second.isRequired === false;
|
|
486
|
-
expect(propsIsRequired1).toEqual(true);
|
|
487
|
-
const propsIsRequired2 =
|
|
488
|
-
(schema2 as any).properties.second.isRequired === true;
|
|
489
|
-
expect(propsIsRequired2).toEqual(true);
|
|
490
|
-
});
|
|
491
|
-
|
|
492
|
-
test('makePropNullable - 1', () => {
|
|
493
|
-
const schema1 = object().addProps({
|
|
494
|
-
first: number(),
|
|
495
|
-
second: number()
|
|
496
|
-
});
|
|
497
|
-
const schema2 = schema1.makePropNullable('second');
|
|
498
|
-
const equal = (schema1 as any) === schema2;
|
|
499
|
-
expect(equal).toEqual(false);
|
|
500
|
-
const propsEqual =
|
|
501
|
-
(schema1 as any).properties.second ===
|
|
502
|
-
(schema2 as any).properties.second;
|
|
503
|
-
expect(propsEqual).toEqual(false);
|
|
504
|
-
const propsIsNullable1 =
|
|
505
|
-
(schema1 as any).properties.second.isNullable === false;
|
|
506
|
-
expect(propsIsNullable1).toEqual(true);
|
|
507
|
-
const propsIsNullable2 =
|
|
508
|
-
(schema2 as any).properties.second.isNullable === true;
|
|
509
|
-
expect(propsIsNullable2).toEqual(true);
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
test('makePropNullable - 2', () => {
|
|
513
|
-
expect(() => {
|
|
514
|
-
const schema1 = object().addProps({
|
|
515
|
-
first: number(),
|
|
516
|
-
second: number()
|
|
517
|
-
});
|
|
518
|
-
schema1.makePropNullable('second123' as any);
|
|
519
|
-
}).toThrow();
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
test('makePropNullable - 3', () => {
|
|
523
|
-
const schema1 = object().addProps({
|
|
524
|
-
first: number(),
|
|
525
|
-
second: {
|
|
526
|
-
type: 'number',
|
|
527
|
-
isNullable: false
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
const schema2 = schema1.makePropNullable('second');
|
|
531
|
-
const equal = (schema1 as any) === schema2;
|
|
532
|
-
expect(equal).toEqual(false);
|
|
533
|
-
const propsEqual =
|
|
534
|
-
(schema1 as any).properties.second ===
|
|
535
|
-
(schema2 as any).properties.second;
|
|
536
|
-
expect(propsEqual).toEqual(false);
|
|
537
|
-
const propsIsNullable1 =
|
|
538
|
-
(schema1 as any).properties.second.isNullable === false;
|
|
539
|
-
expect(propsIsNullable1).toEqual(true);
|
|
540
|
-
const propsIsNullable2 =
|
|
541
|
-
(schema2 as any).properties.second.isNullable === true;
|
|
542
|
-
expect(propsIsNullable2).toEqual(true);
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
test('makePropNotNullable - 1', () => {
|
|
546
|
-
const schema1 = object().addProps({
|
|
547
|
-
first: number().notNullable(),
|
|
548
|
-
second: number().notNullable()
|
|
549
|
-
});
|
|
550
|
-
const schema2 = schema1.makePropNullable('second');
|
|
551
|
-
const equal = (schema1 as any) === schema2;
|
|
552
|
-
expect(equal).toEqual(false);
|
|
553
|
-
const propsEqual =
|
|
554
|
-
(schema1 as any).properties.second ===
|
|
555
|
-
(schema2 as any).properties.second;
|
|
556
|
-
expect(propsEqual).toEqual(false);
|
|
557
|
-
const propsIsNotNullable1 =
|
|
558
|
-
(schema1 as any).properties.second.isNullable === false;
|
|
559
|
-
expect(propsIsNotNullable1).toEqual(true);
|
|
560
|
-
const propsIsNotNullable2 =
|
|
561
|
-
(schema2 as any).properties.second.isNullable === true;
|
|
562
|
-
expect(propsIsNotNullable2).toEqual(true);
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
test('makePropNotNullable - 2', () => {
|
|
566
|
-
expect(() => {
|
|
567
|
-
const schema1 = object().addProps({
|
|
568
|
-
first: number(),
|
|
569
|
-
second: number()
|
|
570
|
-
});
|
|
571
|
-
schema1.makePropNotNullable('second123' as any);
|
|
572
|
-
}).toThrow();
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
test('makePropNotNullable - 3', () => {
|
|
576
|
-
const schema1 = object().addProps({
|
|
577
|
-
first: number().optional(),
|
|
578
|
-
second: {
|
|
579
|
-
type: 'number',
|
|
580
|
-
isNullable: false
|
|
581
|
-
}
|
|
582
|
-
});
|
|
583
|
-
const schema2 = schema1.makePropNotNullable('second');
|
|
584
|
-
const equal = (schema1 as any) === schema2;
|
|
585
|
-
expect(equal).toEqual(false);
|
|
586
|
-
const propsEqual =
|
|
587
|
-
(schema1 as any).properties.second ===
|
|
588
|
-
(schema2 as any).properties.second;
|
|
589
|
-
expect(propsEqual).toEqual(false);
|
|
590
|
-
const propsIsNullable1 =
|
|
591
|
-
(schema1 as any).properties.second.isNullable === false;
|
|
592
|
-
expect(propsIsNullable1).toEqual(true);
|
|
593
|
-
const propsIsNullable2 =
|
|
594
|
-
(schema2 as any).properties.second.isNullable === false;
|
|
595
|
-
expect(propsIsNullable2).toEqual(true);
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
type TestType = { first?: number; second: number };
|
|
599
|
-
|
|
600
|
-
test('mapToType - 1', () => {
|
|
601
|
-
const schema1 = object({
|
|
602
|
-
first: number().optional(),
|
|
603
|
-
second: number()
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
const schema2 = schema1.mapToType<TestType>();
|
|
607
|
-
|
|
608
|
-
const equals = (schema1 as any) === schema2;
|
|
609
|
-
expect(equals).toEqual(true);
|
|
610
|
-
});
|
|
611
|
-
|
|
612
|
-
test('mapToType - 1', () => {
|
|
613
|
-
const schema1 = object({
|
|
614
|
-
first: number().optional(),
|
|
615
|
-
second: number()
|
|
616
|
-
}).mapToType<TestType>();
|
|
617
|
-
|
|
618
|
-
const schema2 = schema1.clearMapToType();
|
|
619
|
-
|
|
620
|
-
const equals = (schema1 as any) === schema2;
|
|
621
|
-
expect(equals).toEqual(true);
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
test('makeAllPropsOptional - 1', () => {
|
|
625
|
-
const schema = object({
|
|
626
|
-
first: number(),
|
|
627
|
-
second: number()
|
|
628
|
-
}).makeAllPropsOptional();
|
|
629
|
-
|
|
630
|
-
const firstIsOptional =
|
|
631
|
-
schema._schema.properties.first.isRequired === false;
|
|
632
|
-
const secondIsOptional =
|
|
633
|
-
schema._schema.properties.second.isRequired === false;
|
|
634
|
-
|
|
635
|
-
expect(firstIsOptional).toEqual(true);
|
|
636
|
-
expect(secondIsOptional).toEqual(true);
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
test('makeAllPropsOptional - 2', () => {
|
|
640
|
-
const schema = object({
|
|
641
|
-
first: number(),
|
|
642
|
-
second: number(),
|
|
643
|
-
id: number()
|
|
644
|
-
})
|
|
645
|
-
.makeAllPropsOptional()
|
|
646
|
-
.makePropRequired('id');
|
|
647
|
-
|
|
648
|
-
const firstIsOptional =
|
|
649
|
-
schema._schema.properties.first.isRequired === false;
|
|
650
|
-
const secondIsOptional =
|
|
651
|
-
schema._schema.properties.second.isRequired === false;
|
|
652
|
-
const idIsRequired = schema._schema.properties.id.isRequired === true;
|
|
653
|
-
|
|
654
|
-
expect(firstIsOptional).toEqual(true);
|
|
655
|
-
expect(secondIsOptional).toEqual(true);
|
|
656
|
-
expect(idIsRequired).toEqual(true);
|
|
657
|
-
});
|