class-validator-errors-flattener-temp
TypeScript icon, indicating that this package has built-in type declarations

2.1.3 • Public • Published

class-validator-errors-flattener

A flattener to class-validator's ValidationError array

Build status NPM version Issues License: Unlicense Downloads

Install

npm install class-validator-errors-flattener

Usage

To flatten the class-validator ValidationError array, just pass it to the flattenValidationErrors function

Example:

import { validate, MinLength, Min, IsNotEmpty, ValidateNested } from 'class-validator';
import { flattenValidationErrors } from 'class-validators-errors-flattener'

class Address {
  @IsNotEmpty()
  city = "Goiânia";

  @IsNotEmpty()
  state = "";
}

class User {
  @ValidateNested()
  address: Address = new Address();

  @Min(12)
  age = 11;
}
  
  const errors = await validate(new User(), {validationError: { target: false, value: false } });
  const flattenedErrors = flattenValidationErrors(errors);
  
  /* Flat: property as path instead of nested ValidationError objects
    [
      {
        property: 'address.state',
        constraints: { isNotEmpty: 'state should not be empty' }
      },
      {
        property: 'age',
        constraints: { min: 'age must not be less than 12' }
      }
    ]
  */

Options

The flattenValidationErrors accepts an options parameter:

export interface Options {
    delimiter?: string;
    omitErrorsMessages?: boolean;
    omitErrorsNames?: boolean;
}
  • delimiter

    • Defines the delimiter with which the path should be concatenated
    • Default: .
  • omitErrorsMessages

    • Sets whether errors messages should be omitted
    • If true, constraints property will be an array of the constraints names
    • Default: false
  • omitErrorsNames

    • Sets whether errors names should be omitted
    • If true, constraints property will be an array of the constraints messages
    • Default: false

License

Licensed under The Unlicense

Readme

Keywords

Package Sidebar

Install

npm i class-validator-errors-flattener-temp

Weekly Downloads

40

Version

2.1.3

License

Unlicense

Unpacked Size

46.8 kB

Total Files

26

Last publish

Collaborators

  • dabernathy89