@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.
Files changed (47) hide show
  1. package/README.md +213 -163
  2. package/package.json +3 -3
  3. package/src/builders/ArraySchemaBuilder.test.ts +270 -0
  4. package/src/builders/ArraySchemaBuilder.ts +381 -0
  5. package/src/builders/BooleanSchemaBuilder.test.ts +196 -0
  6. package/src/builders/BooleanSchemaBuilder.ts +155 -0
  7. package/src/builders/FunctionSchemaBuilder.test.ts +134 -0
  8. package/src/builders/FunctionSchemaBuilder.ts +109 -0
  9. package/src/builders/NumberSchemaBuilder.test.ts +493 -0
  10. package/src/builders/NumberSchemaBuilder.ts +789 -0
  11. package/src/builders/ObjectSchemaBuilder.test.ts +657 -0
  12. package/src/builders/ObjectSchemaBuilder.ts +794 -0
  13. package/src/builders/SchemaBuilder.test.ts +73 -0
  14. package/src/builders/SchemaBuilder.ts +135 -0
  15. package/src/builders/StringSchemaBuilder.test.ts +318 -0
  16. package/src/builders/StringSchemaBuilder.ts +392 -0
  17. package/src/builders/UnionSchemaBuilder.test.ts +162 -0
  18. package/src/builders/UnionSchemaBuilder.ts +154 -0
  19. package/src/defaultSchemas.ts +44 -0
  20. package/src/index.ts +58 -190
  21. package/src/schema.ts +827 -0
  22. package/src/schemaRegistry.builders.test.ts +1393 -0
  23. package/src/schemaRegistry.test.ts +118 -0
  24. package/src/schemaRegistry.ts +461 -0
  25. package/src/validators/validateArray.ts +4 -7
  26. package/src/validators/validateBoolean.ts +2 -11
  27. package/src/validators/validateFunction.ts +25 -0
  28. package/src/validators/validateNumber.ts +2 -7
  29. package/src/validators/validateObject.ts +32 -21
  30. package/src/validators/validateString.ts +2 -7
  31. package/src/validators/validateUnion.ts +36 -0
  32. package/dist/index.d.ts +0 -94
  33. package/dist/index.js +0 -9
  34. package/dist/schemaValidator.d.ts +0 -16
  35. package/dist/schemaValidator.js +0 -234
  36. package/dist/validators/validateArray.d.ts +0 -2
  37. package/dist/validators/validateArray.js +0 -60
  38. package/dist/validators/validateBoolean.d.ts +0 -2
  39. package/dist/validators/validateBoolean.js +0 -33
  40. package/dist/validators/validateNumber.d.ts +0 -2
  41. package/dist/validators/validateNumber.js +0 -69
  42. package/dist/validators/validateObject.d.ts +0 -2
  43. package/dist/validators/validateObject.js +0 -73
  44. package/dist/validators/validateString.d.ts +0 -2
  45. package/dist/validators/validateString.js +0 -50
  46. package/src/schemaValidator.test.ts +0 -1656
  47. package/src/schemaValidator.ts +0 -367
