@@ -55,11 +55,12 @@ declare global {
55
55
is < T > ( constructor : Constructor < T > ) : Schema < T >
56
56
array < X > ( inner : X ) : Schema < TypeS < X > [ ] , TypeT < X > [ ] >
57
57
dict < X , Y extends Schema < any , string > = Schema < string > > ( inner : X , sKey ?: Y ) : Schema < Dict < TypeS < X > , TypeS < Y > > , Dict < TypeT < X > , TypeT < Y > > >
58
- tuple < X extends readonly any [ ] > ( list : X ) : Schema < TupleS < X > , TupleT < X > >
58
+ tuple < const X extends readonly any [ ] > ( list : X ) : Schema < TupleS < X > , TupleT < X > >
59
59
object < X extends Dict > ( dict : X ) : Schema < ObjectS < X > , ObjectT < X > >
60
60
union < const X > ( list : readonly X [ ] ) : Schema < TypeS < X > , TypeT < X > >
61
61
intersect < const X > ( list : readonly X [ ] ) : Schema < IntersectS < X > , IntersectT < X > >
62
62
transform < X , T > ( inner : X , callback : ( value : TypeS < X > , options : Schemastery . Options ) => T , preserve ?: boolean ) : Schema < TypeS < X > , T >
63
+ lazy < X extends Schema > ( callback : ( ) => X ) : X
63
64
ValidationError : typeof ValidationError
64
65
}
65
66
@@ -102,6 +103,7 @@ declare global {
102
103
dict ?: Dict < Schema >
103
104
bits ?: Dict < number >
104
105
callback ?: Function
106
+ builder ?: Function
105
107
value ?: T
106
108
refs ?: Dict < Schema >
107
109
preserve ?: boolean
@@ -384,7 +386,7 @@ Schema.resolve = function resolve(data, schema, options = {}, strict = false) {
384
386
if ( ! schema ) return [ data ]
385
387
if ( options . ignore ?.( data , schema ) ) return [ data ]
386
388
387
- if ( isNullable ( data ) ) {
389
+ if ( isNullable ( data ) && schema . type !== 'lazy' ) {
388
390
if ( schema . meta . required ) throw new ValidationError ( `missing required value` , options )
389
391
let current = schema
390
392
let fallback = schema . meta . default
@@ -427,6 +429,18 @@ Schema.from = function from(source: any) {
427
429
}
428
430
}
429
431
432
+ Schema . lazy = function lazy ( builder ) {
433
+ const toJSON = ( ) => {
434
+ if ( ! schema . inner ! [ kSchema ] ) {
435
+ schema . inner = schema . builder ! ( )
436
+ schema . inner ! . meta = { ...schema . meta , ...schema . inner ! . meta }
437
+ }
438
+ return schema . inner ! . toJSON ( )
439
+ }
440
+ const schema = new Schema ( { type : 'lazy' , builder, inner : { toJSON } as any } )
441
+ return schema as any
442
+ }
443
+
430
444
Schema . natural = function natural ( ) {
431
445
return Schema . number ( ) . step ( 1 ) . min ( 0 )
432
446
}
@@ -479,6 +493,14 @@ Schema.arrayBuffer = function arrayBuffer(encoding?: 'hex' | 'base64') {
479
493
] )
480
494
}
481
495
496
+ Schema . extend ( 'lazy' , ( data , schema , options , strict ) => {
497
+ if ( ! schema . inner ! [ kSchema ] ) {
498
+ schema . inner = schema . builder ! ( )
499
+ schema . inner ! . meta = { ...schema . meta , ...schema . inner ! . meta }
500
+ }
501
+ return Schema . resolve ( data , schema . inner ! , options , strict )
502
+ } )
503
+
482
504
Schema . extend ( 'any' , ( data ) => {
483
505
return [ data ]
484
506
} )
0 commit comments