Skip to content

Commit 15da551

Browse files
committed
feat: implement ref input object
1 parent 57d022c commit 15da551

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/valibot/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { buildApiForValibot, formatDirectiveConfig } from '../directive';
1717
import { BaseSchemaVisitor } from '../schema_visitor';
1818
import type { Visitor } from '../visitor';
1919
import {
20+
isInput,
2021
isListType,
2122
isNamedType,
2223
isNonNullType,
@@ -190,6 +191,9 @@ function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, vis
190191
}
191192

192193
function maybeLazy(type: TypeNode, schema: string): string {
194+
if (isNamedType(type) && isInput(type.name.value))
195+
return `v.lazy(() => ${schema})`;
196+
193197
return schema;
194198
}
195199

tests/valibot.spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,43 @@ describe('valibot', () => {
9090
expect(result.content).toContain(wantContain);
9191
});
9292

93-
it.todo('ref input object')
93+
it('ref input object', async () => {
94+
const schema = buildSchema(/* GraphQL */ `
95+
input AInput {
96+
b: BInput!
97+
}
98+
input BInput {
99+
c: CInput!
100+
}
101+
input CInput {
102+
a: AInput!
103+
}
104+
`);
105+
const scalars = undefined
106+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
107+
expect(result.content).toMatchInlineSnapshot(`
108+
"
109+
110+
export function AInputSchema() {
111+
return v.object({
112+
b: v.lazy(() => BInputSchema())
113+
})
114+
}
115+
116+
export function BInputSchema() {
117+
return v.object({
118+
c: v.lazy(() => CInputSchema())
119+
})
120+
}
121+
122+
export function CInputSchema() {
123+
return v.object({
124+
a: v.lazy(() => AInputSchema())
125+
})
126+
}
127+
"
128+
`);
129+
})
94130
it.todo('nested input object')
95131

96132
it('enum', async () => {
@@ -274,7 +310,7 @@ describe('valibot', () => {
274310
275311
export function InputOneSchema() {
276312
return v.object({
277-
field: InputNestedSchema()
313+
field: v.lazy(() => InputNestedSchema())
278314
})
279315
}
280316

0 commit comments

Comments
 (0)