@@ -0,0 +1,794 @@
1
+ import { ISchemaBuilder, SchemaBuilder } from './SchemaBuilder.js';
2
+ import { defaultSchemas } from '../defaultSchemas.js';
3
+ import {
4
+ Schema,
5
+ Validator,
6
+ InferType,
7
+ MakeOptional,
8
+ MakeRequired,
9
+ MakeNullable,
10
+ MakeNotNullable
11
+ } from '../schema.js';
12
+
13
+ export interface IObjectSchemaBuilder<
14
+ TRequired extends boolean = true,
15
+ TNullable extends boolean = false,
16
+ TNoUnknownProperties extends boolean = true,
17
+ TProperties = {},
18
+ TMapToType = undefined
19
+ > extends ISchemaBuilder<TRequired, TNullable> {
20
+ optional(): IObjectSchemaBuilder<
21
+ false,
22
+ TNullable,
23
+ TNoUnknownProperties,
24
+ TProperties,
25
+ TMapToType
26
+ >;
27
+ required(): IObjectSchemaBuilder<
28
+ true,
29
+ TNullable,
30
+ TNoUnknownProperties,
31
+ TProperties,
32
+ TMapToType
33
+ >;
34
+ nullable(): IObjectSchemaBuilder<
35
+ TRequired,
36
+ true,
37
+ TNoUnknownProperties,
38
+ TProperties,
39
+ TMapToType
40
+ >;
41
+ notNullable(): IObjectSchemaBuilder<
42
+ TRequired,
43
+ false,
44
+ TNoUnknownProperties,
45
+ TProperties,
46
+ TMapToType
47
+ >;
48
+ addProps<T extends Record<string, any>>(
49
+ val?: T
50
+ ): IObjectSchemaBuilder<
51
+ TRequired,
52
+ TNullable,
53
+ TNoUnknownProperties,
54
+ TProperties & T,
55
+ TMapToType
56
+ >;
57
+ removeProp<T extends keyof TProperties>(
58
+ property: T
59
+ ): IObjectSchemaBuilder<
60
+ TRequired,
61
+ TNullable,
62
+ TNoUnknownProperties,
63
+ Omit<TProperties, T>,
64
+ TMapToType
65
+ >;
66
+ canHaveUnknownProps(): IObjectSchemaBuilder<
67
+ TRequired,
68
+ TNullable,
69
+ false,
70
+ TProperties,
71
+ TMapToType
72
+ >;
73
+ noUnknownProps(): IObjectSchemaBuilder<
74
+ TRequired,
75
+ TNullable,
76
+ true,
77
+ TProperties,
78
+ TMapToType
79
+ >;
80
+ setPropPreprocessor<
81
+ T extends keyof TProperties | '*',
82
+ S = T extends keyof TProperties
83
+ ?
84
+ | ((
85
+ value: any
86
+ ) =>
87
+ | undefined
88
+ | InferType<TProperties[T]>
89
+ | Promise<InferType<TProperties[T]>>)
90
+ | string
91
+ : (value: InferType<this>) => void | Promise<void>
92
+ >(
93
+ property: T,
94
+ validator: S
95
+ ): IObjectSchemaBuilder<
96
+ TRequired,
97
+ TNullable,
98
+ TNoUnknownProperties,
99
+ TProperties,
100
+ TMapToType
101
+ >;
102
+ unsetPropPreprocessor<T extends keyof TProperties | '*'>(
103
+ property: T
104
+ ): IObjectSchemaBuilder<
105
+ TRequired,
106
+ TNullable,
107
+ TNoUnknownProperties,
108
+ TProperties,
109
+ TMapToType
110
+ >;
111
+ clearPropsPreprocessors(): IObjectSchemaBuilder<
112
+ TRequired,
113
+ TNullable,
114
+ TNoUnknownProperties,
115
+ TProperties,
116
+ TMapToType
117
+ >;
118
+ makeAllPropsOptional<T extends keyof TProperties>(): IObjectSchemaBuilder<
119
+ TRequired,
120
+ TNullable,
121
+ TNoUnknownProperties,
122
+ { [k in T]: MakeOptional<TProperties[k]> },
123
+ TMapToType
124
+ >;
125
+ makePropOptional<T extends keyof TProperties>(
126
+ property: T
127
+ ): IObjectSchemaBuilder<
128
+ TRequired,
129
+ TNullable,
130
+ TNoUnknownProperties,
131
+ Omit<TProperties, T> & { [k in T]: MakeOptional<TProperties[k]> },
132
+ TMapToType
133
+ >;
134
+ makePropRequired<T extends keyof TProperties>(
135
+ property: T
136
+ ): IObjectSchemaBuilder<
137
+ TRequired,
138
+ TNullable,
139
+ TNoUnknownProperties,
140
+ Omit<TProperties, T> & { [k in T]: MakeRequired<TProperties[k]> },
141
+ TMapToType
142
+ >;
143
+ makePropNullable<T extends keyof TProperties>(
144
+ property: T
145
+ ): IObjectSchemaBuilder<
146
+ TRequired,
147
+ TNullable,
148
+ TNoUnknownProperties,
149
+ Omit<TProperties, T> & { [k in T]: MakeNullable<TProperties[k]> },
150
+ TMapToType
151
+ >;
152
+ makePropNotNullable<T extends keyof TProperties>(
153
+ property: T
154
+ ): IObjectSchemaBuilder<
155
+ TRequired,
156
+ TNullable,
157
+ TNoUnknownProperties,
158
+ Omit<TProperties, T> & { [k in T]: MakeNotNullable<TProperties[k]> },
159
+ TMapToType
160
+ >;
161
+ mapToType<T>(): IObjectSchemaBuilder<
162
+ TRequired,
163
+ TNullable,
164
+ TNoUnknownProperties,
165
+ TProperties,
166
+ T
167
+ >;
168
+ clearMapToType(): IObjectSchemaBuilder<
169
+ TRequired,
170
+ TNullable,
171
+ TNoUnknownProperties,
172
+ TProperties,
173
+ undefined
174
+ >;
175
+ }
176
+
177
+ export class ObjectSchemaBuilder<
178
+ TRequired extends boolean = true,
179
+ TNullable extends boolean = false,
180
+ TNoUnknownProperties extends boolean = true,
181
+ TProperties = {},
182
+ TMapToType = undefined
183
+ >
184
+ extends SchemaBuilder<TRequired, TNullable>
185
+ implements
186
+ IObjectSchemaBuilder<
187
+ TRequired,
188
+ TNullable,
189
+ TNoUnknownProperties,
190
+ TProperties,
191
+ TMapToType
192
+ >
193
+ {
194
+ protected readonly type = 'object';
195
+
196
+ protected noUnknownProperties?: TNoUnknownProperties;
197
+ protected properties?: TProperties;
198
+ protected preprocessors?: {
199
+ [S in keyof TProperties | '*']?: S extends keyof TProperties
200
+ ?
201
+ | ((
202
+ value: any
203
+ ) =>
204
+ | undefined
205
+ | InferType<TProperties[S]>
206
+ | Promise<InferType<TProperties[S]>>)
207
+ | string
208
+ : (value: InferType<TProperties>) => void | Promise<void>;
209
+ };
210
+
211
+ public clone(): this {
212
+ return ObjectSchemaBuilder.create({
213
+ isNullable: this._isNullable,
214
+ isRequired: this._isRequired,
215
+ noUnknownProperties: this.noUnknownProperties,
216
+ properties: this.properties,
217
+ preprocessor: this.preprocessor,
218
+ preprocessors: this.preprocessors,
219
+ validators: this.validators
220
+ }) as any as this;
221
+ }
222
+
223
+ public get _schema(): Schema {
224
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
225
+ const that = this;
226
+ return Object.assign(
227
+ {
228
+ type: this.type
229
+ },
230
+ this.getCommonSchema(),
231
+ typeof that.properties !== 'undefined' && that.properties
232
+ ? {
233
+ properties: Object.keys(that.properties).reduce(
234
+ (acc: any, curr) => {
235
+ if (!that.properties) return;
236
+ if (
237
+ that.properties[curr] instanceof SchemaBuilder
238
+ ) {
239
+ acc[curr] = that.properties[curr].clone();
240
+ } else {
241
+ acc[curr] = that.properties[curr];
242
+ }
243
+ return acc;
244
+ },
245
+ {}
246
+ )
247
+ }
248
+ : {},
249
+ typeof that.noUnknownProperties !== 'undefined'
250
+ ? { noUnknownProperties: that.noUnknownProperties }
251
+ : {},
252
+ typeof that.preprocessors !== 'undefined' && that.preprocessors
253
+ ? {
254
+ preprocessors: {
255
+ ...that.preprocessors
256
+ }
257
+ }
258
+ : {}
259
+ );
260
+ }
261
+
262
+ private constructor(obj: {
263
+ isRequired: TRequired;
264
+ isNullable: TNullable;
265
+ noUnknownProperties: TNoUnknownProperties;
266
+ properties: TProperties;
267
+ validators?: Validator[];
268
+ preprocessor?:
269
+ | ((value: unknown) => unknown | Promise<unknown>)
270
+ | string;
271
+ preprocessors?: {
272
+ [S in keyof TProperties | '*']?: S extends keyof TProperties
273
+ ?
274
+ | ((
275
+ value: any
276
+ ) =>
277
+ | undefined
278
+ | InferType<TProperties[S]>
279
+ | Promise<InferType<TProperties[S]>>)
280
+ | string
281
+ : (value: InferType<TProperties>) => void | Promise<void>;
282
+ };
283
+ }) {
284
+ super(obj);
285
+ if (typeof obj === 'object' && obj) {
286
+ if (typeof obj.properties !== 'undefined') {
287
+ this.properties = Object.keys(obj.properties).reduce(
288
+ (acc, curr) => {
289
+ if (obj.properties[curr] instanceof SchemaBuilder) {
290
+ acc[curr] = obj.properties[curr].clone();
291
+ } else {
292
+ acc[curr] = {
293
+ ...obj.properties[curr]
294
+ };
295
+ }
296
+ return acc;
297
+ },
298
+ {}
299
+ ) as any;
300
+ }
301
+ if (typeof obj.preprocessors !== 'undefined') {
302
+ this.preprocessors = { ...obj.preprocessors };
303
+ }
304
+ if (typeof obj.noUnknownProperties !== 'undefined') {
305
+ this.noUnknownProperties = obj.noUnknownProperties;
306
+ }
307
+ } else {
308
+ const defaultSchema = defaultSchemas['object'];
309
+ this.isRequired = (defaultSchema as any).isRequired;
310
+ this.isNullable = (defaultSchema as any).isNullable;
311
+ }
312
+ }
313
+
314
+ public static create<
315
+ TCRequired extends boolean = true,
316
+ TCNullable extends boolean = false,
317
+ TCNoUnknownProperties extends boolean = false,
318
+ TCProperties = {},
319
+ TMapToType = undefined
320
+ >(obj?: {
321
+ isRequired: TCRequired;
322
+ isNullable: TCNullable;
323
+ properties: TCProperties;
324
+ noUnknownProperties: TCNoUnknownProperties;
325
+ validators?: Validator[];
326
+ preprocessor?:
327
+ | ((value: unknown) => unknown | Promise<unknown>)
328
+ | string;
329
+ preprocessors?: {
330
+ [S in keyof TCProperties | '*']?: S extends keyof TCProperties
331
+ ?
332
+ | ((
333
+ value: any
334
+ ) =>
335
+ | undefined
336
+ | InferType<TCProperties[S]>
337
+ | Promise<InferType<TCProperties[S]>>)
338
+ | string
339
+ : (value: InferType<TCProperties>) => void | Promise<void>;
340
+ };
341
+ }): IObjectSchemaBuilder<
342
+ TCRequired,
343
+ TCNullable,
344
+ TCNoUnknownProperties,
345
+ TCProperties,
346
+ TMapToType
347
+ > {
348
+ return new ObjectSchemaBuilder<
349
+ TCRequired,
350
+ TCNullable,
351
+ TCNoUnknownProperties,
352
+ TCProperties,
353
+ TMapToType
354
+ >(obj as any);
355
+ }
356
+
357
+ public canHaveUnknownProps(): IObjectSchemaBuilder<
358
+ TRequired,
359
+ TNullable,
360
+ false,
361
+ TProperties,
362
+ TMapToType
363
+ > {
364
+ if (this.noUnknownProperties === false) {
365
+ return this as IObjectSchemaBuilder<
366
+ TRequired,
367
+ TNullable,
368
+ false,
369
+ TProperties
370
+ >;
371
+ }
372
+ return ObjectSchemaBuilder.create({
373
+ ...(this._schema as any),
374
+ noUnknownProperties: false
375
+ });
376
+ }
377
+
378
+ public noUnknownProps(): IObjectSchemaBuilder<
379
+ TRequired,
380
+ TNullable,
381
+ true,
382
+ TProperties,
383
+ TMapToType
384
+ > {
385
+ if (this.noUnknownProperties === true) {
386
+ return this as any;
387
+ }
388
+ return ObjectSchemaBuilder.create({
389
+ ...(this._schema as any),
390
+ noUnknownProperties: true
391
+ });
392
+ }
393
+
394
+ public optional(): IObjectSchemaBuilder<
395
+ false,
396
+ TNullable,
397
+ TNoUnknownProperties,
398
+ TProperties,
399
+ TMapToType
400
+ > {
401
+ if (this.isRequired === false) {
402
+ return this as any;
403
+ }
404
+ return ObjectSchemaBuilder.create({
405
+ ...(this._schema as any),
406
+ isRequired: false
407
+ });
408
+ }
409
+
410
+ public required(): IObjectSchemaBuilder<
411
+ true,
412
+ TNullable,
413
+ TNoUnknownProperties,
414
+ TProperties,
415
+ TMapToType
416
+ > {
417
+ if (this.isRequired === true) {
418
+ return this as any;
419
+ }
420
+ return ObjectSchemaBuilder.create({
421
+ ...(this._schema as any),
422
+ isRequired: true
423
+ });
424
+ }
425
+
426
+ public nullable(): IObjectSchemaBuilder<
427
+ TRequired,
428
+ true,
429
+ TNoUnknownProperties,
430
+ TProperties,
431
+ TMapToType
432
+ > {
433
+ if (this.isNullable === true) {
434
+ return this as IObjectSchemaBuilder<
435
+ TRequired,
436
+ true,
437
+ TNoUnknownProperties,
438
+ TProperties
439
+ >;
440
+ }
441
+ return ObjectSchemaBuilder.create({
442
+ ...(this._schema as any),
443
+ isNullable: true
444
+ });
445
+ }
446
+
447
+ public notNullable(): IObjectSchemaBuilder<
448
+ TRequired,
449
+ false,
450
+ TNoUnknownProperties,
451
+ TProperties,
452
+ TMapToType
453
+ > {
454
+ if (this.isNullable === false) {
455
+ return this as any;
456
+ }
457
+ return ObjectSchemaBuilder.create({
458
+ ...(this._schema as any),
459
+ isNullable: false
460
+ });
461
+ }
462
+
463
+ public addProps<T extends Record<string, any>>(
464
+ val?: T
465
+ ): IObjectSchemaBuilder<
466
+ TRequired,
467
+ TNullable,
468
+ TNoUnknownProperties,
469
+ TProperties & T,
470
+ TMapToType
471
+ > {
472
+ if (typeof val === 'undefined') return this as any;
473
+ return ObjectSchemaBuilder.create({
474
+ ...(this._schema as any),
475
+ properties: {
476
+ ...this.properties,
477
+ ...val
478
+ }
479
+ });
480
+ }
481
+
482
+ public removeProp<T extends keyof TProperties>(
483
+ property: T
484
+ ): IObjectSchemaBuilder<
485
+ TRequired,
486
+ TNullable,
487
+ TNoUnknownProperties,
488
+ Omit<TProperties, T>,
489
+ TMapToType
490
+ > {
491
+ if (typeof this.properties === 'undefined') {
492
+ return this as any;
493
+ }
494
+
495
+ if (!(property in this.properties)) {
496
+ return this as any;
497
+ }
498
+
499
+ const newProperties = {
500
+ ...this.properties
501
+ };
502
+
503
+ delete newProperties[property];
504
+
505
+ let newPreprocessors = this.preprocessors;
506
+ if (
507
+ typeof newPreprocessors !== 'undefined' &&
508
+ property in newPreprocessors
509
+ ) {
510
+ if (Object.keys(newPreprocessors).length === 1) {
511
+ newPreprocessors = undefined;
512
+ } else {
513
+ newPreprocessors = {
514
+ ...this.preprocessors
515
+ } as any;
516
+ delete (newPreprocessors as any)[property];
517
+ }
518
+ }
519
+ return ObjectSchemaBuilder.create({
520
+ ...(this._schema as any),
521
+ preprocessors: newPreprocessors,
522
+ properties: newProperties
523
+ });
524
+ }
525
+
526
+ public setPropPreprocessor<
527
+ T extends keyof TProperties | '*',
528
+ S = T extends keyof TProperties
529
+ ?
530
+ | ((
531
+ value: any
532
+ ) =>
533
+ | undefined
534
+ | InferType<TProperties[T]>
535
+ | Promise<InferType<TProperties[T]>>)
536
+ | string
537
+ : (value: InferType<T>) => void | Promise<void>
538
+ >(
539
+ property: T,
540
+ validator: S
541
+ ): IObjectSchemaBuilder<
542
+ TRequired,
543
+ TNullable,
544
+ TNoUnknownProperties,
545
+ TProperties,
546
+ TMapToType
547
+ > {
548
+ if (typeof this.preprocessors === 'undefined') {
549
+ this.preprocessors = {
550
+ [property]: validator
551
+ } as any;
552
+ } else {
553
+ this.preprocessors = {
554
+ ...this.preprocessors,
555
+ [property]: validator
556
+ };
557
+ }
558
+ return ObjectSchemaBuilder.create({
559
+ ...(this._schema as any),
560
+ preprocessors: {
561
+ ...this.preprocessors
562
+ }
563
+ });
564
+ }
565
+
566
+ public unsetPropPreprocessor<T extends keyof TProperties | '*'>(
567
+ property: T
568
+ ): IObjectSchemaBuilder<
569
+ TRequired,
570
+ TNullable,
571
+ TNoUnknownProperties,
572
+ TProperties,
573
+ TMapToType
574
+ > {
575
+ if (
576
+ typeof this.preprocessors === 'undefined' ||
577
+ !(property in this.preprocessors)
578
+ ) {
579
+ return this;
580
+ }
581
+ let newPreprocessors;
582
+ if (Object.keys(this.preprocessors).length === 1) {
583
+ newPreprocessors = undefined;
584
+ } else {
585
+ newPreprocessors = {
586
+ ...this.preprocessors
587
+ };
588
+ delete newPreprocessors[property];
589
+ }
590
+ return ObjectSchemaBuilder.create({
591
+ ...(this._schema as any),
592
+ preprocessors: newPreprocessors
593
+ });
594
+ }
595
+
596
+ public clearPropsPreprocessors(): IObjectSchemaBuilder<
597
+ TRequired,
598
+ TNullable,
599
+ TNoUnknownProperties,
600
+ TProperties,
601
+ TMapToType
602
+ > {
603
+ if (typeof this.preprocessors === 'undefined') {
604
+ return this;
605
+ }
606
+ return ObjectSchemaBuilder.create({
607
+ ...(this._schema as any),
608
+ preprocessors: undefined
609
+ });
610
+ }
611
+
612
+ public mapToType<T>(): IObjectSchemaBuilder<
613
+ TRequired,
614
+ TNullable,
615
+ TNoUnknownProperties,
616
+ TProperties,
617
+ T
618
+ > {
619
+ return this as any;
620
+ }
621
+
622
+ public clearMapToType(): IObjectSchemaBuilder<
623
+ TRequired,
624
+ TNullable,
625
+ TNoUnknownProperties,
626
+ TProperties,
627
+ undefined
628
+ > {
629
+ return this as any;
630
+ }
631
+
632
+ public makeAllPropsOptional<
633
+ T extends keyof TProperties
634
+ >(): IObjectSchemaBuilder<
635
+ TRequired,
636
+ TNullable,
637
+ TNoUnknownProperties,
638
+ { [k in T]: MakeOptional<TProperties[k]> },
639
+ TMapToType
640
+ > {
641
+ let res = this;
642
+
643
+ for (const prop in this.properties) {
644
+ res = res.makePropOptional(prop) as any;
645
+ }
646
+
647
+ return res as any;
648
+ }
649
+
650
+ public makePropOptional<T extends keyof TProperties>(
651
+ property: T
652
+ ): IObjectSchemaBuilder<
653
+ TRequired,
654
+ TNullable,
655
+ TNoUnknownProperties,
656
+ Omit<TProperties, T> & { [k in T]: MakeOptional<TProperties[k]> },
657
+ TMapToType
658
+ > {
659
+ if (typeof this.properties[property] === 'undefined')
660
+ throw new Error(`Property ${property as string} does not exists`);
661
+ if (this.properties[property] instanceof SchemaBuilder) {
662
+ return ObjectSchemaBuilder.create({
663
+ ...(this._schema as any),
664
+ properties: {
665
+ ...this.properties,
666
+ [property]: (this.properties[property] as any).optional()
667
+ }
668
+ });
669
+ } else {
670
+ return ObjectSchemaBuilder.create({
671
+ ...(this._schema as any),
672
+ properties: {
673
+ ...this.properties,
674
+ [property]: {
675
+ ...this.properties[property],
676
+ isRequired: false
677
+ }
678
+ }
679
+ });
680
+ }
681
+ }
682
+
683
+ public makePropRequired<T extends keyof TProperties>(
684
+ property: T
685
+ ): IObjectSchemaBuilder<
686
+ TRequired,
687
+ TNullable,
688
+ TNoUnknownProperties,
689
+ Omit<TProperties, T> & { [k in T]: MakeRequired<TProperties[k]> },
690
+ TMapToType
691
+ > {
692
+ if (typeof this.properties[property] === 'undefined')
693
+ throw new Error(`Property ${property as string} does not exists`);
694
+ if (this.properties[property] instanceof SchemaBuilder) {
695
+ return ObjectSchemaBuilder.create({
696
+ ...(this._schema as any),
697
+ properties: {
698
+ ...this.properties,
699
+ [property]: (this.properties[property] as any).required()
700
+ }
701
+ });
702
+ } else {
703
+ return ObjectSchemaBuilder.create({
704
+ ...(this._schema as any),
705
+ properties: {
706
+ ...this.properties,
707
+ [property]: {
708
+ ...this.properties[property],
709
+ isRequired: true
710
+ }
711
+ }
712
+ });
713
+ }
714
+ }
715
+
716
+ public makePropNullable<T extends keyof TProperties>(
717
+ property: T
718
+ ): IObjectSchemaBuilder<
719
+ TRequired,
720
+ TNullable,
721
+ TNoUnknownProperties,
722
+ Omit<TProperties, T> & { [k in T]: MakeNullable<TProperties[k]> },
723
+ TMapToType
724
+ > {
725
+ if (typeof this.properties[property] === 'undefined')
726
+ throw new Error(`Property ${property as string} does not exists`);
727
+ if (this.properties[property] instanceof SchemaBuilder) {
728
+ return ObjectSchemaBuilder.create({
729
+ ...(this._schema as any),
730
+ properties: {
731
+ ...this.properties,
732
+ [property]: (this.properties[property] as any).nullable()
733
+ }
734
+ });
735
+ } else {
736
+ return ObjectSchemaBuilder.create({
737
+ ...(this._schema as any),
738
+ properties: {
739
+ ...this.properties,
740
+ [property]: {
741
+ ...this.properties[property],
742
+ isNullable: true
743
+ }
744
+ }
745
+ });
746
+ }
747
+ }
748
+
749
+ public makePropNotNullable<T extends keyof TProperties>(
750
+ property: T
751
+ ): IObjectSchemaBuilder<
752
+ TRequired,
753
+ TNullable,
754
+ TNoUnknownProperties,
755
+ Omit<TProperties, T> & { [k in T]: MakeNotNullable<TProperties[k]> },
756
+ TMapToType
757
+ > {
758
+ if (typeof this.properties[property] === 'undefined')
759
+ throw new Error(`Property ${property as string} does not exists`);
760
+ if (this.properties[property] instanceof SchemaBuilder) {
761
+ return ObjectSchemaBuilder.create({
762
+ ...(this._schema as any),
763
+ properties: {
764
+ ...this.properties,
765
+ [property]: (this.properties[property] as any).notNullable()
766
+ }
767
+ });
768
+ } else {
769
+ return ObjectSchemaBuilder.create({
770
+ ...(this._schema as any),
771
+ properties: {
772
+ ...this.properties,
773
+ [property]: {
774
+ ...this.properties[property],
775
+ isNullable: false
776
+ }
777
+ }
778
+ });
779
+ }
780
+ }
781
+ }
782
+
783
+ export const object = <T>(
784
+ properties?: T
785
+ ): IObjectSchemaBuilder<
786
+ true,
787
+ false,
788
+ true,
789
+ T extends undefined ? {} : T,
790
+ undefined
791
+ > =>
792
+ typeof properties === 'undefined'
793
+ ? ObjectSchemaBuilder.create()
794
+ : (ObjectSchemaBuilder.create().addProps(properties) as any);