Skip to content

Commit ac18b28

Browse files
committed
feat: support scalarSchemas
1 parent c8e7c07 commit ac18b28

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/valibot/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ function maybeLazy(type: TypeNode, schema: string): string {
151151
}
152152

153153
function valibot4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string {
154+
if (config.scalarSchemas?.[scalarName])
155+
return config.scalarSchemas[scalarName];
156+
154157
const tsType = visitor.getScalarType(scalarName);
155158
switch (tsType) {
156159
case 'string':

tests/valibot.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,41 @@ describe('valibot', () => {
301301
"
302302
`);
303303
});
304+
it.todo('with notAllowEmptyString')
305+
it.todo('with notAllowEmptyString issue #386')
306+
it('with scalarSchemas', async () => {
307+
const schema = buildSchema(/* GraphQL */ `
308+
input ScalarsInput {
309+
date: Date!
310+
email: Email
311+
str: String!
312+
}
313+
scalar Date
314+
scalar Email
315+
`);
316+
const result = await plugin(
317+
schema,
318+
[],
319+
{
320+
schema: 'valibot',
321+
scalarSchemas: {
322+
Date: 'v.date()',
323+
Email: 'v.string([v.email()])',
324+
},
325+
},
326+
{},
327+
);
328+
expect(result.content).toMatchInlineSnapshot(`
329+
"
330+
331+
export function ScalarsInputSchema(): v.GenericSchema<ScalarsInput> {
332+
return v.object({
333+
date: v.date(),
334+
email: v.nullish(v.string([v.email()])),
335+
str: v.string()
336+
})
337+
}
338+
"
339+
`)
340+
});
304341
})

0 commit comments

Comments
 (0)