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:
- Install Zod: If you haven't already, install Zod in your project by running the following command:
npm install zod
- import Zod into your file and define your schema. For example, here's how you might create a schema for a blog post:
- 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: