From 101833d52d6a060415c6b6722553792b69461040 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 6 Jul 2026 21:29:27 -0500 Subject: [PATCH] =?UTF-8?q?feat(validation):=20ValidationError=20=E2=86=92?= =?UTF-8?q?=20422=20with=20source=20+=20issues=20(before=20HttpError)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Diego Resendez --- src/signals.ts | 8 ++++++++ src/transformers.ts | 14 +++++++++++++- test/transformers.test.ts | 21 +++++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/signals.ts b/src/signals.ts index d9f36a7..3b47c24 100644 --- a/src/signals.ts +++ b/src/signals.ts @@ -1,3 +1,5 @@ +import type { StandardIssue } from './standard-schema'; + export class HttpError extends Error { constructor(readonly status: number, message?: string, readonly body?: unknown) { super(message ?? `HTTP ${status}`); @@ -21,6 +23,12 @@ export class Redirect extends HttpError { constructor(readonly location: string) { super(302); } } +export class ValidationError extends HttpError { + constructor(public issues: StandardIssue[], public source: string) { + super(422, 'Validation failed'); + } +} + export function isHttpError(e: unknown): e is HttpError { return e instanceof HttpError; } diff --git a/src/transformers.ts b/src/transformers.ts index b208385..57c5989 100644 --- a/src/transformers.ts +++ b/src/transformers.ts @@ -1,5 +1,6 @@ -import { isHttpError, Redirect } from './signals'; +import { isHttpError, Redirect, ValidationError } from './signals'; import type { TransformerFn } from './metadata'; +import { flattenPath } from './standard-schema'; export const JsonTransformer: TransformerFn = (value) => ({ status: 200, @@ -8,6 +9,17 @@ export const JsonTransformer: TransformerFn = (value) => ({ }); export function errorToResponse(e: unknown): { status: number; headers: Record; body: string } { + if (e instanceof ValidationError) { + return { + status: 422, + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ + error: 'Validation failed', + source: e.source, + issues: e.issues.map((i) => ({ path: flattenPath(i), message: i.message })), + }), + }; + } if (isHttpError(e)) { const headers: Record = { 'content-type': 'application/json' }; if (e instanceof Redirect) headers.location = e.location; diff --git a/test/transformers.test.ts b/test/transformers.test.ts index fa3909d..f67cdb5 100644 --- a/test/transformers.test.ts +++ b/test/transformers.test.ts @@ -1,6 +1,6 @@ -import { expect, test } from 'vitest'; +import { describe, expect, it, test } from 'vitest'; import { JsonTransformer, errorToResponse } from '../src/transformers'; -import { Unauthorized, Redirect } from '../src/signals'; +import { HttpError, Unauthorized, Redirect, ValidationError } from '../src/signals'; test('JsonTransformer serializes value with json content-type', () => { expect(JsonTransformer({ a: 1 })).toEqual({ @@ -21,3 +21,20 @@ test('errorToResponse sets location for Redirect', () => { test('unknown error becomes 500', () => { expect(errorToResponse(new Error('boom')).status).toBe(500); }); + +describe('errorToResponse ValidationError', () => { + it('maps ValidationError to 422 with source + flattened issues (before generic HttpError)', () => { + const err = new ValidationError([{ message: 'Invalid email', path: ['email'] }], 'body'); + const r = errorToResponse(err); + expect(r.status).toBe(422); + expect(JSON.parse(r.body)).toEqual({ + error: 'Validation failed', source: 'body', + issues: [{ path: 'email', message: 'Invalid email' }], + }); + }); + it('still maps a plain HttpError to { error: message }', () => { + const r = errorToResponse(new HttpError(403, 'Nope')); + expect(r.status).toBe(403); + expect(JSON.parse(r.body)).toEqual({ error: 'Nope' }); + }); +});