Skip to content

Commit 2fb1e1b

Browse files
committed
feat: support default input values
1 parent 82c2425 commit 2fb1e1b

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/valibot/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
2-
import type {
3-
EnumTypeDefinitionNode,
4-
FieldDefinitionNode,
5-
GraphQLSchema,
6-
InputObjectTypeDefinitionNode,
7-
InputValueDefinitionNode,
8-
NameNode,
9-
TypeNode,
2+
import {
3+
Kind,
4+
type EnumTypeDefinitionNode,
5+
type FieldDefinitionNode,
6+
type GraphQLSchema,
7+
type InputObjectTypeDefinitionNode,
8+
type InputValueDefinitionNode,
9+
type NameNode,
10+
type TypeNode,
1011
} from 'graphql';
1112

1213
import type { ValidationSchemaPluginConfig } from '../config';
@@ -113,10 +114,19 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
113114
return maybeLazy(type.type, gen);
114115
}
115116
if (isNamedType(type)) {
116-
const gen = generateNameNodeValibotSchema(config, visitor, type.name);
117+
let gen = generateNameNodeValibotSchema(config, visitor, type.name);
117118
if (isListType(parentType))
118119
return `v.nullable(${gen})`;
119120

121+
if (field.kind === Kind.INPUT_VALUE_DEFINITION) {
122+
const { defaultValue } = field;
123+
if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN)
124+
gen = `v.optional(${gen}, ${defaultValue.value})`;
125+
126+
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
127+
gen = `v.optional(${gen}, "${defaultValue.value}")`;
128+
129+
}
120130
if (isNonNullType(parentType)) {
121131
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
122132
return "v.string([v.minLength(1)])"; // TODO

tests/valibot.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,48 @@ describe('valibot', () => {
322322
"
323323
`)
324324
});
325-
})
325+
326+
it.todo('with typesPrefix');
327+
it.todo('with typesSuffix');
328+
329+
it('with default input values', async () => {
330+
const schema = buildSchema(/* GraphQL */ `
331+
enum PageType {
332+
PUBLIC
333+
BASIC_AUTH
334+
}
335+
input PageInput {
336+
pageType: PageType! = PUBLIC
337+
greeting: String = "Hello"
338+
score: Int = 100
339+
ratio: Float = 0.5
340+
isMember: Boolean = true
341+
}
342+
`);
343+
const result = await plugin(
344+
schema,
345+
[],
346+
{
347+
schema: 'valibot',
348+
importFrom: './types',
349+
},
350+
{},
351+
);
352+
353+
expect(result.content).toMatchInlineSnapshot(`
354+
"
355+
export const PageTypeSchema = v.enum_(PageType);
356+
357+
export function PageInputSchema() {
358+
return v.object({
359+
pageType: v.optional(PageTypeSchema, "PUBLIC"),
360+
greeting: v.nullish(v.optional(v.string(), "Hello")),
361+
score: v.nullish(v.optional(v.number(), 100)),
362+
ratio: v.nullish(v.optional(v.number(), 0.5)),
363+
isMember: v.nullish(v.optional(v.boolean(), true))
364+
})
365+
}
366+
"
367+
`)
368+
});
369+
})

0 commit comments

Comments
 (0)