2.1.3 • Published 1 year ago

class-validator-errors-flattener-temp v2.1.3

Weekly downloads
-
License
Unlicense
Repository
github
Last release
1 year ago

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

2.1.3

1 year ago