Skip to content
Merged
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
9 changes: 8 additions & 1 deletion example/myzod/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as myzod from 'myzod'
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'
import { Admin, AttributeInput, ButtonComponentType, Comment, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'

export const definedNonNullAnySchema = myzod.object({});

Expand All @@ -25,6 +25,13 @@ export function AttributeInputSchema(): myzod.Type<AttributeInput> {
})
}

export function CommentSchema(): myzod.Type<Comment> {
return myzod.object({
__typename: myzod.literal('Comment').optional(),
replies: myzod.array(myzod.lazy(() => CommentSchema())).optional().nullable()
})
}

export function ComponentInputSchema(): myzod.Type<ComponentInput> {
return myzod.object({
child: myzod.lazy(() => ComponentInputSchema().optional().nullable()),
Expand Down
4 changes: 4 additions & 0 deletions example/test.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ directive @constraint(
multipleOf: Float
uniqueTypeName: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

type Comment {
replies: [Comment!]
}
5 changes: 5 additions & 0 deletions example/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export enum ButtonComponentType {
Submit = 'SUBMIT'
}

export type Comment = {
__typename?: 'Comment';
replies?: Maybe<Array<Comment>>;
};

export type ComponentInput = {
child?: InputMaybe<ComponentInput>;
childrens?: InputMaybe<Array<InputMaybe<ComponentInput>>>;
Expand Down
9 changes: 8 additions & 1 deletion example/valibot/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as v from 'valibot'
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'
import { Admin, AttributeInput, ButtonComponentType, Comment, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'

export const ButtonComponentTypeSchema = v.enum_(ButtonComponentType);

Expand All @@ -23,6 +23,13 @@ export function AttributeInputSchema(): v.GenericSchema<AttributeInput> {
})
}

export function CommentSchema(): v.GenericSchema<Comment> {
return v.object({
__typename: v.optional(v.literal('Comment')),
replies: v.nullish(v.array(v.lazy(() => CommentSchema())))
})
}

export function ComponentInputSchema(): v.GenericSchema<ComponentInput> {
return v.object({
child: v.lazy(() => v.nullish(ComponentInputSchema())),
Expand Down
9 changes: 8 additions & 1 deletion example/yup/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as yup from 'yup'
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User, UserKind } from '../types'
import { Admin, AttributeInput, ButtonComponentType, Comment, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User, UserKind } from '../types'

export const ButtonComponentTypeSchema = yup.string<ButtonComponentType>().oneOf(Object.values(ButtonComponentType)).defined();

Expand Down Expand Up @@ -29,6 +29,13 @@ export function AttributeInputSchema(): yup.ObjectSchema<AttributeInput> {
})
}

export function CommentSchema(): yup.ObjectSchema<Comment> {
return yup.object({
__typename: yup.string<'Comment'>().optional(),
replies: yup.array(yup.lazy(() => CommentSchema().nonNullable())).defined().nullable().optional()
})
}

export function ComponentInputSchema(): yup.ObjectSchema<ComponentInput> {
return yup.object({
child: yup.lazy(() => ComponentInputSchema()).optional(),
Expand Down
9 changes: 8 additions & 1 deletion example/zod/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod/v3'
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'
import { Admin, AttributeInput, ButtonComponentType, Comment, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'

type Properties<T> = Required<{
[K in keyof T]: z.ZodType<T[K], any, T[K]>;
Expand Down Expand Up @@ -33,6 +33,13 @@ export function AttributeInputSchema(): z.ZodObject<Properties<AttributeInput>>
})
}

export function CommentSchema(): z.ZodObject<Properties<Comment>> {
return z.object({
__typename: z.literal('Comment').optional(),
replies: z.array(z.lazy(() => CommentSchema())).nullish()
})
}

export function ComponentInputSchema(): z.ZodObject<Properties<ComponentInput>> {
return z.object({
child: z.lazy(() => ComponentInputSchema().nullish()),
Expand Down
11 changes: 9 additions & 2 deletions example/zodv4/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'
import { Admin, AttributeInput, ButtonComponentType, Comment, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types'

type Properties<T> = Required<{
[K in keyof T]: z.ZodType<T[K]>;
Expand Down Expand Up @@ -33,6 +33,13 @@ export function AttributeInputSchema(): z.ZodObject<Properties<AttributeInput>>
})
}

export function CommentSchema(): z.ZodObject<Properties<Comment>> {
return z.object({
__typename: z.literal('Comment').optional(),
replies: z.array(z.lazy(() => CommentSchema())).nullish()
})
}

export function ComponentInputSchema(): z.ZodObject<Properties<ComponentInput>> {
return z.object({
child: z.lazy(() => ComponentInputSchema().nullish()),
Expand Down Expand Up @@ -128,7 +135,7 @@ export function UserSchema(): z.ZodObject<Properties<User>> {
createdAt: definedNonNullAnySchema.nullish(),
email: z.string().nullish(),
id: z.string().nullish(),
kind: UserKindSchema().nullish(),
kind: z.lazy(() => UserKindSchema().nullish()),
name: z.string().nullish(),
password: z.string().nullish(),
updatedAt: definedNonNullAnySchema.nullish()
Expand Down
22 changes: 13 additions & 9 deletions src/zodv4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import type { Visitor } from '../visitor.js';
import { resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
import { convertNameParts, DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
import {
isEnumType,
isScalarType,
Kind,
} from 'graphql';
import { buildApi, formatDirectiveConfig } from '../directive.js';
import {
escapeGraphQLCharacters,
InterfaceTypeDefinitionBuilder,
isInput,
isListType,
isNamedType,
isNonNullType,
Expand Down Expand Up @@ -275,22 +276,22 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {

function generateFieldZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, indentCount: number): string {
const gen = generateFieldTypeZodSchema(config, visitor, field, field.type);
return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount);
return indent(`${field.name.value}: ${maybeLazy(visitor, field.type, gen)}`, indentCount);
}

function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, parentType?: TypeNode): string {
if (isListType(type)) {
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type);
if (!isNonNullType(parentType)) {
const arrayGen = `z.array(${maybeLazy(type.type, gen)})`;
const arrayGen = `z.array(${maybeLazy(visitor, type.type, gen)})`;
const maybeLazyGen = applyDirectives(config, field, arrayGen);
return `${maybeLazyGen}.nullish()`;
}
return `z.array(${maybeLazy(type.type, gen)})`;
return `z.array(${maybeLazy(visitor, type.type, gen)})`;
}
if (isNonNullType(type)) {
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type);
return maybeLazy(type.type, gen);
return maybeLazy(visitor, type.type, gen);
}
if (isNamedType(type)) {
const gen = generateNameNodeZodSchema(config, visitor, type.name);
Expand Down Expand Up @@ -371,11 +372,14 @@ function generateNameNodeZodSchema(config: ValidationSchemaPluginConfig, visitor
}
}

function maybeLazy(type: TypeNode, schema: string): string {
if (isNamedType(type) && isInput(type.name.value))
return `z.lazy(() => ${schema})`;
function maybeLazy(visitor: Visitor, type: TypeNode, schema: string): string {
if (!isNamedType(type)) {
return schema;
}

return schema;
const schemaType = visitor.getType(type.name.value);
const isComplexType = !isScalarType(schemaType) && !isEnumType(schemaType);
return isComplexType ? `z.lazy(() => ${schema})` : schema;
}

function zod4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string {
Expand Down
20 changes: 10 additions & 10 deletions tests/zodv4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,15 +1336,15 @@ describe('zodv4', () => {
export function BookSchema(): z.ZodObject<Properties<Book>> {
return z.object({
__typename: z.literal('Book').optional(),
author: AuthorSchema().nullish(),
author: z.lazy(() => AuthorSchema().nullish()),
title: z.string().nullish()
})
}

export function AuthorSchema(): z.ZodObject<Properties<Author>> {
return z.object({
__typename: z.literal('Author').optional(),
books: z.array(BookSchema().nullable()).nullish(),
books: z.array(z.lazy(() => BookSchema().nullable())).nullish(),
name: z.string().nullish()
})
}
Expand Down Expand Up @@ -1621,7 +1621,7 @@ describe('zodv4', () => {
export function GeometrySchema(): z.ZodObject<Properties<Geometry>> {
return z.object({
__typename: z.literal('Geometry').optional(),
shape: ShapeSchema().nullish()
shape: z.lazy(() => ShapeSchema().nullish())
})
}
"
Expand Down Expand Up @@ -1772,7 +1772,7 @@ describe('zodv4', () => {

export const GeometrySchema: z.ZodObject<Properties<Geometry>> = z.object({
__typename: z.literal('Geometry').optional(),
shape: ShapeSchema.nullish()
shape: z.lazy(() => ShapeSchema.nullish())
});
"
`)
Expand Down Expand Up @@ -1924,14 +1924,14 @@ describe('zodv4', () => {

export function BookSchema(): z.ZodObject<Properties<Book>> {
return z.object({
author: AuthorSchema().nullish(),
author: z.lazy(() => AuthorSchema().nullish()),
title: z.string().nullish()
})
}

export function AuthorSchema(): z.ZodObject<Properties<Author>> {
return z.object({
books: z.array(BookSchema().nullable()).nullish(),
books: z.array(z.lazy(() => BookSchema().nullable())).nullish(),
name: z.string().nullish()
})
}
Expand Down Expand Up @@ -1987,15 +1987,15 @@ describe('zodv4', () => {
export function BookSchema(): z.ZodObject<Properties<Book>> {
return z.object({
title: z.string(),
author: AuthorSchema()
author: z.lazy(() => AuthorSchema())
})
}

export function TextbookSchema(): z.ZodObject<Properties<Textbook>> {
return z.object({
__typename: z.literal('Textbook').optional(),
title: z.string(),
author: AuthorSchema(),
author: z.lazy(() => AuthorSchema()),
courses: z.array(z.string())
})
}
Expand All @@ -2004,15 +2004,15 @@ describe('zodv4', () => {
return z.object({
__typename: z.literal('ColoringBook').optional(),
title: z.string(),
author: AuthorSchema(),
author: z.lazy(() => AuthorSchema()),
colors: z.array(z.string())
})
}

export function AuthorSchema(): z.ZodObject<Properties<Author>> {
return z.object({
__typename: z.literal('Author').optional(),
books: z.array(BookSchema()).nullish(),
books: z.array(z.lazy(() => BookSchema())).nullish(),
name: z.string().nullish()
})
}
Expand Down