Wrap zod validation errors in user-friendly readable messages

Overview

zod-validation-error

Wrap zod validation errors in user-friendly readable messages.

Build Status npm version

Features

  • User-friendly readable messages, configurable via options;
  • Maintain original errors under error.details;
  • Extensive tests.

Installation

npm install zod-validation-error

Requirements

  • Node.js v.14+

Quick start

// create zod schema
const zodSchema = zod.object({
  id: zod.number().int().positive(),
  email: zod.string().email(),
});

// parse some invalid value
try {
  zodSchema.parse({
    id: 1,
    email: 'foobar', // note: invalid email
  });
} catch (err) {
  const validationError = fromZodError(err);
  // the error now is readable by the user
  // you may print it to console
  // or return it via an API
  console.log(validationError);
}

Motivation

Zod errors are difficult to consume for the end-user. This library wraps Zod validation errors in user-friendly readable messages that can be exposed to the outer world, while maintaining the original errors in an array for dev use.

Example

Input (from Zod)

[
  {
    "code": "too_small",
    "inclusive": false,
    "message": "Number must be greater than 0",
    "minimum": 0,
    "path": ["id"],
    "type": "number"
  },
  {
    "code": "invalid_string",
    "message": "Invalid email",
    "path": ["email"],
    "validation": "email"
  }
]

Output

Validation error: Number must be greater than 0 at "id"; Invalid email at "email"

Contribute

Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.

We are hiring

Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.

License

MIT

Comments
  • build: drop SWC; use TS for cjs/esm bundles

    build: drop SWC; use TS for cjs/esm bundles

    What changed

    • Drop SWC in favour of using just TypeScript
    • Introduce 3 different tsconfigs. 1) ESM, 2) CJS, 3) Types
    • Completely drop exports key

    Other References

    Beta link

    https://www.npmjs.com/package/zod-validation-error/v/0.2.2-beta.1

    opened by dimitrisnl 3
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    [email protected]

    Minor Changes

    • 59ad8df: Expose isValidationErrorLike type-guard
    opened by github-actions[bot] 0
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    [email protected]

    Patch Changes

    • fa81c9b: Drop SWC; Fix ESM export
    opened by github-actions[bot] 0
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    [email protected]

    Patch Changes

    • 7f420d1: Update build and npm badges on README.md
    opened by github-actions[bot] 0
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    [email protected]

    Minor Changes

    • fde2f50: update dependency in package json so the user does not have to manually install it, will be installed on package install.
    opened by github-actions[bot] 0
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    [email protected]

    Patch Changes

    • 67336ac: Enable automatic release to npm
    opened by github-actions[bot] 0
  • Version Packages

    Version Packages

    This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

    Releases

    @causaly/[email protected]

    Minor Changes

    • fcda684: Initial functionality
    opened by github-actions[bot] 0
  • Introduce ValidationError with main functionality

    Introduce ValidationError with main functionality

    This PR is introducing a new ValidationError class alongside related functionality to convert ZodError to ValidationError and compose a user-friendly readable message.

    opened by jmike 0
Releases(v0.3.0)
Owner
Causaly
Biomedical search platform
Causaly
Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

openapi-zod-client Generates a zodios (typescript http client with zod validation) from a (json/yaml) OpenAPI spec (or just use the generated schemas/

Alexandre Stahmer 104 Jan 4, 2023
A crash course on Zod - a schema validation library for TypeScript

Zod Crash Course This Zod crash course will give you everything you ever needed to know about Zod - an amazing library for building type-safe AND runt

Total TypeScript 339 Dec 28, 2022
Send encrypted and decrypted messages with verifiable keys and human readable names.

zooko-msg Encrypt and decrypt messages using AES with a preshared ECDH key generated using keys associated with Handshake names. I noticed that there

Publius Federalist 31 Jul 27, 2022
A live instant messaging app that translates user messages into the other user's preferred language.

BabelFish Description BabelFish is an interactive multi-person chat app with built in live translation. It is created using the MERN stack (MongoDB, E

Jamel 3 Jul 18, 2022
RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be displayed. Try it!

RenderIf RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be disp

Oscar Cornejo Aguila 6 Jul 12, 2022
This project is a user friendly project that provides the accurate constellation name of the user

OVERVIEW: This is a group project by certain members of the 100Devs community. Contributors Lee Gill | GitHub: LeeGee64 | Twitter: @LeeGee64 | LinkedI

Lee Gill 5 Jun 15, 2022
A chat logs online saver for discord bots to save messages history & cleared messages online

Chat Logs NPM package that saves messages online to view it later Useful for bots where users can save messages history & cleared messages online Supp

TARIQ 8 Dec 28, 2022
Wrap selected text in custom tags with shortcuts.

Obsidian Wrap With Shortcuts Wrap the selected text in customized tags with shortcuts. Underline is provided with Ctrl-u(Cmd-u) as default wrappers. P

Manic Chuang 24 Dec 28, 2022
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
Prisma 2+ generator to emit Zod schemas from your Prisma schema

Prisma Zod Generator Automatically generate Zod schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 212 Dec 27, 2022
Zod utilities for Remix loaders and actions.

Zodix Zodix is a collection of Zod utilities for Remix loaders and actions. It abstracts the complexity of parsing and validating FormData and URLSear

Riley Tomasek 172 Dec 22, 2022
Convert JSON to human readable HTML

json.human.js: Json Formatting for Human Beings A small library to convert a JSON object into a human readable HTML representation that is easy to sty

Mariano Guerra 955 Dec 3, 2022
Render readable & responsive tables in the terminal

terminal-columns Readable tables for the terminal. Tables can be automatically responsive! Features Content wrapped to fit column width Column widths

hiroki osame 28 Oct 28, 2022
⏱ Simple Alpine.js plugin to display the human-readable distance between a date and now.

⏱ Alpine TimeAgo ⏱ An Alpine.js plugin to return the distance between a given date and now in words (like "3 months ago", "about 2 hours ago" or "in a

Marc Reichel 47 Dec 22, 2022
⛲ Sort import declarations into a pleasing and readable cascade.

⛲ eslint-plugin-cascading-imports This plugin allows to automatically enforce a visual "cascading" order for import declarations. Imports in each bloc

Florent 1 Jan 20, 2022
A compiled-away, type-safe, readable RegExp alternative

?? magic-regexp A compiled-away, type-safe, readable RegExp alternative ✨ Changelog ?? Documentation ▶️ Online playground Features Runtime is zero-dep

Daniel Roe 1.5k Jan 8, 2023
Enhanced interval features for Node.js, such as promisified interval and human readable time parsing.

Interval-next Interval-next is a package that extends Javascript's built-in setInterval() capabilities. You have a plain and promisified interval meth

Snowy 5 Jul 28, 2022
Library for readable and manageable Next.js middleware

?? Next Compose Middleware This is a library for building Next.js complex middleware declaratively. You can create highly readable and manageable midd

Ibuki Kaji 14 Dec 19, 2022