> ## Content Index
> Fetch the complete content index at: https://catalins.tech/llms.txt
> Use this file to discover other available public pages before exploring further.

# Share Zod Validation Schema Between Server and Client
- URL: https://catalins.tech/share-zod-validation-schema-between-server-and-client/
- Published: 2023-04-07T15:04:41.000Z
- Updated: 2023-07-15T12:12:51.000Z
- Description: This article illustrates how to share the Zod validation schemas between the server and client.
- Author: Catalin Pit
- Tags: Zod

I recently encountered a scenario where the server and the client needed to use the same Zod validation schema.

The [only solution](https://kitchen-sink.trpc.io/react-hook-form) I could find was to define the schema on the client and then import it on the server. However, sharing the validation schema from the client to the server is not the best idea.

## Solution

My solution is to extract the validation schema (or schemas) in a separate file and import it (them) where necessary.

Let's take the following validation schemas as an example:

```
import * as z from "zod";

export const FormSchema = z.object({
  username: z.string(),
  email: z.string(),
  isAdmin: z.boolean(),
  createdAt: z.date(),
});

export const BlogPostSchema = z.object({
  title: z.string(),
  content: z.string(),
  url: z.string(),
  published: z.boolean(),
  cover_img: z.string(),
});

```

You could add the code in the `src/utils/schemas.tsx` file and import the schemas where needed.

> If you are using Zod, you might be interested in these articles about [form validation with React Hook Form and Zod](https://catalins.tech/form-validation-with-react-hook-form-zod-typescript/) and [validating environment variables with Zod](https://catalins.tech/validate-environment-variables-with-zod/).