Package Exports
- @schema-hub/zod-error-formatter
- @schema-hub/zod-error-formatter/tuple/non-empty-array.js
- @schema-hub/zod-error-formatter/zod-error-formatter/formatter.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@schema-hub/zod-error-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
zod-error-formatter
Format errors generated by zod
into simple and easy-to-understand messages.
How it Works
- Each issue is prefixed by a path, except for top-level issues.
- Union errors are simplified, especially for simple cases like
string | number
. - Aggregated error messages are generated: a single line for single-issue errors and multiline for multi-issue errors.
Design Notes
The error message deliberately excludes the actual input passed to the schema parser. While this information could be helpful in certain cases, including it poses a risk of leaking sensitive information such as passwords or payment details. By omitting input data from error messages, we prioritize security and privacy. Instead, we provide the type of the input data to aid developers in understanding validation errors without compromising data privacy.
Example
Input:
import { safeParse } from '@schema-hub/zod-error-formatter';
const schema = z.union([z.string(), z.number()]);
const result = safeParse(schema, true);
Error message:
Validation failed: invalid value: expected one of string or number, but got boolean
Installation
npm install @schema-hub/zod-error-formatter
Usage
formatZodError(zodError, inputData)
Formats an instance of ZodError
into a new Error
instance with aggregated and formatted messages, along with an issues
array containing all individual formatted issues.
parse(schema, inputData)
Wraps zod
's schema.parse(inputData)
method and automatically formats any thrown ZodError
.
safeParse(schema, inputData)
Wraps zod
's schema.safeParse(inputData)
method and automatically formats any ZodError
in the failure result.