Schemas package contains data schemas for the applications, including resource schemas.Data schema — is a Zod schema that defines shape of the entity. It must strictly define all fields. Resource schema is defined in entity.schema file e.x. user.schema.
Zod schemas simplify form validation in react-hook-form:
Copy
Ask AI
import { z } from 'zod';import { useForm } from 'react-hook-form';import { zodResolver } from '@hookform/resolvers/zod';import { EMAIL_REGEX, PASSWORD_REGEX } from 'app-constants';const schema = z.object({ firstName: z.string().min(1, 'Please enter First name').max(100), lastName: z.string().min(1, 'Please enter Last name').max(100), email: z.string().regex(EMAIL_REGEX, 'Email format is incorrect.'), password: z.string().regex(PASSWORD_REGEX, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),});type SignUpParams = z.infer<typeof schema>const SignUp = () => { const methods = useForm<SignUpParams>({ resolver: zodResolver(schema), }); return ( //your code here )}export default SignUp;
Additionally, data can be validated using the saveParse method:
Copy
Ask AI
const parsed = zodSchema.saveParse(data);if (!parsed.success) { throw new Error('Invalid data');}