Validations

This section outlines the various validation schemas used to ensure data integrity and consistency.

Features

🗂️ Zod : Utilizes the Zod library, a schema declaration and validation library, to define and validate the structure of our data objects.

📖 Chaining and Composition: Easily create complex schemas by combining simpler ones.

📝 Detailed Error Messages: Provides informative error messages for easier debugging.

👤 Transformation: Allows for data transformation during parsing.

What's Included

Blog Category Schema

The blogCategorySchema is a Zod schema that validates the structure of a blog category object. This schema ensures that the name field is a string with a minimum length of 3 characters.

Blog Post Schema

The blogPostSchema is a Zod schema that validates the structure of a blog post object. This schema ensures that the name field is a string with a minimum length of 3 characters and includes a nested blogCategory object with value and label fields, which are also required to be strings.

Newsletter Schema

The newsletterSchema is a Zod schema that validates the structure of a newsletter subscription object. This schema ensures that the email field is a properly formatted email address.

Role Schema

The roleSchema is a Zod schema that validates the structure of a role object. This schema ensures that the name field is a string with a minimum length of 3 characters.

Story Schema

The storySchema is a Zod schema that validates the structure of a story object. This schema ensures that the name field is a string with a minimum length of 3 characters.

How To Do Guides

How to Create a Validation Schema

All validation schemas should be stored in the /src/validations directory of your project. Follow these steps to create your new schema:

  1. Install Zod: If you haven't already, install Zod in your project by running the following command: npm install zod
  2. import Zod into your file and define your schema. For example, here's how you might create a schema for a blog post:
import { z } from 'zod';
 
export const blogPostSchema = z.object({
  name: z.string().min(3, 'Name must be at least 3 characters long'),
  blogCategory: z.object({
    value: z.string(),
    label: z.string(),
  }),
});
  1. Use the Schema for Validation: Once you've defined your schema, you can use it to validate data objects. Here's an example of how to use the schema:
const {
  control,
  formState: { errors },
  register,
  handleSubmit,
  watch,
} = useForm({
  resolver: zodResolver(blogPostSchema),
  defaultValues,
});