Skip to content

Commit 8aeee2c

Browse files
paalesbramvanderholst
authored andcommitted
Added zod tests and initial implementation for default input values
1 parent 2c66366 commit 8aeee2c

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/zod/index.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GraphQLSchema,
66
InputObjectTypeDefinitionNode,
77
InputValueDefinitionNode,
8+
Kind,
89
NameNode,
910
ObjectTypeDefinitionNode,
1011
TypeNode,
@@ -129,16 +130,16 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
129130
this.enumDeclarations.push(
130131
this.config.enumsAsTypes
131132
? new DeclarationBlock({})
132-
.export()
133-
.asKind('const')
134-
.withName(`${enumname}Schema`)
135-
.withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`)
136-
.string
133+
.export()
134+
.asKind('const')
135+
.withName(`${enumname}Schema`)
136+
.withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`)
137+
.string
137138
: new DeclarationBlock({})
138-
.export()
139-
.asKind('const')
140-
.withName(`${enumname}Schema`)
141-
.withContent(`z.nativeEnum(${enumname})`).string
139+
.export()
140+
.asKind('const')
141+
.withName(`${enumname}Schema`)
142+
.withContent(`z.nativeEnum(${enumname})`).string
142143
);
143144
},
144145
};
@@ -247,7 +248,19 @@ const generateFieldTypeZodSchema = (
247248
if (isListType(parentType)) {
248249
return `${gen}.nullable()`;
249250
}
250-
const appliedDirectivesGen = applyDirectives(config, field, gen);
251+
let appliedDirectivesGen = applyDirectives(config, field, gen);
252+
253+
if (field.kind === Kind.INPUT_VALUE_DEFINITION) {
254+
const { defaultValue } = field;
255+
256+
if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN) {
257+
appliedDirectivesGen = `${appliedDirectivesGen}.default(${defaultValue.value})`;
258+
}
259+
if ((defaultValue?.kind === Kind.STRING) || (defaultValue?.kind === Kind.ENUM)) {
260+
appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`;
261+
}
262+
}
263+
251264
if (isNonNullType(parentType)) {
252265
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
253266
return `${appliedDirectivesGen}.min(1)`;

tests/zod.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,40 @@ describe('zod', () => {
403403
expect(result.content).toContain('export function SayISchema(): z.ZodObject<Properties<SayI>> {');
404404
});
405405

406+
it('with default input values', async () => {
407+
const schema = buildSchema(/* GraphQL */ `
408+
enum PageType {
409+
PUBLIC
410+
BASIC_AUTH
411+
}
412+
input PageInput {
413+
pageType: PageType! = PUBLIC
414+
greeting: String = "Hello"
415+
score: Int = 100
416+
ratio: Float = 0.5
417+
isMember: Boolean = true
418+
}
419+
`);
420+
const result = await plugin(
421+
schema,
422+
[],
423+
{
424+
schema: 'zod',
425+
importFrom: './types',
426+
},
427+
{}
428+
);
429+
430+
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
431+
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
432+
433+
expect(result.content).toContain('pageType: PageTypeSchema.default("PUBLIC")');
434+
expect(result.content).toContain('greeting: z.string().default("Hello").nullish()');
435+
expect(result.content).toContain('score: z.number().default(100).nullish()');
436+
expect(result.content).toContain('ratio: z.number().default(0.5).nullish()');
437+
expect(result.content).toContain('isMember: z.boolean().default(true).nullish()');
438+
});
439+
406440
describe('issues #19', () => {
407441
it('string field', async () => {
408442
const schema = buildSchema(/* GraphQL */ `

0 commit comments

Comments
 (0)