Skip to content

Commit 82c2425

Browse files
committed
support scalarSchema
1 parent 248a35d commit 82c2425

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/valibot/index.ts

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

164164
function valibot4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string {
165+
if (config.scalarSchemas?.[scalarName])
166+
return config.scalarSchemas[scalarName];
167+
165168
const tsType = visitor.getScalarType(scalarName);
166169
switch (tsType) {
167170
case 'string':

tests/valibot.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,40 @@ describe('valibot', () => {
286286
"
287287
`);
288288
});
289+
290+
it('with scalarSchemas', async () => {
291+
const schema = buildSchema(/* GraphQL */ `
292+
input ScalarsInput {
293+
date: Date!
294+
email: Email
295+
str: String!
296+
}
297+
scalar Date
298+
scalar Email
299+
`);
300+
const result = await plugin(
301+
schema,
302+
[],
303+
{
304+
schema: 'valibot',
305+
scalarSchemas: {
306+
Date: 'v.date()',
307+
Email: 'v.string([v.email()])',
308+
},
309+
},
310+
{},
311+
);
312+
expect(result.content).toMatchInlineSnapshot(`
313+
"
314+
315+
export function ScalarsInputSchema() {
316+
return v.object({
317+
date: v.date(),
318+
email: v.nullish(v.string([v.email()])),
319+
str: v.string()
320+
})
321+
}
322+
"
323+
`)
324+
});
289325
})

0 commit comments

Comments
 (0)