mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { ZodValidationPipe } from '../../../src/nest/common/zod-validation.pipe';
|
|
import { HttpException } from '@nestjs/common';
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import { z } from 'zod';
|
|
|
|
describe('ZodValidationPipe', () => {
|
|
const pipe = new ZodValidationPipe(z.object({ name: z.string().min(1) }));
|
|
const meta = {} as never;
|
|
|
|
it('returns the parsed value for valid input', () => {
|
|
expect(pipe.transform({ name: 'x' }, meta)).toEqual({ name: 'x' });
|
|
});
|
|
|
|
it('throws TREK { error } envelope with status 400 on invalid input', () => {
|
|
let thrown: unknown;
|
|
try {
|
|
pipe.transform({ name: '' }, meta);
|
|
} catch (e) {
|
|
thrown = e;
|
|
}
|
|
expect(thrown).toBeInstanceOf(HttpException);
|
|
expect((thrown as HttpException).getStatus()).toBe(400);
|
|
expect((thrown as HttpException).getResponse()).toHaveProperty('error');
|
|
});
|
|
|
|
it("labels a root-level (empty path) issue as 'body'", () => {
|
|
const rootPipe = new ZodValidationPipe(z.string());
|
|
let thrown: unknown;
|
|
try {
|
|
rootPipe.transform(123, meta);
|
|
} catch (e) {
|
|
thrown = e;
|
|
}
|
|
expect(thrown).toBeInstanceOf(HttpException);
|
|
const body = (thrown as HttpException).getResponse() as { error: string };
|
|
expect(body.error).toMatch(/^body: /);
|
|
});
|
|
|
|
it('joins multiple issues with a semicolon', () => {
|
|
const multiPipe = new ZodValidationPipe(z.object({ a: z.string(), b: z.number() }));
|
|
let thrown: unknown;
|
|
try {
|
|
multiPipe.transform({ a: 1, b: 'x' }, meta);
|
|
} catch (e) {
|
|
thrown = e;
|
|
}
|
|
const body = (thrown as HttpException).getResponse() as { error: string };
|
|
expect(body.error).toContain('a: ');
|
|
expect(body.error).toContain('b: ');
|
|
expect(body.error).toContain('; ');
|
|
});
|
|
});
|