Skip to content

Commit c8e7c07

Browse files
committed
feat: support for enumsAsTypes
1 parent 5a567db commit c8e7c07

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/valibot/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff 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
};

tests/valibot.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)