typescriptexpresszod

Why does Zod make all my schema fields optional?


I am using Zod inside my Express & TypeScript & Mongoose API project and when trying to validate my user input against the user schema it returns types conflicts:

  Argument of type '{ firstName?: string; lastName?: string; password?: string; passwordConfirmation?: string; email?: string; }' is not assignable to parameter of type 'UserInput'.
      Property 'email' is optional in type '{ firstName?: string; lastName?: string; password?: string; passwordConfirmation?: string; email?: string; }' but required in type 'UserInput'

Here is the schema def:

export const createUserSchema = object({
  body: object({
    firstName: string({
      required_error: 'First name is required',
    }),
    lastName: string({
      required_error: 'Last name is required',
    }).nonempty(),
    password: string({
      required_error: 'Password is required',
    })
      .nonempty()
      .min(6, 'Password too short - should be 6 chars minimum'),

    passwordConfirmation: string({
      required_error: 'Confirm password is required',
    }),
    email: string({
      required_error: 'Email is required',
    })
      .email('Not a valid email')
      .nonempty(),
  }).refine((data) => data.password === data.passwordConfirmation, {
    message: 'Passwords do not match',
    path: ['passwordConfirmation'],
  }),
});

export type CreateUserInput = Omit<TypeOf<typeof createUserSchema>, 'body.passwordConfirmation'>;


export interface UserInput {
  email: string;
  firstName: string;
  lastName: string;
  password: string;
}

How to make these Zod schema fields all not optional as it is making it optional by default?


Solution

  • This might be caused by not using strict: true in TypeScript compiler options as referred to installation section in README file.

    Following basic tsconfig.json file would fix that:

    {
        "compilerOptions": {
            "strict": true   
        }
    }