From 6388336b3ca09f1dfc12c035208e39cd790b97f8 Mon Sep 17 00:00:00 2001 From: "Sophia (Turner)" Date: Wed, 17 Sep 2025 23:31:18 +0900 Subject: [PATCH] fix: correct typo in json formatting function name and update related tests --- README.md | 8 +++---- ....spec.ts.snap => formatError.spec.ts.snap} | 22 +++++++++---------- ...rmatErrors.spec.ts => formatError.spec.ts} | 22 +++++++++---------- src/{formatErrors.ts => formatError.ts} | 2 +- src/index.ts | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) rename src/__snapshots__/{formatErrors.spec.ts.snap => formatError.spec.ts.snap} (62%) rename src/{formatErrors.spec.ts => formatError.spec.ts} (91%) rename src/{formatErrors.ts => formatError.ts} (97%) diff --git a/README.md b/README.md index 089df30..7738227 100644 --- a/README.md +++ b/README.md @@ -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"), @@ -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 { @@ -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. diff --git a/src/__snapshots__/formatErrors.spec.ts.snap b/src/__snapshots__/formatError.spec.ts.snap similarity index 62% rename from src/__snapshots__/formatErrors.spec.ts.snap rename to src/__snapshots__/formatError.spec.ts.snap index bee4549..46eb998 100644 --- a/src/__snapshots__/formatErrors.spec.ts.snap +++ b/src/__snapshots__/formatError.spec.ts.snap @@ -1,6 +1,6 @@ // 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 @@ -8,7 +8,7 @@ exports[`formatErrors Error Formatting should format errors in arrays 1`] = ` 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 @@ -16,7 +16,7 @@ exports[`formatErrors Error Formatting should format errors in arrays 2`] = ` 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 @@ -24,7 +24,7 @@ exports[`formatErrors Error Formatting should format errors in arrays 3`] = ` 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 @@ -32,7 +32,7 @@ exports[`formatErrors Error Formatting should format errors with code frames 1`] 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 @@ -40,7 +40,7 @@ exports[`formatErrors Error Formatting should format errors with code frames 2`] 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 @@ -48,7 +48,7 @@ exports[`formatErrors Error Formatting should handle nested errors correctly 1`] 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 @@ -56,17 +56,17 @@ exports[`formatErrors Error Formatting should handle nested errors correctly 2`] 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 @@ -74,7 +74,7 @@ exports[`formatErrors Error Formatting should handle root level errors 3`] = ` 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 diff --git a/src/formatErrors.spec.ts b/src/formatError.spec.ts similarity index 91% rename from src/formatErrors.spec.ts rename to src/formatError.spec.ts index 51f9e56..24a1e90 100644 --- a/src/formatErrors.spec.ts +++ b/src/formatError.spec.ts @@ -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({ @@ -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, }), @@ -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, }), @@ -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, }), @@ -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, }), @@ -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, }) @@ -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, }) @@ -234,7 +234,7 @@ describe('formatErrors', () => { } expect(() => - formatErrors(issue, invalidPayload, { + formatError(issue, invalidPayload, { useColor: false, syntaxHighlighting: false, }), diff --git a/src/formatErrors.ts b/src/formatError.ts similarity index 97% rename from src/formatErrors.ts rename to src/formatError.ts index ce1b679..ca59fb5 100644 --- a/src/formatErrors.ts +++ b/src/formatError.ts @@ -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, diff --git a/src/index.ts b/src/index.ts index 2053df0..d416a74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export * from './formatErrors' +export * from './formatError.ts'