Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ $ pnpm add better-zod-errors

## Usage

First, you need to validate your data using Zod schema. If validation fails, you can catch the `ZodError` and format the error messages using `formatErrors` function from this library.
First, you need to validate your data using Zod schema. If validation fails, you can catch the `ZodError` and format the error messages using `formatError` function from this library.

```typescript
import { z } from "zod";
import { formatErrors } from "better-zod-errors";
import { formatError } from "better-zod-errors";

const schema = z.object({
name: z.string().min(2, "Name must be at least 2 characters long"),
Expand All @@ -56,7 +56,7 @@ try {
} catch (e) {
if (e instanceof z.ZodError) {
for (const issue of e.issues) {
const formattedError = formatErrors(issue, payload);
const formattedError = formatError(issue, payload);
console.log(formattedError); // formatted error message with code frame
}
} else {
Expand All @@ -67,7 +67,7 @@ try {

## API

### `formatErrors(issue: z.ZodIssue, data: any, [options: FormatErrorOptions]): string`
### `formatError(issue: z.ZodIssue, data: any, [options: FormatErrorOptions]): string`

Formats a single Zod issue into a more readable error message with a code frame.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`formatErrors Error Formatting should format errors in arrays 1`] = `
exports[`formatError Error Formatting should format errors in arrays 1`] = `
" 3 | "id": 1,
> 4 | "value": "ok"
> | ^^^^ Too small: expected string to have >=3 characters
5 | },
6 | {"
`;

exports[`formatErrors Error Formatting should format errors in arrays 2`] = `
exports[`formatError Error Formatting should format errors in arrays 2`] = `
" 7 | "id": 2,
> 8 | "value": "no"
> | ^^^^ Too small: expected string to have >=3 characters
9 | },
10 | {"
`;

exports[`formatErrors Error Formatting should format errors in arrays 3`] = `
exports[`formatError Error Formatting should format errors in arrays 3`] = `
" 10 | {
> 11 | "id": "three",
> | ^^^^^^^ Invalid input: expected number, received string
12 | "value": "valid"
13 | }"
`;

exports[`formatErrors Error Formatting should format errors with code frames 1`] = `
exports[`formatError Error Formatting should format errors with code frames 1`] = `
" 2 | "name": "John Doe",
> 3 | "age": -5,
> | ^^ Too small: expected number to be >=0
4 | "address": {
5 | "street": "123 Main St","
`;

exports[`formatErrors Error Formatting should format errors with code frames 2`] = `
exports[`formatError Error Formatting should format errors with code frames 2`] = `
" 5 | "street": "123 Main St",
> 6 | "city": 456
> | ^^^ Invalid input: expected string, received number
7 | }
8 | }"
`;

exports[`formatErrors Error Formatting should handle nested errors correctly 1`] = `
exports[`formatError Error Formatting should handle nested errors correctly 1`] = `
" 2 | "user": {
> 3 | "id": "invalid-uuid",
> | ^^^^^^^^^^^^^^ Invalid UUID
4 | "profile": {
5 | "email": "not-an-email""
`;

exports[`formatErrors Error Formatting should handle nested errors correctly 2`] = `
exports[`formatError Error Formatting should handle nested errors correctly 2`] = `
" 4 | "profile": {
> 5 | "email": "not-an-email"
> | ^^^^^^^^^^^^^^ Invalid email address
6 | }
7 | }"
`;

exports[`formatErrors Error Formatting should handle root level errors 1`] = `
exports[`formatError Error Formatting should handle root level errors 1`] = `
"> 1 | -1
> | ^^ Too small: expected number to be >=0"
`;

exports[`formatErrors Error Formatting should handle root level errors 2`] = `
exports[`formatError Error Formatting should handle root level errors 2`] = `
"> 1 | "no"
> | ^^^^ Too small: expected string to have >=3 characters"
`;

exports[`formatErrors Error Formatting should handle root level errors 3`] = `
exports[`formatError Error Formatting should handle root level errors 3`] = `
" 2 | true,
> 3 | "false",
> | ^^^^^^^ Invalid input: expected boolean, received string
4 | 123
5 | ]"
`;

exports[`formatErrors Error Formatting should handle root level errors 4`] = `
exports[`formatError Error Formatting should handle root level errors 4`] = `
" 3 | "false",
> 4 | 123
> | ^^^ Invalid input: expected boolean, received number
Expand Down
22 changes: 11 additions & 11 deletions src/formatErrors.spec.ts → src/formatError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import z from 'zod'
import { formatErrors } from './formatErrors'
import { formatError } from './formatError.ts'

describe('formatErrors', () => {
describe('formatError', () => {
describe('Error Formatting', () => {
it('should format errors with code frames', () => {
const schema = z.object({
Expand All @@ -26,7 +26,7 @@ describe('formatErrors', () => {

if (!result.success) {
const formattedErrors = result.error.issues.map((issue) =>
formatErrors(issue, payload, {
formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
}),
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('formatErrors', () => {

if (!result.success) {
const formattedErrors = result.error.issues.map((issue) =>
formatErrors(issue, payload, {
formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
}),
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('formatErrors', () => {

if (!result.success) {
const formattedErrors = result.error.issues.map((issue) =>
formatErrors(issue, payload, {
formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
}),
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('formatErrors', () => {
const result = schema.safeParse(payload)
if (!result.success) {
const formattedErrors = result.error.issues.map((issue) =>
formatErrors(issue, payload, {
formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
}),
Expand Down Expand Up @@ -162,11 +162,11 @@ describe('formatErrors', () => {
throw new Error('No issues found in the error')
}

const errorWithColor = formatErrors(issue, payload, {
const errorWithColor = formatError(issue, payload, {
useColor: true,
syntaxHighlighting: false,
})
const errorWithoutColor = formatErrors(issue, payload, {
const errorWithoutColor = formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
})
Expand Down Expand Up @@ -196,11 +196,11 @@ describe('formatErrors', () => {
throw new Error('No issues found in the error')
}

const errorWithHighlighting = formatErrors(issue, payload, {
const errorWithHighlighting = formatError(issue, payload, {
useColor: false,
syntaxHighlighting: true,
})
const errorWithoutHighlighting = formatErrors(issue, payload, {
const errorWithoutHighlighting = formatError(issue, payload, {
useColor: false,
syntaxHighlighting: false,
})
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('formatErrors', () => {
}

expect(() =>
formatErrors(issue, invalidPayload, {
formatError(issue, invalidPayload, {
useColor: false,
syntaxHighlighting: false,
}),
Expand Down
2 changes: 1 addition & 1 deletion src/formatErrors.ts → src/formatError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FormatErrorOptions {
syntaxHighlighting?: boolean
}

export function formatErrors(
export function formatError(
issue: z.core.$ZodIssue,
payload: number | bigint | boolean | string | object,
options?: FormatErrorOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './formatErrors'
export * from './formatError.ts'
Loading