feat(validation): ValidationError → 422 with source + issues (before HttpError)
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+40
-3
No files matched your search
@@ -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' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user