Skip to content

Commit 056596b

Browse files
committed
feat: supprt enumAsTypes
1 parent 02eb6f2 commit 056596b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/valibot/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@ export class ValibotSchemaVisitor extends BaseSchemaVisitor {
5555

5656
// hoist enum declarations
5757
this.enumDeclarations.push(
58-
new DeclarationBlock({})
59-
.export()
60-
.asKind('const')
61-
.withName(`${enumname}Schema`)
62-
.withContent(`v.enum_(${enumname})`).string,
58+
this.config.enumsAsTypes
59+
? new DeclarationBlock({})
60+
.export()
61+
.asKind('const')
62+
.withName(`${enumname}Schema`)
63+
.withContent(`v.picklist([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`)
64+
.string
65+
: new DeclarationBlock({})
66+
.export()
67+
.asKind('const')
68+
.withName(`${enumname}Schema`)
69+
.withContent(`v.enum_(${enumname})`).string,
6370
);
6471
},
6572
};

tests/valibot.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,31 @@ describe('valibot', () => {
182182
"
183183
`);
184184
});
185+
186+
it.todo('with importFrom');
187+
it.todo('with importFrom & useTypeImports')
188+
189+
it('with enumsAsTypes', async () => {
190+
const schema = buildSchema(/* GraphQL */ `
191+
enum PageType {
192+
PUBLIC
193+
BASIC_AUTH
194+
}
195+
`);
196+
const result = await plugin(
197+
schema,
198+
[],
199+
{
200+
schema: 'valibot',
201+
enumsAsTypes: true,
202+
},
203+
{},
204+
);
205+
expect(result.content).toMatchInlineSnapshot(`
206+
"
207+
export const PageTypeSchema = v.picklist([\'PUBLIC\', \'BASIC_AUTH\']);
208+
"
209+
`);
210+
});
211+
185212
})

0 commit comments

Comments
 (0)