Skip to content

Commit 0e81efc

Browse files
committed
feat: support enum
1 parent 657ee60 commit 0e81efc

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/valibot/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
22
import type {
3+
EnumTypeDefinitionNode,
34
FieldDefinitionNode,
45
GraphQLSchema,
56
InputObjectTypeDefinitionNode,
@@ -28,7 +29,11 @@ export class ValibotSchemaVisitor extends BaseSchemaVisitor {
2829
}
2930

3031
initialEmit(): string {
31-
return '';
32+
return (
33+
`\n${[
34+
...this.enumDeclarations,
35+
].join('\n')}`
36+
);
3237
}
3338

3439
get InputObjectTypeDefinition() {
@@ -42,6 +47,25 @@ export class ValibotSchemaVisitor extends BaseSchemaVisitor {
4247
};
4348
}
4449

50+
get EnumTypeDefinition() {
51+
return {
52+
leave: (node: EnumTypeDefinitionNode) => {
53+
const visitor = this.createVisitor('both');
54+
const enumname = visitor.convertName(node.name.value);
55+
this.importTypes.push(enumname);
56+
57+
// hoist enum declarations
58+
this.enumDeclarations.push(
59+
new DeclarationBlock({})
60+
.export()
61+
.asKind('const')
62+
.withName(`${enumname}Schema`)
63+
.withContent(`v.enum_(${enumname})`).string,
64+
);
65+
},
66+
};
67+
}
68+
4569
protected buildInputFields(
4670
fields: readonly (FieldDefinitionNode | InputValueDefinitionNode)[],
4771
visitor: Visitor,
@@ -102,6 +126,8 @@ function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, vis
102126
default:
103127
return `${converter.convertName()}Schema()`;
104128
}
129+
case 'EnumTypeDefinition':
130+
return `${converter.convertName()}Schema`;
105131
default:
106132
if (converter?.targetKind)
107133
console.warn('Unknown targetKind', converter?.targetKind);

tests/valibot.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('valibot', () => {
1919
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
2020
expect(result.content).toMatchInlineSnapshot(`
2121
"
22+
2223
export function PrimitiveInputSchema(): v.GenericSchema<PrimitiveInput> {
2324
return v.object({
2425
a: v.string(),
@@ -48,6 +49,7 @@ describe('valibot', () => {
4849
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
4950
expect(result.content).toMatchInlineSnapshot(`
5051
"
52+
5153
export function PrimitiveInputSchema(): v.GenericSchema<PrimitiveInput> {
5254
return v.object({
5355
a: v.nullish(v.string()),
@@ -76,6 +78,7 @@ describe('valibot', () => {
7678
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
7779
expect(result.content).toMatchInlineSnapshot(`
7880
"
81+
7982
export function PrimitiveInputSchema(): v.GenericSchema<PrimitiveInput> {
8083
return v.object({
8184
a: v.nullish(v.array(v.nullable(v.string()))),
@@ -105,6 +108,7 @@ describe('valibot', () => {
105108
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
106109
expect(result.content).toMatchInlineSnapshot(`
107110
"
111+
108112
export function AInputSchema(): v.GenericSchema<AInput> {
109113
return v.object({
110114
b: v.lazy(() => BInputSchema())
@@ -125,4 +129,29 @@ describe('valibot', () => {
125129
"
126130
`);
127131
})
128-
})
132+
it.todo('nested input object')
133+
it('enum', async () => {
134+
const schema = buildSchema(/* GraphQL */ `
135+
enum PageType {
136+
PUBLIC
137+
BASIC_AUTH
138+
}
139+
input PageInput {
140+
pageType: PageType!
141+
}
142+
`);
143+
const scalars = undefined
144+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
145+
expect(result.content).toMatchInlineSnapshot(`
146+
"
147+
export const PageTypeSchema = v.enum_(PageType);
148+
149+
export function PageInputSchema(): v.GenericSchema<PageInput> {
150+
return v.object({
151+
pageType: PageTypeSchema
152+
})
153+
}
154+
"
155+
`);
156+
})
157+
})

0 commit comments

Comments
 (0)