-
I'm trying to combine zod schema validation with the builder.prismaCreate method, but it seems it does not work for nested objects. My example is this: builder.mutationField('createRecipe', (t) =>
t.withAuth({ public: true }).prismaField({
errors: {
types: [ZodError],
},
type: 'Recipe',
args: {
data: t.arg({ type: CreateRecipeInput, required: true }),
},
resolve: async (query, root, args, context) => {
const recipe = await db.recipe.create({
...query,
data: {
...args.data,
author: {
connect: {
id: context.user.id,
},
},
},
});
pubSub.publish('recipeCreated', { recipe });
return recipe;
},
}),
);
const stepSchema = z.object({
order: z.number().int().positive(),
description: z.string().trim().nonempty(),
});
const recipeSchema = z.object({
title: z.string().trim().nonempty(),
description: z.string().trim().nonempty(),
servings: z.number().int().positive(),
});
export const CreateRecipeInput: InputObjectRef<Prisma.RecipeCreateInput> = builder.prismaCreate('Recipe', {
name: 'CreateRecipeInput',
fields: () => ({
title: 'String',
description: 'String',
servings: 'Int',
steps: RecipeCreateSteps,
}),
validate: {
schema: recipeSchema,
},
});
export const StepCreate: InputObjectRef<Prisma.StepCreateInput> = builder.prismaCreate('Step', {
name: 'StepCreate',
fields: () => ({
order: 'Int',
description: 'String',
}),
validate: {
schema: stepSchema,
},
});
export const RecipeCreateSteps = builder.prismaCreateRelation('Recipe', 'steps', {
fields: () => ({
create: StepCreate,
}),
}); It seems only the schema validation is run for the "main" object CreateRecipeInput, not StepCreate, as the graphql api does not give any errors for this mutation for example: mutation MyMutation {
createRecipe(
data: {description: "d", servings: 10, title: "d", steps: {create: {description: " ", order: -1}}}
) {
... on CreateRecipeSuccess {
__typename
data {
steps {
order
description
}
}
}
... on ValidationError {
__typename
fieldErrors {
message
path
}
}
}
} which gives response: {
"data": {
"createRecipe": {
"__typename": "CreateRecipeSuccess",
"data": {
"steps": [
{
"order": -1,
"description": " "
}
]
}
}
}
} Am I doing something wrong or is it simply not supported to do validation the way I try to do it in the code above? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
this may be a bug, I don't see anything wrong with they way you are defining things |
Beta Was this translation helpful? Give feedback.
-
After some debugging, this is an easy thing to run into, but I'm not sure if there is a good way to fix it. zod.object() by default will strip out unknown properties. You need to do |
Beta Was this translation helpful? Give feedback.
After some debugging, this is an easy thing to run into, but I'm not sure if there is a good way to fix it.
zod.object() by default will strip out unknown properties. You need to do
zod.object({...}).passthrough()
if you are providing a partial schema. The nested schemas will run correctly, provided that the values are not stripped out by another schema.