Skip to content

Commit 6cead63

Browse files
committed
feat: support nullish
1 parent fb5c8e0 commit 6cead63

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/valibot/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
7373

7474
if (isNonNullType(parentType))
7575
return gen;
76+
77+
return `v.nullish(${gen})`;
7678
}
7779
console.warn('unhandled type:', type);
7880
return '';

tests/valibot.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,34 @@ describe('valibot', () => {
3131
"
3232
`);
3333
})
34+
it('nullish', async () => {
35+
const schema = buildSchema(/* GraphQL */ `
36+
input PrimitiveInput {
37+
a: ID
38+
b: String
39+
c: Boolean
40+
d: Int
41+
e: Float
42+
z: String! # no defined check
43+
}
44+
`);
45+
const scalars = {
46+
ID: 'string',
47+
}
48+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
49+
expect(result.content).toMatchInlineSnapshot(`
50+
"
51+
export function PrimitiveInputSchema(): v.GenericSchema<PrimitiveInput> {
52+
return v.object({
53+
a: v.nullish(v.string()),
54+
b: v.nullish(v.string()),
55+
c: v.nullish(v.boolean()),
56+
d: v.nullish(v.number()),
57+
e: v.nullish(v.number()),
58+
z: v.string()
59+
})
60+
}
61+
"
62+
`);
63+
})
3464
})

0 commit comments

Comments
 (0)