typescript 为什么Zod让我的所有模式字段都是可选的?

deikduxw  于 2023-05-19  发布在  TypeScript
关注(0)|答案(3)|浏览(189)

我在我的Express & TypeScript & Mongoose API项目中使用Zod,当试图根据用户模式验证我的用户输入时,它返回类型冲突:

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'

下面是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;
}

如何使这些Zod模式字段都不是可选的,因为它是使它在默认情况下是可选的?

50pmv0ei

50pmv0ei1#

这可能是由于在TypeScript编译器选项中未使用strict: true导致的,如README文件中的安装部分所述。
下面的基本tsconfig.json文件可以解决这个问题:

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

qhhrdooz2#

我建议创建一个基本的版本,然后用“passwordConfirmation”和相对的细化来扩展它。同样重要的是,从zod到 typescript 的“推断”,使 typescript 可用。

import { object, string, z } from 'zod';

export const userSchema = 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'),
  email: string({
    required_error: 'Email is required',
  })
    .email('Not a valid email')
    .nonempty(),
});
type userSchema = z.infer<typeof userSchema>;

export const createUserSchema = object({
  body: userSchema
    .extend({
      passwordConfirmation: string({
        required_error: 'Confirm password is required',
      }),
    })
    .refine((data) => data.password === data.passwordConfirmation, {
      message: 'Passwords do not match',
      path: ['passwordConfirmation'],
    }),
});
type createUserSchema = z.infer<typeof createUserSchema>;

const us: userSchema = {
  email: '',
  firstName: '',
  lastName: '',
  password: '',
};

const cui: createUserSchema = {
  body: {
    email: '',
    firstName: '',
    lastName: '',
    password: '',
    passwordConfirmation: '',
  },
};
iqxoj9l9

iqxoj9l93#

正如@pro0gramist在评论中指出的那样,公认的答案应该是在tsconfig.json中添加此设置:

{
    "compilerOptions": {
        "strictNullChecks": true   
    }
}

根据默认的zod配置,您可能还需要在模式中添加以下内容:

requiredField: z.string().nonempty()

此方法比"strict": true方法侵入性更小,后者向代码库添加了更多的类型脚本约束。

相关问题