Skip to content

Commit 657ee60

Browse files
committed
feat: support ref input
1 parent df63604 commit 657ee60

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/valibot/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { ValidationSchemaPluginConfig } from '../config';
1212
import { BaseSchemaVisitor } from '../schema_visitor';
1313
import type { Visitor } from '../visitor';
1414
import {
15+
isInput,
1516
isListType,
1617
isNamedType,
1718
isNonNullType,
@@ -95,6 +96,12 @@ function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, vis
9596
const converter = visitor.getNameNodeConverter(node);
9697

9798
switch (converter?.targetKind) {
99+
case 'InputObjectTypeDefinition':
100+
// using switch-case rather than if-else to allow for future expansion
101+
switch (config.validationSchemaExportType) {
102+
default:
103+
return `${converter.convertName()}Schema()`;
104+
}
98105
default:
99106
if (converter?.targetKind)
100107
console.warn('Unknown targetKind', converter?.targetKind);
@@ -104,6 +111,9 @@ function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, vis
104111
}
105112

106113
function maybeLazy(type: TypeNode, schema: string): string {
114+
if (isNamedType(type) && isInput(type.name.value))
115+
return `v.lazy(() => ${schema})`;
116+
107117
return schema;
108118
}
109119

tests/valibot.spec.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,40 @@ describe('valibot', () => {
8989
"
9090
`);
9191
})
92-
})
92+
it('ref input object', async () => {
93+
const schema = buildSchema(/* GraphQL */ `
94+
input AInput {
95+
b: BInput!
96+
}
97+
input BInput {
98+
c: CInput!
99+
}
100+
input CInput {
101+
a: AInput!
102+
}
103+
`);
104+
const scalars = undefined
105+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
106+
expect(result.content).toMatchInlineSnapshot(`
107+
"
108+
export function AInputSchema(): v.GenericSchema<AInput> {
109+
return v.object({
110+
b: v.lazy(() => BInputSchema())
111+
})
112+
}
113+
114+
export function BInputSchema(): v.GenericSchema<BInput> {
115+
return v.object({
116+
c: v.lazy(() => CInputSchema())
117+
})
118+
}
119+
120+
export function CInputSchema(): v.GenericSchema<CInput> {
121+
return v.object({
122+
a: v.lazy(() => AInputSchema())
123+
})
124+
}
125+
"
126+
`);
127+
})
128+
})

0 commit comments

Comments
 (0)