File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -56,11 +56,18 @@ export class ValibotSchemaVisitor extends BaseSchemaVisitor {
5656
5757 // hoist enum declarations
5858 this . enumDeclarations . push (
59- new DeclarationBlock ( { } )
60- . export ( )
61- . asKind ( 'const' )
62- . withName ( `${ enumname } Schema` )
63- . withContent ( `v.enum_(${ enumname } )` ) . string ,
59+ this . config . enumsAsTypes
60+ ? new DeclarationBlock ( { } )
61+ . export ( )
62+ . asKind ( 'const' )
63+ . withName ( `${ enumname } Schema` )
64+ . withContent ( `v.picklist([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])` )
65+ . string
66+ : new DeclarationBlock ( { } )
67+ . export ( )
68+ . asKind ( 'const' )
69+ . withName ( `${ enumname } Schema` )
70+ . withContent ( `v.enum_(${ enumname } )` ) . string ,
6471 ) ;
6572 } ,
6673 } ;
Original file line number Diff line number Diff line change @@ -279,4 +279,26 @@ describe('valibot', () => {
279279 "
280280 ` ) ;
281281 } ) ;
282+ it ( 'with enumsAsTypes' , async ( ) => {
283+ const schema = buildSchema ( /* GraphQL */ `
284+ enum PageType {
285+ PUBLIC
286+ BASIC_AUTH
287+ }
288+ ` ) ;
289+ const result = await plugin (
290+ schema ,
291+ [ ] ,
292+ {
293+ schema : 'valibot' ,
294+ enumsAsTypes : true ,
295+ } ,
296+ { } ,
297+ ) ;
298+ expect ( result . content ) . toMatchInlineSnapshot ( `
299+ "
300+ export const PageTypeSchema = v.picklist([\'PUBLIC\', \'BASIC_AUTH\']);
301+ "
302+ ` ) ;
303+ } ) ;
282304} )
You can’t perform that action at this time.
0 commit comments