Modify existing schema #887
-
I want to reuse same register schema on both client and server. Naturally I don't send On server i call schema like this: const validateUserRegister = withValidation({
schema: userRegisterSchema, // bypass confirmPassword check here
type: 'Zod',
mode: 'body',
}); schema: export const userRegisterSchema = z
.object({
email: z.string().email(),
password: z.string().min(passwordMin).max(passwordMax),
// +
confirmPassword: z.string().optional().or(z.literal('')),
name: z.string().min(nameMin).max(nameMax),
username: z.string().min(usernameMin).max(usernameMax),
})
.refine((data) => data.confirmPassword === data.password, {
message: "Passwords don't match.",
path: ['confirmPassword'],
}); |
Beta Was this translation helpful? Give feedback.
Answered by
JacobWeisenburger
Jan 31, 2022
Replies: 1 comment 1 reply
-
It looks like you are wanting .pick/.omit I hope this helps. const userRegisterSchema = z.object( {
email: z.string().email(),
password: z.string().min( passwordMin ).max( passwordMax ),
confirmPassword: z.string().optional().or( z.literal( '' ) ),
name: z.string().min( nameMin ).max( nameMax ),
username: z.string().min( usernameMin ).max( usernameMax ),
} ).refine( ( data ) => data.confirmPassword === data.password, {
message: "Passwords don't match.",
path: [ 'confirmPassword' ],
} )
const userRegisterSchemaOnlyEmailAndPassword = userRegisterSchema.innerType().pick( {
email: true,
password: true,
} )
type UserRegisterSchemaOnlyEmailAndPassword = z.infer<typeof userRegisterSchemaOnlyEmailAndPassword>
// type UserRegisterSchemaOnlyEmailAndPassword = {
// email: string;
// password: string;
// }
const userRegisterSchemaWithoutConfirmPassword = userRegisterSchema.innerType().omit( {
confirmPassword: true,
} )
type UserRegisterSchemaWithoutConfirmPassword = z.infer<typeof userRegisterSchemaWithoutConfirmPassword>
// type UserRegisterSchemaWithoutConfirmPassword = {
// email: string;
// password: string;
// name: string;
// username: string;
// } |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nemanjam
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like you are wanting .pick/.omit
I hope this helps.