schematox 1.2.2 → 1.2.3-alpha

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.
@@ -1,1252 +0,0 @@
1
- import * as x from '../../'
2
- import * as fixture from '../fixtures'
3
-
4
- import type { StructSharedKeys } from '../type'
5
-
6
- describe('Type inference and parse by schema/construct/struct (foldA)', () => {
7
- it('required', () => {
8
- const schema = { type: 'string' } as const satisfies x.Schema
9
- const struct = x.string()
10
-
11
- type ExpectedSubj = string
12
-
13
- const subjects: Array<ExpectedSubj> = fixture.DATA_VARIANTS_BY_TYPE.string
14
-
15
- foldA: {
16
- const construct = x.makeStruct(schema)
17
-
18
- /* ensure that schema/construct/struct/~standard subject types are identical */
19
-
20
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
21
-
22
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
23
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
24
-
25
- type SchemaSubj = x.Infer<typeof schema>
26
-
27
- x.tCh<SchemaSubj, ExpectedSubj>()
28
- x.tCh<ExpectedSubj, SchemaSubj>()
29
-
30
- type StructSubj = x.Infer<typeof struct.__schema>
31
-
32
- x.tCh<StructSubj, ExpectedSubj>()
33
- x.tCh<ExpectedSubj, StructSubj>()
34
-
35
- type StandardSubj = NonNullable<
36
- (typeof struct)['~standard']['types']
37
- >['output']
38
-
39
- x.tCh<StandardSubj, ExpectedSubj>()
40
- x.tCh<ExpectedSubj, StandardSubj>()
41
-
42
- /* parsed either type check */
43
-
44
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
45
-
46
- const parsed = x.parse(schema, undefined)
47
-
48
- type SchemaParsed = typeof parsed
49
-
50
- x.tCh<SchemaParsed, ExpectedParsed>()
51
- x.tCh<ExpectedParsed, SchemaParsed>()
52
-
53
- type ConstructParsed = ReturnType<typeof construct.parse>
54
-
55
- x.tCh<ConstructParsed, ExpectedParsed>()
56
- x.tCh<ExpectedParsed, ConstructParsed>()
57
-
58
- type StructParsed = ReturnType<typeof struct.parse>
59
-
60
- x.tCh<StructParsed, ExpectedParsed>()
61
- x.tCh<ExpectedParsed, StructParsed>()
62
-
63
- type StandardParsed = Extract<
64
- ReturnType<(typeof struct)['~standard']['validate']>,
65
- { value: unknown }
66
- >['value']
67
-
68
- x.tCh<StandardParsed, ExpectedSubj>()
69
- x.tCh<ExpectedSubj, StandardParsed>()
70
-
71
- /* runtime schema check */
72
-
73
- expect(struct.__schema).toStrictEqual(schema)
74
- expect(construct.__schema).toStrictEqual(schema)
75
- expect(construct.__schema === schema).toBe(false)
76
-
77
- /* parse result check */
78
-
79
- for (const subj of subjects) {
80
- const schemaParsed = x.parse(schema, subj)
81
-
82
- expect(schemaParsed.error).toBe(undefined)
83
- expect(schemaParsed.data).toStrictEqual(subj)
84
-
85
- const constructParsed = construct.parse(subj)
86
-
87
- expect(constructParsed.error).toBe(undefined)
88
- expect(constructParsed.data).toStrictEqual(subj)
89
-
90
- const structParsed = struct.parse(subj)
91
-
92
- expect(structParsed.error).toBe(undefined)
93
- expect(structParsed.data).toStrictEqual(subj)
94
-
95
- const standardParsed = struct['~standard'].validate(subj)
96
-
97
- if (standardParsed instanceof Promise) {
98
- throw Error('Not expected')
99
- }
100
-
101
- if (standardParsed.issues !== undefined) {
102
- throw Error('not expected')
103
- }
104
-
105
- expect(standardParsed.value).toStrictEqual(subj)
106
- }
107
- }
108
- })
109
-
110
- it('optional', () => {
111
- const schema = {
112
- type: 'string',
113
- optional: true,
114
- } as const satisfies x.Schema
115
-
116
- const struct = x.string().optional()
117
-
118
- type ExpectedSubj = string | undefined
119
-
120
- const subjects: Array<ExpectedSubj> = [
121
- ...fixture.DATA_VARIANTS_BY_TYPE.string,
122
- undefined,
123
- ]
124
-
125
- foldA: {
126
- const construct = x.makeStruct(schema)
127
-
128
- /* ensure that schema/construct/struct/~standard subject types are identical */
129
-
130
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
131
-
132
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
133
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
134
-
135
- type SchemaSubj = x.Infer<typeof schema>
136
-
137
- x.tCh<SchemaSubj, ExpectedSubj>()
138
- x.tCh<ExpectedSubj, SchemaSubj>()
139
-
140
- type StructSubj = x.Infer<typeof struct.__schema>
141
-
142
- x.tCh<StructSubj, ExpectedSubj>()
143
- x.tCh<ExpectedSubj, StructSubj>()
144
-
145
- type StandardSubj = NonNullable<
146
- (typeof struct)['~standard']['types']
147
- >['output']
148
-
149
- x.tCh<StandardSubj, ExpectedSubj>()
150
- x.tCh<ExpectedSubj, StandardSubj>()
151
-
152
- /* parsed either type check */
153
-
154
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
155
-
156
- const parsed = x.parse(schema, undefined)
157
-
158
- type SchemaParsed = typeof parsed
159
-
160
- x.tCh<SchemaParsed, ExpectedParsed>()
161
- x.tCh<ExpectedParsed, SchemaParsed>()
162
-
163
- type ConstructParsed = ReturnType<typeof construct.parse>
164
-
165
- x.tCh<ConstructParsed, ExpectedParsed>()
166
- x.tCh<ExpectedParsed, ConstructParsed>()
167
-
168
- type StructParsed = ReturnType<typeof struct.parse>
169
-
170
- x.tCh<StructParsed, ExpectedParsed>()
171
- x.tCh<ExpectedParsed, StructParsed>()
172
-
173
- type StandardParsed = Extract<
174
- ReturnType<(typeof struct)['~standard']['validate']>,
175
- { value: unknown }
176
- >['value']
177
-
178
- x.tCh<StandardParsed, ExpectedSubj>()
179
- x.tCh<ExpectedSubj, StandardParsed>()
180
-
181
- /* runtime schema check */
182
-
183
- expect(struct.__schema).toStrictEqual(schema)
184
- expect(construct.__schema).toStrictEqual(schema)
185
- expect(construct.__schema === schema).toBe(false)
186
-
187
- /* parse result check */
188
-
189
- for (const subj of subjects) {
190
- const schemaParsed = x.parse(schema, subj)
191
-
192
- expect(schemaParsed.error).toBe(undefined)
193
- expect(schemaParsed.data).toStrictEqual(subj)
194
-
195
- const constructParsed = construct.parse(subj)
196
-
197
- expect(constructParsed.error).toBe(undefined)
198
- expect(constructParsed.data).toStrictEqual(subj)
199
-
200
- const structParsed = struct.parse(subj)
201
-
202
- expect(structParsed.error).toBe(undefined)
203
- expect(structParsed.data).toStrictEqual(subj)
204
-
205
- const standardParsed = struct['~standard'].validate(subj)
206
-
207
- if (standardParsed instanceof Promise) {
208
- throw Error('Not expected')
209
- }
210
-
211
- if (standardParsed.issues !== undefined) {
212
- throw Error('not expected')
213
- }
214
-
215
- expect(standardParsed.value).toStrictEqual(subj)
216
- }
217
- }
218
- })
219
-
220
- it('nullable', () => {
221
- const schema = {
222
- type: 'string',
223
- nullable: true,
224
- } as const satisfies x.Schema
225
-
226
- const struct = x.string().nullable()
227
-
228
- type ExpectedSubj = string | null
229
-
230
- const subjects: Array<ExpectedSubj> = [
231
- ...fixture.DATA_VARIANTS_BY_TYPE.string,
232
- null,
233
- ]
234
-
235
- foldA: {
236
- const construct = x.makeStruct(schema)
237
-
238
- /* ensure that schema/construct/struct/~standard subject types are identical */
239
-
240
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
241
-
242
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
243
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
244
-
245
- type SchemaSubj = x.Infer<typeof schema>
246
-
247
- x.tCh<SchemaSubj, ExpectedSubj>()
248
- x.tCh<ExpectedSubj, SchemaSubj>()
249
-
250
- type StructSubj = x.Infer<typeof struct.__schema>
251
-
252
- x.tCh<StructSubj, ExpectedSubj>()
253
- x.tCh<ExpectedSubj, StructSubj>()
254
-
255
- type StandardSubj = NonNullable<
256
- (typeof struct)['~standard']['types']
257
- >['output']
258
-
259
- x.tCh<StandardSubj, ExpectedSubj>()
260
- x.tCh<ExpectedSubj, StandardSubj>()
261
-
262
- /* parsed either type check */
263
-
264
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
265
-
266
- const parsed = x.parse(schema, undefined)
267
-
268
- type SchemaParsed = typeof parsed
269
-
270
- x.tCh<SchemaParsed, ExpectedParsed>()
271
- x.tCh<ExpectedParsed, SchemaParsed>()
272
-
273
- type ConstructParsed = ReturnType<typeof construct.parse>
274
-
275
- x.tCh<ConstructParsed, ExpectedParsed>()
276
- x.tCh<ExpectedParsed, ConstructParsed>()
277
-
278
- type StructParsed = ReturnType<typeof struct.parse>
279
-
280
- x.tCh<StructParsed, ExpectedParsed>()
281
- x.tCh<ExpectedParsed, StructParsed>()
282
-
283
- type StandardParsed = Extract<
284
- ReturnType<(typeof struct)['~standard']['validate']>,
285
- { value: unknown }
286
- >['value']
287
-
288
- x.tCh<StandardParsed, ExpectedSubj>()
289
- x.tCh<ExpectedSubj, StandardParsed>()
290
-
291
- /* runtime schema check */
292
-
293
- expect(struct.__schema).toStrictEqual(schema)
294
- expect(construct.__schema).toStrictEqual(schema)
295
- expect(construct.__schema === schema).toBe(false)
296
-
297
- /* parse result check */
298
-
299
- for (const subj of subjects) {
300
- const schemaParsed = x.parse(schema, subj)
301
-
302
- expect(schemaParsed.error).toBe(undefined)
303
- expect(schemaParsed.data).toStrictEqual(subj)
304
-
305
- const constructParsed = construct.parse(subj)
306
-
307
- expect(constructParsed.error).toBe(undefined)
308
- expect(constructParsed.data).toStrictEqual(subj)
309
-
310
- const structParsed = struct.parse(subj)
311
-
312
- expect(structParsed.error).toBe(undefined)
313
- expect(structParsed.data).toStrictEqual(subj)
314
-
315
- const standardParsed = struct['~standard'].validate(subj)
316
-
317
- if (standardParsed instanceof Promise) {
318
- throw Error('Not expected')
319
- }
320
-
321
- if (standardParsed.issues !== undefined) {
322
- throw Error('not expected')
323
- }
324
-
325
- expect(standardParsed.value).toStrictEqual(subj)
326
- }
327
- }
328
- })
329
-
330
- it('minLength', () => {
331
- const schema = {
332
- type: 'string',
333
- minLength: 2,
334
- } as const satisfies x.Schema
335
-
336
- const struct = x.string().minLength(schema.minLength)
337
-
338
- type ExpectedSubj = string
339
-
340
- const subjects: Array<ExpectedSubj> = ['xx', 'xxx', 'xxxx']
341
-
342
- foldA: {
343
- const construct = x.makeStruct(schema)
344
-
345
- /* ensure that schema/construct/struct/~standard subject types are identical */
346
-
347
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
348
-
349
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
350
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
351
-
352
- type SchemaSubj = x.Infer<typeof schema>
353
-
354
- x.tCh<SchemaSubj, ExpectedSubj>()
355
- x.tCh<ExpectedSubj, SchemaSubj>()
356
-
357
- type StructSubj = x.Infer<typeof struct.__schema>
358
-
359
- x.tCh<StructSubj, ExpectedSubj>()
360
- x.tCh<ExpectedSubj, StructSubj>()
361
-
362
- type StandardSubj = NonNullable<
363
- (typeof struct)['~standard']['types']
364
- >['output']
365
-
366
- x.tCh<StandardSubj, ExpectedSubj>()
367
- x.tCh<ExpectedSubj, StandardSubj>()
368
-
369
- /* parsed either type check */
370
-
371
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
372
-
373
- const parsed = x.parse(schema, undefined)
374
-
375
- type SchemaParsed = typeof parsed
376
-
377
- x.tCh<SchemaParsed, ExpectedParsed>()
378
- x.tCh<ExpectedParsed, SchemaParsed>()
379
-
380
- type ConstructParsed = ReturnType<typeof construct.parse>
381
-
382
- x.tCh<ConstructParsed, ExpectedParsed>()
383
- x.tCh<ExpectedParsed, ConstructParsed>()
384
-
385
- type StructParsed = ReturnType<typeof struct.parse>
386
-
387
- x.tCh<StructParsed, ExpectedParsed>()
388
- x.tCh<ExpectedParsed, StructParsed>()
389
-
390
- type StandardParsed = Extract<
391
- ReturnType<(typeof struct)['~standard']['validate']>,
392
- { value: unknown }
393
- >['value']
394
-
395
- x.tCh<StandardParsed, ExpectedSubj>()
396
- x.tCh<ExpectedSubj, StandardParsed>()
397
-
398
- /* runtime schema check */
399
-
400
- expect(struct.__schema).toStrictEqual(schema)
401
- expect(construct.__schema).toStrictEqual(schema)
402
- expect(construct.__schema === schema).toBe(false)
403
-
404
- /* parse result check */
405
-
406
- for (const subj of subjects) {
407
- const schemaParsed = x.parse(schema, subj)
408
-
409
- expect(schemaParsed.error).toBe(undefined)
410
- expect(schemaParsed.data).toStrictEqual(subj)
411
-
412
- const constructParsed = construct.parse(subj)
413
-
414
- expect(constructParsed.error).toBe(undefined)
415
- expect(constructParsed.data).toStrictEqual(subj)
416
-
417
- const structParsed = struct.parse(subj)
418
-
419
- expect(structParsed.error).toBe(undefined)
420
- expect(structParsed.data).toStrictEqual(subj)
421
-
422
- const standardParsed = struct['~standard'].validate(subj)
423
-
424
- if (standardParsed instanceof Promise) {
425
- throw Error('Not expected')
426
- }
427
-
428
- if (standardParsed.issues !== undefined) {
429
- throw Error('not expected')
430
- }
431
-
432
- expect(standardParsed.value).toStrictEqual(subj)
433
- }
434
- }
435
- })
436
-
437
- it('maxLength', () => {
438
- const schema = {
439
- type: 'string',
440
- maxLength: 2,
441
- } as const satisfies x.Schema
442
-
443
- const struct = x.string().maxLength(schema.maxLength)
444
-
445
- type ExpectedSubj = string
446
-
447
- const subjects: Array<ExpectedSubj> = ['', 'x', 'xx']
448
-
449
- foldA: {
450
- const construct = x.makeStruct(schema)
451
-
452
- /* ensure that schema/construct/struct/~standard subject types are identical */
453
-
454
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
455
-
456
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
457
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
458
-
459
- type SchemaSubj = x.Infer<typeof schema>
460
-
461
- x.tCh<SchemaSubj, ExpectedSubj>()
462
- x.tCh<ExpectedSubj, SchemaSubj>()
463
-
464
- type StructSubj = x.Infer<typeof struct.__schema>
465
-
466
- x.tCh<StructSubj, ExpectedSubj>()
467
- x.tCh<ExpectedSubj, StructSubj>()
468
-
469
- type StandardSubj = NonNullable<
470
- (typeof struct)['~standard']['types']
471
- >['output']
472
-
473
- x.tCh<StandardSubj, ExpectedSubj>()
474
- x.tCh<ExpectedSubj, StandardSubj>()
475
-
476
- /* parsed either type check */
477
-
478
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
479
-
480
- const parsed = x.parse(schema, undefined)
481
-
482
- type SchemaParsed = typeof parsed
483
-
484
- x.tCh<SchemaParsed, ExpectedParsed>()
485
- x.tCh<ExpectedParsed, SchemaParsed>()
486
-
487
- type ConstructParsed = ReturnType<typeof construct.parse>
488
-
489
- x.tCh<ConstructParsed, ExpectedParsed>()
490
- x.tCh<ExpectedParsed, ConstructParsed>()
491
-
492
- type StructParsed = ReturnType<typeof struct.parse>
493
-
494
- x.tCh<StructParsed, ExpectedParsed>()
495
- x.tCh<ExpectedParsed, StructParsed>()
496
-
497
- type StandardParsed = Extract<
498
- ReturnType<(typeof struct)['~standard']['validate']>,
499
- { value: unknown }
500
- >['value']
501
-
502
- x.tCh<StandardParsed, ExpectedSubj>()
503
- x.tCh<ExpectedSubj, StandardParsed>()
504
-
505
- /* runtime schema check */
506
-
507
- expect(struct.__schema).toStrictEqual(schema)
508
- expect(construct.__schema).toStrictEqual(schema)
509
- expect(construct.__schema === schema).toBe(false)
510
-
511
- /* parse result check */
512
-
513
- for (const subj of subjects) {
514
- const schemaParsed = x.parse(schema, subj)
515
-
516
- expect(schemaParsed.error).toBe(undefined)
517
- expect(schemaParsed.data).toStrictEqual(subj)
518
-
519
- const constructParsed = construct.parse(subj)
520
-
521
- expect(constructParsed.error).toBe(undefined)
522
- expect(constructParsed.data).toStrictEqual(subj)
523
-
524
- const structParsed = struct.parse(subj)
525
-
526
- expect(structParsed.error).toBe(undefined)
527
- expect(structParsed.data).toStrictEqual(subj)
528
-
529
- const standardParsed = struct['~standard'].validate(subj)
530
-
531
- if (standardParsed instanceof Promise) {
532
- throw Error('Not expected')
533
- }
534
-
535
- if (standardParsed.issues !== undefined) {
536
- throw Error('not expected')
537
- }
538
-
539
- expect(standardParsed.value).toStrictEqual(subj)
540
- }
541
- }
542
- })
543
-
544
- it('optional + nullable + minLength + maxLength', () => {
545
- const schema = {
546
- type: 'string',
547
- optional: true,
548
- nullable: true,
549
- minLength: 0,
550
- maxLength: 1,
551
- } as const satisfies x.Schema
552
-
553
- const struct = x
554
- .string()
555
- .optional()
556
- .nullable()
557
- .minLength(schema.minLength)
558
- .maxLength(schema.maxLength)
559
-
560
- type ExpectedSubj = string | undefined | null
561
-
562
- const subjects: Array<ExpectedSubj> = ['', 'x', null, undefined]
563
-
564
- foldA: {
565
- const construct = x.makeStruct(schema)
566
-
567
- /* ensure that schema/construct/struct/~standard subject types are identical */
568
-
569
- type ConstructSchemaSubj = x.Infer<typeof construct.__schema>
570
-
571
- x.tCh<ConstructSchemaSubj, ExpectedSubj>()
572
- x.tCh<ExpectedSubj, ConstructSchemaSubj>()
573
-
574
- type SchemaSubj = x.Infer<typeof schema>
575
-
576
- x.tCh<SchemaSubj, ExpectedSubj>()
577
- x.tCh<ExpectedSubj, SchemaSubj>()
578
-
579
- type StructSubj = x.Infer<typeof struct.__schema>
580
-
581
- x.tCh<StructSubj, ExpectedSubj>()
582
- x.tCh<ExpectedSubj, StructSubj>()
583
-
584
- type StandardSubj = NonNullable<
585
- (typeof struct)['~standard']['types']
586
- >['output']
587
-
588
- x.tCh<StandardSubj, ExpectedSubj>()
589
- x.tCh<ExpectedSubj, StandardSubj>()
590
-
591
- /* parsed either type check */
592
-
593
- type ExpectedParsed = x.ParseResult<ExpectedSubj>
594
-
595
- const parsed = x.parse(schema, undefined)
596
-
597
- type SchemaParsed = typeof parsed
598
-
599
- x.tCh<SchemaParsed, ExpectedParsed>()
600
- x.tCh<ExpectedParsed, SchemaParsed>()
601
-
602
- type ConstructParsed = ReturnType<typeof construct.parse>
603
-
604
- x.tCh<ConstructParsed, ExpectedParsed>()
605
- x.tCh<ExpectedParsed, ConstructParsed>()
606
-
607
- type StructParsed = ReturnType<typeof struct.parse>
608
-
609
- x.tCh<StructParsed, ExpectedParsed>()
610
- x.tCh<ExpectedParsed, StructParsed>()
611
-
612
- type StandardParsed = Extract<
613
- ReturnType<(typeof struct)['~standard']['validate']>,
614
- { value: unknown }
615
- >['value']
616
-
617
- x.tCh<StandardParsed, ExpectedSubj>()
618
- x.tCh<ExpectedSubj, StandardParsed>()
619
-
620
- /* runtime schema check */
621
-
622
- expect(struct.__schema).toStrictEqual(schema)
623
- expect(construct.__schema).toStrictEqual(schema)
624
- expect(construct.__schema === schema).toBe(false)
625
-
626
- /* parse result check */
627
-
628
- for (const subj of subjects) {
629
- const schemaParsed = x.parse(schema, subj)
630
-
631
- expect(schemaParsed.error).toBe(undefined)
632
- expect(schemaParsed.data).toStrictEqual(subj)
633
-
634
- const constructParsed = construct.parse(subj)
635
-
636
- expect(constructParsed.error).toBe(undefined)
637
- expect(constructParsed.data).toStrictEqual(subj)
638
-
639
- const structParsed = struct.parse(subj)
640
-
641
- expect(structParsed.error).toBe(undefined)
642
- expect(structParsed.data).toStrictEqual(subj)
643
-
644
- const standardParsed = struct['~standard'].validate(subj)
645
-
646
- if (standardParsed instanceof Promise) {
647
- throw Error('Not expected')
648
- }
649
-
650
- if (standardParsed.issues !== undefined) {
651
- throw Error('not expected')
652
- }
653
-
654
- expect(standardParsed.value).toStrictEqual(subj)
655
- }
656
- }
657
- })
658
- })
659
-
660
- describe('Struct parameter keys reduction and schema immutability (foldB)', () => {
661
- it('optional', () => {
662
- const schema = {
663
- type: 'string',
664
- optional: true,
665
- } as const satisfies x.Schema
666
-
667
- const prevStruct = x.string()
668
- const struct = prevStruct.optional()
669
-
670
- type ExpectedKeys =
671
- | StructSharedKeys
672
- | 'brand'
673
- | 'description'
674
- | 'maxLength'
675
- | 'minLength'
676
- | 'nullable'
677
-
678
- foldB: {
679
- const construct = x.makeStruct(schema)
680
-
681
- /* ensure that struct keys are reduced after application */
682
-
683
- type StructKeys = keyof typeof struct
684
-
685
- x.tCh<StructKeys, ExpectedKeys>()
686
- x.tCh<ExpectedKeys, StructKeys>()
687
-
688
- type ConstructKeys = keyof typeof construct
689
-
690
- x.tCh<ConstructKeys, ExpectedKeys>()
691
- x.tCh<ExpectedKeys, ConstructKeys>()
692
-
693
- /* ensure that construct/struct schema types are identical */
694
-
695
- type ExpectedSchema = typeof schema
696
- type StructSchema = typeof struct.__schema
697
-
698
- x.tCh<StructSchema, ExpectedSchema>()
699
- x.tCh<ExpectedSchema, StructSchema>()
700
-
701
- type ConstructSchema = typeof struct.__schema
702
-
703
- x.tCh<ConstructSchema, ExpectedSchema>()
704
- x.tCh<ExpectedSchema, ConstructSchema>()
705
-
706
- /* runtime schema check */
707
-
708
- expect(struct.__schema).toStrictEqual(schema)
709
- expect(construct.__schema).toStrictEqual(schema)
710
- expect(construct.__schema === schema).toBe(false)
711
-
712
- /* runtime schema parameter application immutability check */
713
-
714
- expect(prevStruct.__schema === struct.__schema).toBe(false)
715
- }
716
- })
717
-
718
- it('optional + nullable', () => {
719
- const schema = {
720
- type: 'string',
721
- optional: true,
722
- nullable: true,
723
- } as const satisfies x.Schema
724
-
725
- const prevStruct = x.string().optional()
726
- const struct = prevStruct.nullable()
727
-
728
- type ExpectedKeys =
729
- | StructSharedKeys
730
- | 'brand'
731
- | 'description'
732
- | 'maxLength'
733
- | 'minLength'
734
-
735
- foldB: {
736
- const construct = x.makeStruct(schema)
737
-
738
- /* ensure that struct keys are reduced after application */
739
-
740
- type StructKeys = keyof typeof struct
741
-
742
- x.tCh<StructKeys, ExpectedKeys>()
743
- x.tCh<ExpectedKeys, StructKeys>()
744
-
745
- type ConstructKeys = keyof typeof construct
746
-
747
- x.tCh<ConstructKeys, ExpectedKeys>()
748
- x.tCh<ExpectedKeys, ConstructKeys>()
749
-
750
- /* ensure that construct/struct schema types are identical */
751
-
752
- type ExpectedSchema = typeof schema
753
- type StructSchema = typeof struct.__schema
754
-
755
- x.tCh<StructSchema, ExpectedSchema>()
756
- x.tCh<ExpectedSchema, StructSchema>()
757
-
758
- type ConstructSchema = typeof struct.__schema
759
-
760
- x.tCh<ConstructSchema, ExpectedSchema>()
761
- x.tCh<ExpectedSchema, ConstructSchema>()
762
-
763
- /* runtime schema check */
764
-
765
- expect(struct.__schema).toStrictEqual(schema)
766
- expect(construct.__schema).toStrictEqual(schema)
767
- expect(construct.__schema === schema).toBe(false)
768
-
769
- /* runtime schema parameter application immutability check */
770
-
771
- expect(prevStruct.__schema === struct.__schema).toBe(false)
772
- }
773
- })
774
-
775
- it('optional + nullable + brand', () => {
776
- const schema = {
777
- type: 'string',
778
- optional: true,
779
- nullable: true,
780
- brand: ['x', 'y'],
781
- } as const satisfies x.Schema
782
-
783
- const prevStruct = x.string().optional().nullable()
784
- const struct = prevStruct.brand('x', 'y')
785
-
786
- type ExpectedKeys =
787
- | StructSharedKeys
788
- | 'description'
789
- | 'minLength'
790
- | 'maxLength'
791
-
792
- foldB: {
793
- const construct = x.makeStruct(schema)
794
-
795
- /* ensure that struct keys are reduced after application */
796
-
797
- type StructKeys = keyof typeof struct
798
-
799
- x.tCh<StructKeys, ExpectedKeys>()
800
- x.tCh<ExpectedKeys, StructKeys>()
801
-
802
- type ConstructKeys = keyof typeof construct
803
-
804
- x.tCh<ConstructKeys, ExpectedKeys>()
805
- x.tCh<ExpectedKeys, ConstructKeys>()
806
-
807
- /* ensure that construct/struct schema types are identical */
808
-
809
- type ExpectedSchema = typeof schema
810
- type StructSchema = typeof struct.__schema
811
-
812
- x.tCh<StructSchema, ExpectedSchema>()
813
- x.tCh<ExpectedSchema, StructSchema>()
814
-
815
- type ConstructSchema = typeof struct.__schema
816
-
817
- x.tCh<ConstructSchema, ExpectedSchema>()
818
- x.tCh<ExpectedSchema, ConstructSchema>()
819
-
820
- /* runtime schema check */
821
-
822
- expect(struct.__schema).toStrictEqual(schema)
823
- expect(construct.__schema).toStrictEqual(schema)
824
- expect(construct.__schema === schema).toBe(false)
825
-
826
- /* runtime schema parameter application immutability check */
827
-
828
- expect(prevStruct.__schema === struct.__schema).toBe(false)
829
- }
830
- })
831
-
832
- it('optional + nullable + brand + minLength', () => {
833
- const schema = {
834
- type: 'string',
835
- optional: true,
836
- nullable: true,
837
- brand: ['x', 'y'],
838
- minLength: 0,
839
- } as const satisfies x.Schema
840
-
841
- const prevStruct = x
842
- .string()
843
- .optional()
844
- .nullable()
845
- .minLength(schema.minLength)
846
-
847
- const struct = prevStruct.brand('x', 'y')
848
-
849
- type ExpectedKeys = StructSharedKeys | 'description' | 'maxLength'
850
-
851
- foldB: {
852
- const construct = x.makeStruct(schema)
853
-
854
- /* ensure that struct keys are reduced after application */
855
-
856
- type StructKeys = keyof typeof struct
857
-
858
- x.tCh<StructKeys, ExpectedKeys>()
859
- x.tCh<ExpectedKeys, StructKeys>()
860
-
861
- type ConstructKeys = keyof typeof construct
862
-
863
- x.tCh<ConstructKeys, ExpectedKeys>()
864
- x.tCh<ExpectedKeys, ConstructKeys>()
865
-
866
- /* ensure that construct/struct schema types are identical */
867
-
868
- type ExpectedSchema = typeof schema
869
- type StructSchema = typeof struct.__schema
870
-
871
- x.tCh<StructSchema, ExpectedSchema>()
872
- x.tCh<ExpectedSchema, StructSchema>()
873
-
874
- type ConstructSchema = typeof struct.__schema
875
-
876
- x.tCh<ConstructSchema, ExpectedSchema>()
877
- x.tCh<ExpectedSchema, ConstructSchema>()
878
-
879
- /* runtime schema check */
880
-
881
- expect(struct.__schema).toStrictEqual(schema)
882
- expect(construct.__schema).toStrictEqual(schema)
883
- expect(construct.__schema === schema).toBe(false)
884
-
885
- /* runtime schema parameter application immutability check */
886
-
887
- expect(prevStruct.__schema === struct.__schema).toBe(false)
888
- }
889
- })
890
-
891
- it('optional + nullable + brand + minLength + maxLength', () => {
892
- const schema = {
893
- type: 'string',
894
- optional: true,
895
- nullable: true,
896
- brand: ['x', 'y'],
897
- minLength: 0,
898
- maxLength: 0,
899
- } as const satisfies x.Schema
900
-
901
- const prevStruct = x
902
- .string()
903
- .optional()
904
- .nullable()
905
- .minLength(schema.minLength)
906
- .maxLength(schema.maxLength)
907
-
908
- const struct = prevStruct.brand('x', 'y')
909
-
910
- type ExpectedKeys = StructSharedKeys | 'description'
911
-
912
- foldB: {
913
- const construct = x.makeStruct(schema)
914
-
915
- /* ensure that struct keys are reduced after application */
916
-
917
- type StructKeys = keyof typeof struct
918
-
919
- x.tCh<StructKeys, ExpectedKeys>()
920
- x.tCh<ExpectedKeys, StructKeys>()
921
-
922
- type ConstructKeys = keyof typeof construct
923
-
924
- x.tCh<ConstructKeys, ExpectedKeys>()
925
- x.tCh<ExpectedKeys, ConstructKeys>()
926
-
927
- /* ensure that construct/struct schema types are identical */
928
-
929
- type ExpectedSchema = typeof schema
930
- type StructSchema = typeof struct.__schema
931
-
932
- x.tCh<StructSchema, ExpectedSchema>()
933
- x.tCh<ExpectedSchema, StructSchema>()
934
-
935
- type ConstructSchema = typeof struct.__schema
936
-
937
- x.tCh<ConstructSchema, ExpectedSchema>()
938
- x.tCh<ExpectedSchema, ConstructSchema>()
939
-
940
- /* runtime schema check */
941
-
942
- expect(struct.__schema).toStrictEqual(schema)
943
- expect(construct.__schema).toStrictEqual(schema)
944
- expect(construct.__schema === schema).toBe(false)
945
-
946
- /* runtime schema parameter application immutability check */
947
-
948
- expect(prevStruct.__schema === struct.__schema).toBe(false)
949
- }
950
- })
951
-
952
- it('optional + nullable + brand + minLength + maxLength + description', () => {
953
- const schema = {
954
- type: 'string',
955
- optional: true,
956
- nullable: true,
957
- brand: ['x', 'y'],
958
- minLength: 0,
959
- maxLength: 0,
960
- description: 'x',
961
- } as const satisfies x.Schema
962
-
963
- const prevStruct = x
964
- .string()
965
- .optional()
966
- .nullable()
967
- .minLength(0)
968
- .maxLength(0)
969
- .description('x')
970
-
971
- const struct = prevStruct.brand('x', 'y')
972
-
973
- type ExpectedKeys = StructSharedKeys
974
-
975
- foldB: {
976
- const construct = x.makeStruct(schema)
977
-
978
- /* ensure that struct keys are reduced after application */
979
-
980
- type StructKeys = keyof typeof struct
981
-
982
- x.tCh<StructKeys, ExpectedKeys>()
983
- x.tCh<ExpectedKeys, StructKeys>()
984
-
985
- type ConstructKeys = keyof typeof construct
986
-
987
- x.tCh<ConstructKeys, ExpectedKeys>()
988
- x.tCh<ExpectedKeys, ConstructKeys>()
989
-
990
- /* ensure that construct/struct schema types are identical */
991
-
992
- type ExpectedSchema = typeof schema
993
- type StructSchema = typeof struct.__schema
994
-
995
- x.tCh<StructSchema, ExpectedSchema>()
996
- x.tCh<ExpectedSchema, StructSchema>()
997
-
998
- type ConstructSchema = typeof struct.__schema
999
-
1000
- x.tCh<ConstructSchema, ExpectedSchema>()
1001
- x.tCh<ExpectedSchema, ConstructSchema>()
1002
-
1003
- /* runtime schema check */
1004
-
1005
- expect(struct.__schema).toStrictEqual(schema)
1006
- expect(construct.__schema).toStrictEqual(schema)
1007
- expect(construct.__schema === schema).toBe(false)
1008
-
1009
- /* runtime schema parameter application immutability check */
1010
-
1011
- expect(prevStruct.__schema === struct.__schema).toBe(false)
1012
- }
1013
- })
1014
-
1015
- it('description + maxLength + minLength + brand + nullable + optional', () => {
1016
- const schema = {
1017
- type: 'string',
1018
- optional: true,
1019
- nullable: true,
1020
- brand: ['x', 'y'],
1021
- description: 'x',
1022
- minLength: 0,
1023
- maxLength: 0,
1024
- } as const satisfies x.Schema
1025
-
1026
- const prevStruct = x
1027
- .string()
1028
- .description('x')
1029
- .maxLength(schema.maxLength)
1030
- .minLength(schema.minLength)
1031
- .brand('x', 'y')
1032
- .nullable()
1033
-
1034
- const struct = prevStruct.optional()
1035
-
1036
- type ExpectedKeys = StructSharedKeys
1037
-
1038
- foldB: {
1039
- const construct = x.makeStruct(schema)
1040
-
1041
- /* ensure that struct keys are reduced after application */
1042
-
1043
- type StructKeys = keyof typeof struct
1044
-
1045
- x.tCh<StructKeys, ExpectedKeys>()
1046
- x.tCh<ExpectedKeys, StructKeys>()
1047
-
1048
- type ConstructKeys = keyof typeof construct
1049
-
1050
- x.tCh<ConstructKeys, ExpectedKeys>()
1051
- x.tCh<ExpectedKeys, ConstructKeys>()
1052
-
1053
- /* ensure that construct/struct schema types are identical */
1054
-
1055
- type ExpectedSchema = typeof schema
1056
- type StructSchema = typeof struct.__schema
1057
-
1058
- x.tCh<StructSchema, ExpectedSchema>()
1059
- x.tCh<ExpectedSchema, StructSchema>()
1060
-
1061
- type ConstructSchema = typeof struct.__schema
1062
-
1063
- x.tCh<ConstructSchema, ExpectedSchema>()
1064
- x.tCh<ExpectedSchema, ConstructSchema>()
1065
-
1066
- /* runtime schema check */
1067
-
1068
- expect(struct.__schema).toStrictEqual(schema)
1069
- expect(construct.__schema).toStrictEqual(schema)
1070
- expect(construct.__schema === schema).toBe(false)
1071
-
1072
- /* runtime schema parameter application immutability check */
1073
-
1074
- expect(prevStruct.__schema === struct.__schema).toBe(false)
1075
- }
1076
- })
1077
- })
1078
-
1079
- describe('ERROR_CODE.invalidType (foldC)', () => {
1080
- it('iterate over fixture.DATA_TYPE', () => {
1081
- const schema = { type: 'string' } satisfies x.Schema
1082
- const struct = x.string()
1083
- const source = fixture.DATA_TYPE
1084
-
1085
- foldC: {
1086
- const construct = x.makeStruct(schema)
1087
-
1088
- for (const [kind, types] of source) {
1089
- if (kind === schema.type) {
1090
- continue
1091
- }
1092
-
1093
- for (const subject of types) {
1094
- const expectedError = [
1095
- {
1096
- code: x.ERROR_CODE.invalidType,
1097
- schema: schema,
1098
- subject: subject,
1099
- path: [],
1100
- },
1101
- ]
1102
-
1103
- const parsedSchema = x.parse(schema, subject)
1104
- const parsedConstruct = construct.parse(subject)
1105
- const parsedStruct = struct.parse(subject)
1106
-
1107
- expect(parsedSchema.error).toStrictEqual(expectedError)
1108
- expect(parsedConstruct.error).toStrictEqual(expectedError)
1109
- expect(parsedStruct.error).toStrictEqual(expectedError)
1110
-
1111
- const parsedStandard = struct['~standard'].validate(subject)
1112
-
1113
- if (parsedStandard instanceof Promise) {
1114
- throw Error('Not expected')
1115
- }
1116
-
1117
- expect(parsedStandard.issues).toStrictEqual([
1118
- { message: x.ERROR_CODE.invalidType, path: [] },
1119
- ])
1120
- }
1121
- }
1122
- }
1123
- })
1124
- })
1125
-
1126
- describe('ERROR_CODE.invalidRange (foldD)', () => {
1127
- it('minLength', () => {
1128
- const schema = { type: 'string', minLength: 2 } satisfies x.Schema
1129
- const struct = x.string().minLength(schema.minLength)
1130
- const subjects = ['', 'x']
1131
-
1132
- foldD: {
1133
- const construct = x.makeStruct(schema)
1134
-
1135
- for (const subject of subjects) {
1136
- const expectedError = [
1137
- {
1138
- code: x.ERROR_CODE.invalidRange,
1139
- schema: schema,
1140
- subject: subject,
1141
- path: [],
1142
- },
1143
- ]
1144
-
1145
- const parsedSchema = x.parse(schema, subject)
1146
- const parsedConstruct = construct.parse(subject)
1147
- const parsedStruct = struct.parse(subject)
1148
-
1149
- expect(parsedSchema.error).toStrictEqual(expectedError)
1150
- expect(parsedConstruct.error).toStrictEqual(expectedError)
1151
- expect(parsedStruct.error).toStrictEqual(expectedError)
1152
-
1153
- const parsedStandard = struct['~standard'].validate(subject)
1154
-
1155
- if (parsedStandard instanceof Promise) {
1156
- throw Error('Not expected')
1157
- }
1158
-
1159
- expect(parsedStandard.issues).toStrictEqual([
1160
- { message: x.ERROR_CODE.invalidRange, path: [] },
1161
- ])
1162
- }
1163
- }
1164
- })
1165
-
1166
- it('maxLength', () => {
1167
- const schema = { type: 'string', maxLength: 2 } satisfies x.Schema
1168
- const struct = x.string().maxLength(schema.maxLength)
1169
- const subjects = ['xxx', 'xxxx', 'xxxxx']
1170
-
1171
- foldD: {
1172
- const construct = x.makeStruct(schema)
1173
-
1174
- for (const subject of subjects) {
1175
- const expectedError = [
1176
- {
1177
- code: x.ERROR_CODE.invalidRange,
1178
- schema: schema,
1179
- subject: subject,
1180
- path: [],
1181
- },
1182
- ]
1183
-
1184
- const parsedSchema = x.parse(schema, subject)
1185
- const parsedConstruct = construct.parse(subject)
1186
- const parsedStruct = struct.parse(subject)
1187
-
1188
- expect(parsedSchema.error).toStrictEqual(expectedError)
1189
- expect(parsedConstruct.error).toStrictEqual(expectedError)
1190
- expect(parsedStruct.error).toStrictEqual(expectedError)
1191
-
1192
- const parsedStandard = struct['~standard'].validate(subject)
1193
-
1194
- if (parsedStandard instanceof Promise) {
1195
- throw Error('Not expected')
1196
- }
1197
-
1198
- expect(parsedStandard.issues).toStrictEqual([
1199
- { message: x.ERROR_CODE.invalidRange, path: [] },
1200
- ])
1201
- }
1202
- }
1203
- })
1204
-
1205
- it('minLength + maxLength', () => {
1206
- const schema = {
1207
- type: 'string',
1208
- minLength: 1,
1209
- maxLength: 2,
1210
- } satisfies x.Schema
1211
-
1212
- const struct = x
1213
- .string()
1214
- .minLength(schema.minLength)
1215
- .maxLength(schema.maxLength)
1216
-
1217
- const subjects = ['', 'xxx']
1218
-
1219
- foldD: {
1220
- const construct = x.makeStruct(schema)
1221
-
1222
- for (const subject of subjects) {
1223
- const expectedError = [
1224
- {
1225
- code: x.ERROR_CODE.invalidRange,
1226
- schema: schema,
1227
- subject: subject,
1228
- path: [],
1229
- },
1230
- ]
1231
-
1232
- const parsedSchema = x.parse(schema, subject)
1233
- const parsedConstruct = construct.parse(subject)
1234
- const parsedStruct = struct.parse(subject)
1235
-
1236
- expect(parsedSchema.error).toStrictEqual(expectedError)
1237
- expect(parsedConstruct.error).toStrictEqual(expectedError)
1238
- expect(parsedStruct.error).toStrictEqual(expectedError)
1239
-
1240
- const parsedStandard = struct['~standard'].validate(subject)
1241
-
1242
- if (parsedStandard instanceof Promise) {
1243
- throw Error('Not expected')
1244
- }
1245
-
1246
- expect(parsedStandard.issues).toStrictEqual([
1247
- { message: x.ERROR_CODE.invalidRange, path: [] },
1248
- ])
1249
- }
1250
- }
1251
- })
1252
- })