@@ -167,6 +167,7 @@ interface Instruction {
167
167
* @default 'sanitize'
168
168
**/
169
169
sanitize : 'auto' | 'manual' | 'throw'
170
+ definitions : Record < string , TAnySchema >
170
171
}
171
172
172
173
const SANITIZE = {
@@ -225,6 +226,16 @@ const accelerate = (
225
226
) : string => {
226
227
if ( ! schema ) return ''
227
228
229
+ if (
230
+ Kind in schema &&
231
+ schema [ Kind ] === 'Import' &&
232
+ schema . $ref in schema . $defs
233
+ )
234
+ return accelerate ( schema . $defs [ schema . $ref ] , property , {
235
+ ...instruction ,
236
+ definitions : Object . assign ( instruction . definitions , schema . $defs )
237
+ } )
238
+
228
239
let v = ''
229
240
const isRoot = property === 'v'
230
241
@@ -376,14 +387,14 @@ const accelerate = (
376
387
instruction . array ++
377
388
378
389
if ( schema . items . type === 'string' ) {
379
- if ( isRoot ) v += 'return `'
390
+ if ( isRoot ) v += 'return `'
380
391
381
392
if ( nullableCondition )
382
393
v += `\${${ nullableCondition } ?"null":${ property } .length?\`[${ joinStringArray ( property ) } ]\`:"[]"}`
383
394
else
384
395
v += `\${${ property } .length?\`[${ joinStringArray ( property ) } ]\`:"[]"}`
385
396
386
- if ( isRoot ) v += '`'
397
+ if ( isRoot ) v += '`'
387
398
388
399
break
389
400
}
@@ -394,14 +405,14 @@ const accelerate = (
394
405
schema . items . type === 'bigint' ||
395
406
isInteger ( schema . items )
396
407
) {
397
- if ( isRoot ) v += 'return `'
408
+ if ( isRoot ) v += 'return `'
398
409
399
410
if ( nullableCondition )
400
411
v += `\${${ nullableCondition } ?'"null"':${ property } .length?\`[$\{${ property } .toString()}]\`:"[]"`
401
412
else
402
413
v += `\${${ property } .length?\`[$\{${ property } .toString()}]\`:"[]"}`
403
414
404
- if ( isRoot ) v += '`'
415
+ if ( isRoot ) v += '`'
405
416
406
417
break
407
418
}
@@ -427,6 +438,13 @@ const accelerate = (
427
438
break
428
439
429
440
default :
441
+ if ( schema . $ref && schema . $ref in instruction . definitions )
442
+ return accelerate (
443
+ instruction . definitions [ schema . $ref ] ,
444
+ property ,
445
+ instruction
446
+ )
447
+
430
448
if ( isDateType ( schema ) ) {
431
449
if ( isNullable || isUndefinable )
432
450
v = `\${${ nullableCondition } ?${ schema . default !== undefined ? `'"${ schema . default } "'` : "'null'" } :typeof ${ property } ==="object"?\`\"\${${ property } .toISOString()}\"\`:${ property } }`
@@ -490,17 +508,17 @@ const accelerate = (
490
508
export const createAccelerator = < T extends TAnySchema > (
491
509
schema : T ,
492
510
{
493
- sanitize = 'auto'
494
- } : {
495
- sanitize ?: Instruction [ 'sanitize' ]
496
- } = { }
511
+ sanitize = 'auto' ,
512
+ definitions = { }
513
+ } : Partial < Pick < Instruction , 'sanitize' | 'definitions' > > = { }
497
514
) : ( ( v : T [ 'static' ] ) => string ) => {
498
515
const f = accelerate ( schema , 'v' , {
499
516
array : 0 ,
500
517
optional : 0 ,
501
518
properties : [ ] ,
502
519
hasString : false ,
503
- sanitize
520
+ sanitize,
521
+ definitions
504
522
} )
505
523
506
524
return Function ( 'v' , f ) as any
0 commit comments