feat(validation): ValidationError → 422 with source + issues (before HttpError)

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-06 21:29:27 -05:00
1 parent 81dc9d3a70
commit 101833d52d
3 files changed
+40 -3

No files matched your search

+8
View File
@@ -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;
}
+13 -1
View File
@@ -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<string, string>; 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<string, string> = { 'content-type': 'application/json' };
if (e instanceof Redirect) headers.location = e.location;
+19 -2
View File
@@ -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' });
});
});