UNPKG

nestjs-typebox

Version:

This library provides helper utilities for writing and validating NestJS APIs using [TypeBox](https://github.com/sinclairzx81/typebox) as an alternative to class-validator/class-transformer. Can be configured to patch @nestjs/swagger allowing OpenAPI gene

26 lines (22 loc) 960 B
import { BadRequestException, HttpStatus } from '@nestjs/common'; import { ValueError, ValueErrorIterator, ValueErrorType } from '@sinclair/typebox/errors'; import type { ValidatorType } from './types.js'; export class TypeboxValidationException extends BadRequestException { constructor(type: ValidatorType, errors: ValueErrorIterator) { const topLevelErrors: ValueError[] = []; const unionPaths: string[] = []; for (const error of errors) { // don't deeply traverse union errors to reduce error noise if (unionPaths.some(path => error.path.includes(path))) continue; if (error.type === ValueErrorType.Union) { unionPaths.push(error.path); } topLevelErrors.push(error); } super({ statusCode: HttpStatus.BAD_REQUEST, message: `Validation failed (${type})`, errors: topLevelErrors, }); } }