@@ -15,13 +15,15 @@ import { identifiers } from './constants';
1515import { arrayToPipes } from './toAst/array' ;
1616import { booleanToPipes } from './toAst/boolean' ;
1717import { enumToPipes } from './toAst/enum' ;
18+ import { intersectionToPipes } from './toAst/intersection' ;
1819import { neverToPipes } from './toAst/never' ;
1920import { nullToPipes } from './toAst/null' ;
2021import { numberToPipes } from './toAst/number' ;
2122import { objectToPipes } from './toAst/object' ;
2223import { stringToPipes } from './toAst/string' ;
2324import { tupleToPipes } from './toAst/tuple' ;
2425import { undefinedToPipes } from './toAst/undefined' ;
26+ import { unionToPipes } from './toAst/union' ;
2527import { unknownToPipes } from './toAst/unknown' ;
2628import { voidToPipes } from './toAst/void' ;
2729
@@ -143,16 +145,19 @@ export function createVisitor(
143145 }
144146 } ,
145147 intersection ( items , schemas , parentSchema , ctx ) {
146- const v = ctx . plugin . external ( 'valibot.v' ) ;
147- const itemNodes = items . map ( ( item ) => pipesToNode ( item . pipes , ctx . plugin ) ) ;
148+ const applyModifiers = ( result : ValibotResult , opts ?: { optional ?: boolean } ) =>
149+ this . applyModifiers ( result , ctx , opts ) as ValibotFinal ;
150+
151+ const { pipes } = intersectionToPipes ( {
152+ applyModifiers,
153+ childResults : items ,
154+ parentSchema,
155+ plugin : ctx . plugin ,
156+ } ) ;
148157
149158 return {
150159 meta : composeMeta ( items , { default : parentSchema . default } ) ,
151- pipes : [
152- $ ( v )
153- . attr ( identifiers . schemas . intersect )
154- . call ( $ . array ( ...itemNodes ) ) ,
155- ] ,
160+ pipes,
156161 } ;
157162 } ,
158163 never ( schema , ctx ) {
@@ -290,33 +295,19 @@ export function createVisitor(
290295 } ;
291296 } ,
292297 union ( items , schemas , parentSchema , ctx ) {
293- const v = ctx . plugin . external ( 'valibot.v' ) ;
298+ const applyModifiers = ( result : ValibotResult , opts ?: { optional ?: boolean } ) =>
299+ this . applyModifiers ( result , ctx , opts ) as ValibotFinal ;
294300
295301 const hasNull = schemas . some ( ( s ) => s . type === 'null' ) || items . some ( ( i ) => i . meta . nullable ) ;
296302
297- const nonNullItems : Array < ValibotResult > = [ ] ;
298- items . forEach ( ( item , index ) => {
299- const schema = schemas [ index ] ! ;
300- if ( schema . type !== 'null' ) {
301- nonNullItems . push ( item ) ;
302- }
303+ const { pipes } = unionToPipes ( {
304+ applyModifiers ,
305+ childResults : items ,
306+ parentSchema ,
307+ plugin : ctx . plugin ,
308+ schemas ,
303309 } ) ;
304310
305- let pipes : Pipes ;
306-
307- if ( nonNullItems . length === 0 ) {
308- pipes = [ $ ( v ) . attr ( identifiers . schemas . null ) . call ( ) ] ;
309- } else if ( nonNullItems . length === 1 ) {
310- pipes = nonNullItems [ 0 ] ! . pipes ;
311- } else {
312- const itemNodes = nonNullItems . map ( ( i ) => pipesToNode ( i . pipes , ctx . plugin ) ) ;
313- pipes = [
314- $ ( v )
315- . attr ( identifiers . schemas . union )
316- . call ( $ . array ( ...itemNodes ) ) ,
317- ] ;
318- }
319-
320311 return {
321312 meta : composeMeta ( items , {
322313 default : parentSchema . default ,
0 commit comments