17 lines
546 B
TypeScript
17 lines
546 B
TypeScript
import type { PipeTransform } from '@nestjs/common';
|
|
import { z } from 'zod';
|
|
import { AppError } from './errors/app-error';
|
|
|
|
export class SchemaPipe<T> implements PipeTransform<unknown, T> {
|
|
constructor(private readonly schema: z.ZodType<T>) {}
|
|
transform(value: unknown): T {
|
|
const result = this.schema.safeParse(value);
|
|
if (!result.success) {
|
|
throw new AppError('REQUEST_INVALID', 'SCHEMA_REJECTED', [
|
|
...new Set(result.error.issues.map((issue) => issue.path.join('.'))),
|
|
]);
|
|
}
|
|
return result.data;
|
|
}
|
|
}
|