Validation error with zodValidationPipe in nestjs

Validation error with zodValidationPipe in nestjs


0

I have the following graphql.

input UpdateStudentInput {
  name: String
  motherName: String
  birthDate: String
  gender: Gender
  email: String
  password: String
  avatar: String
}

And I have the following mutation:

updateStudent(updateStudentInput: UpdateStudentInput!, studentId: String): Student!

but when I try to mutate I get a validation error :

mutation {
  updateStudent(updateStudentInput: {
    name: "Abner Matheus Gomes 23"
  }, studentId: "190fc1f5-2e47-4ead-9a00-352e2081a96f") {
    id
    name
    motherName
    birthDate
    gender
    email
    avatar
    isActive
    createdAt
    updatedAt
    createdBy
    updatedBy
  }
}

The error:

{
  "errors": [
    {
      "message": "Validation failed",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateStudent"
      ],
      "extensions": {
        "code": "BAD_REQUEST",
        "stacktrace": [
          "ZodValidationException: Validation failed",
          "    at createZodValidationException (/home/theabnermatheus/Workspace/interacoope-server/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-zod/dist/index.js:85:10)",
          "    at validate (/home/theabnermatheus/Workspace/interacoope-server/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-zod/dist/index.js:95:11)",
          "    at ZodValidationPipe2.transform (/home/theabnermatheus/Workspace/interacoope-server/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-zod/dist/index.js:942:16)",
          "    at /home/theabnermatheus/Workspace/interacoope-server/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/@nestjs/core/pipes/pipes-consumer.js:16:33",
          "    at processTicksAndRejections (node:internal/process/task_queues:95:5)"
        ],
        "originalError": {
          "statusCode": 400,
          "message": "Validation failed",
          "errors": [
            {
              "code": "invalid_type",
              "expected": "object",
              "received": "string",
              "path": [],
              "message": "Expected object, received string"
            }
          ]
        }
      }
    }
  ],
  "data": null
}

my resolver:

  @Mutation('updateStudent')
  @UsePipes(new ZodValidationPipe(UpdateStudentInputSchema))
  async update(
    @Args('updateStudentInput') updateStudentInput: UpdateStudentInput,
    @Args('studentId') studentId: string,
  ): Promise<Student> {
    console.log('updateStudentInput', updateStudentInput);
    return await this.studentService.update(studentId, updateStudentInput);
  }

and my pipeValidationDto:

export const UpdateStudentInputSchema = z.object({
  name: z.string().min(3).max(255).optional(),
  motherName: z.string().min(3).max(255).optional(),
  birthDate: z.string().min(3).max(30).optional(),
  gender: z.string().min(3).max(10).optional(),
  email: z.string().email().optional(),
  password: z.string().optional(),
  avatar: z.string().optional(),
});

export class UpdateStudentInput extends createZodDto(
  UpdateStudentInputSchema,
) {}

where is the error ?

I’m doing the same thing in other mutations and it works normally.

note: i want the user to be able to enter only one of the fields and not have to enter all of them.


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *