代码之家  ›  专栏  ›  技术社区  ›  Bhomik Ranjan

trim,toLowerCase转换在zod中不起作用

  •  -1
  • Bhomik Ranjan  · 技术社区  · 1 年前
    import { z } from "zod";
    
    const signupSchema = z.object({
        name: z
            .string({
                required_error: "Please provide your name",
                invalid_type_error: "Your name must be a string",
            })
            .min(3, { message: "Your name must be atleast 3 characters long" })
            .max(32, { message: "Your name cannot exceed 32 characters" })
            .trim(),
        username: z
            .string({
                required_error: "Please provide your username",
                invalid_type_error: "Username must be a string",
            })
            .min(3, { message: "Username must be atleast 3 characters long" })
            .max(32, { message: "Username cannot exceed 32 characters" })
            .trim()
            .toLowerCase(),
        email: z
            .string({
                required_error: "Please provide your email",
                invalid_type_error: "Email must be a string",
            })
            .email({ message: "Please provide a valid email address" })
            .trim()
            .toLowerCase(),
        password: z
            .string({
                required_error: "Please provide your password",
                invalid_type_error: "Password must be a string",
            })
            .min(8, { message: "Password must be atleast 8 characters long" })
            .regex(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/gm, {
                message:
                    "Password must include at least one uppercase letter, one lowercase letter, one digit and one special character",
            }),
        bio: z
            .string({ invalid_type_error: "Bio must be a string" })
            .max(256, { message: "Bio cannot exceed 256 characters" })
            .trim()
            .optional(),
    });
    
    export { signupSchema };
    

    我用它来验证请求,验证工作正常,但trim()、toLowerCase()等转换不起作用。我正在使用wait-signaupSchema.parseAsync(body)。请帮帮我

    0 回复  |  直到 1 年前