UNPKG

@nestjs/common

Version:

Decorators, pipes, exceptions and utilities for NestJS applications: @Controller(), @Injectable(), @Body(), ValidationPipe, HttpException and more.

116 lines (115 loc) • 6.6 kB
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface.js'; import { TransformerPackage } from '../interfaces/external/transformer-package.interface.js'; import { ValidationError } from '../interfaces/external/validation-error.interface.js'; import { ValidatorOptions } from '../interfaces/external/validator-options.interface.js'; import { ValidatorPackage } from '../interfaces/external/validator-package.interface.js'; import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface.js'; import { Type } from '../interfaces/type.interface.js'; import { ErrorHttpStatusCode } from '../utils/http-error-by-code.util.js'; /** * @publicApi */ export type ValidationErrorFormat = 'list' | 'grouped'; /** * @publicApi */ export interface ValidationPipeOptions extends ValidatorOptions { transform?: boolean; disableErrorMessages?: boolean; transformOptions?: ClassTransformOptions; errorHttpStatusCode?: ErrorHttpStatusCode; exceptionFactory?: (errors: ValidationError[]) => any; validateCustomDecorators?: boolean; expectedType?: Type<any>; validatorPackage?: ValidatorPackage; transformerPackage?: TransformerPackage; /** * Specifies the format of validation error messages. * - 'list': Returns an array of error message strings (default). The response message is `string[]`. * - 'grouped': Returns an object with property paths as keys and arrays of unmodified error messages as values. * The response message is `Record<string, string[]>`. Custom messages defined in validation decorators * (e.g., `@IsNotEmpty({ message: 'Name is required' })`) are preserved without parent path prefixes. * * @remarks * When using 'grouped', the `message` property in the error response changes from `string[]` to `Record<string, string[]>`. * If you have exception filters or interceptors that assume `message` is always an array, they will need to be updated. */ errorFormat?: ValidationErrorFormat; /** * Maximum total length, in characters, of the messages in an error response * of the built-in exception factory, counting the property path that comes * with each message. The errors past it are left out, and a last message * (in the 'grouped' format, a '...' entry) says so. Set it to `Infinity` to * lift the limit. * * @default 1_000_000 */ maxErrorMessagesLength?: number; } /** * @see [Validation](https://docs.nestjs.com/techniques/validation) * * @publicApi */ export declare class ValidationPipe implements PipeTransform { protected isTransformEnabled: boolean; protected isDetailedOutputDisabled?: boolean; protected validatorOptions: ValidatorOptions; protected transformOptions: ClassTransformOptions | undefined; protected errorHttpStatusCode: ErrorHttpStatusCode; protected expectedType: Type<any> | undefined; protected exceptionFactory: (errors: ValidationError[]) => any; protected validateCustomDecorators: boolean; protected errorFormat: ValidationErrorFormat; protected maxErrorMessagesLength: number; constructor(options?: ValidationPipeOptions); protected loadValidator(validatorPackage?: ValidatorPackage): ValidatorPackage | Promise<ValidatorPackage>; protected loadTransformer(transformerPackage?: TransformerPackage): TransformerPackage | Promise<TransformerPackage>; transform(value: unknown, metadata: ArgumentMetadata): Promise<any>; createExceptionFactory(): (validationErrors?: ValidationError[]) => unknown; /** * Leaves out the errors that come after the first "maxErrorMessagesLength" * characters of messages. Each message counts with the path of its * property, which is what makes the response to a deeply nested payload * grow faster than the payload. The errors past the limit are not visited. */ protected limitValidationErrors(validationErrors: ValidationError[], budget: { remaining: number; isExceeded: boolean; }, parentPathLength?: number): ValidationError[]; /** * Removes the value of the errors that have children. That value is the * object or array in which the children were found, so an exception * factory that sends the errors would write a nested value once for each * level above it. The values left are those of separate parts of the * validated value. */ protected stripParentErrorValues(validationErrors: ValidationError[]): void; protected toValidate(metadata: ArgumentMetadata): boolean; protected transformPrimitive(value: unknown, metadata: ArgumentMetadata): unknown; protected toEmptyIfNil<T = any, R = T>(value: unknown, metatype: Type<unknown> | object): R | object | string; /** * Closes the gaps of the sparse arrays in the value to validate, in place * (`[, 'a']` becomes `['a']`), so that the arrays that are validated are * the ones that are returned. */ protected closeArrayGaps(value: unknown): void; protected isPrimitive(value: unknown): boolean; protected isBinary(value: unknown): boolean; protected stripProtoKeys(value: any): void; protected validate(object: object, validatorOptions?: ValidatorOptions): Promise<ValidationError[]> | ValidationError[]; /** * Returns an error for each binary value (a Buffer, a typed array or an * ArrayBuffer) that a property validated as a nested object holds, directly * or as an item, in the shape of class-validator's error for a primitive in * that place. The properties are read from class-validator's metadata * storage, once per class: a validator package without one is not checked. */ protected findNestedBinaryValues(object: any, metadataCache?: Map<unknown, any[]>): ValidationError[]; protected toNestedBinaryError(property: string, value: unknown, message: string, metadataCache: Map<unknown, any[]>): ValidationError | undefined; protected hasValidationRules(metatype: Type<unknown>): Promise<boolean>; protected flattenValidationErrors(validationErrors: ValidationError[]): string[]; protected groupValidationErrors(validationErrors: ValidationError[], parentPath?: string, result?: Record<string, string[]>): Record<string, string[]>; protected mapChildrenToValidationErrors(error: ValidationError, parentPath?: string, validationErrors?: ValidationError[]): ValidationError[]; protected prependConstraintsWithParentProp(parentPath: string, error: ValidationError): ValidationError; }