Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
.withName('Properties<T>')
.withContent(['Required<{', ' [K in keyof T]: z.ZodType<T[K], any, T[K]>;', '}>'].join('\n'))
.string,
// Unfortunately, zod doesnt provide non-null defined any schema.
// Unfortunately, zod doesn't provide non-null defined any schema.
// This is a temporary hack until it is fixed.
// see: https://github.com/colinhacks/zod/issues/884
new DeclarationBlock({}).asKind('type').withName('definedNonNullAny').withContent('{}').string,
Expand All @@ -77,6 +77,12 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
const visitor = this.createVisitor('input');
const name = visitor.convertName(node.name.value);
this.importTypes.push(name);
// Check for @oneOf directive
const hasOneOf = node.directives?.some(d => d.name.value === 'oneOf');
if (hasOneOf) {
return this.buildOneOfInputFields(node.fields ?? [], visitor, name);
}

return this.buildInputFields(node.fields ?? [], visitor, name);
},
};
Expand Down Expand Up @@ -274,6 +280,52 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
.string;
}
}

protected buildOneOfInputFields(
fields: readonly InputValueDefinitionNode[],
visitor: Visitor,
name: string,
): string {
// Generate discriminated union variants
const variants = fields.map((selectedField) => {
const fieldName = selectedField.name.value;
// Get the raw schema without nullish wrapper for discriminated union
const fieldSchema = generateFieldTypeZodSchema(this.config, visitor, selectedField, selectedField.type, {
kind: Kind.NON_NULL_TYPE,
type: {
kind: Kind.NAMED_TYPE,
name: selectedField.name,
}
});
return indent(`z.object({\n`, 2) +
indent(` __type: z.literal("${fieldName}"),\n`, 2) +
indent(` ${fieldName}: ${fieldSchema}\n`, 2) +
indent(`})`, 2);
}).join(',\n');

switch (this.config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema`)
.withContent(`z.discriminatedUnion("__type", [\n ${variants}\n])`)
.string;

case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): z.ZodSchema<${name}>`)
.withBlock([
indent(`return z.discriminatedUnion("__type", [`),
variants,
indent(`]);`),
].join('\n'))
.string;
}
}
}

function generateFieldZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, indentCount: number): string {
Expand Down
50 changes: 50 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1797,4 +1797,54 @@ describe('zod', () => {
"
`)
});

it('with @oneOf directive', async () => {
const schema = buildSchema(/* GraphQL */ `
input UpdateUserInput @oneOf {
email: String
phoneNumber: String
profile: UpdateUserProfileInput
}

input UpdateUserProfileInput {
name: String
age: Int
}
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
},
{},
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export function UpdateUserInputSchema(): z.ZodSchema<UpdateUserInput> {
return z.discriminatedUnion("__type", [
z.object({
__type: z.literal("email"),
email: z.string()
}),
z.object({
__type: z.literal("phoneNumber"),
phoneNumber: z.string()
}),
z.object({
__type: z.literal("profile"),
profile: UpdateUserProfileInputSchema()
})
]);
}

export function UpdateUserProfileInputSchema(): z.ZodObject<Properties<UpdateUserProfileInput>> {
return z.object({
name: z.string().nullish(),
age: z.number().nullish()
})
}
"
`)
});
});