0.0.1 • Published 5 months ago

format-zod-errors v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

A light weight utility for formatting validation errors from Zod schemas, returning an object with easy-to-access error messages for each field.

Motivation

Formatting zod errors can take some time to address. Also, other packages I used didn't return the way I expected and weren't as lightweight as wanted.

Installation

npm install format-zod-errors
# or
yarn add format-zod-errors

Usage

Using safeParse

import { z } from "zod";
import { f } from "format-zod-errors";

const personSchema = z.object({
  id: z.string(),
  email: z.string().email(),
  age: z.number().min(18),
  name: z.string().min(1).max(20),
  lastName: z.string().min(1).max(20),
});

const personData: z.infer<typeof personSchema> = {
  id: "1",
  email: "jane@email.com",
  age: 15,
  name: "Jane",
  lastName: "Doe",
};


const { error } = personSchema.safeParse(personData);

if (error) {
  const errorsMessages = f(error);

  // output
   {
    age: 'Number must be greater than or equal to 18'
   }
}

Or using parse

import { z } from "zod";
import { f } from "format-zod-errors";

const userSchema = z.object({
  name: z.string().min(1),
  lastName: z.string().min(1),
  address: z.object({
    street: z.string(),

    city: z.string(),
    state: z.string(),
    country: z.object({
      name: z.string(),
      code: z.number(),
    }),
  }),
});

const userData = {
  name: "John",
  lastName: 1,
  address: {
    street: "123 Main St",
    city: 123,
    country: { name: 5, code: "21" },
  },
};

 try {
      const data = userSchema.parse(userData);
      // it will throw an error

    } catch (error) {

      if (error instanceof z.ZodError) {
        const formattedErrors = f(error);

        // output
        {
          "lastName": "Expected string, received number",
          "address": {
              "city": "Expected string, received number",
              "state": "Required",
              "country": {
                "name": "Expected string, received number",
                "code": "Expected number, received string"
            }
          }
        }
      }
    }
0.0.1

5 months ago