Skip to content

unlight/class-validator-flat-formatter

Repository files navigation

class-validator-flat-formatter

Convert array of ValidationError objects from class-validator to multiline string

Install

npm install class-validator-flat-formatter

Usage

As string

import { validationError } from 'class-validator-flat-formatter'; const errors = await validate(user); const message = validationError(errors, options); // message => name: should not be empty (isNotEmpty), email: must be an email (isEmail)
Options
export interface ValidationErrorOptions { /**  * Custom template, tokens:  * {propertyPath} - Full dotted property path (e.g user.email)  * {property} - Last piece of {propertyPath} (e.g. email)  * {constraintRule} - Constraint rule id (e.g. isEmail)  * {constraintMessage} - Constraint message (e.g. must be an email)  */ template?: string; /**  * Delimiter of joined several validation messages.  * Default: Comma and space (CS)  */ delimiter?: Delimiter | string; /**  * Period at the end of string.  * Default: false  */ period?: boolean; }

As array

import { validationErrorsAsArray } from 'class-validator-flat-formatter'; const errors = await validate(user); const messages = validationErrorsAsArray(errors); /**  messages => Array<string> {  'name: should not be empty (isNotEmpty)',  'email: must be an email (isEmail)' } */

validationErrorsByProperty

Group validation errors by property

function validationErrorsByProperty( errors: ValidationError[] | ValidationError, template: string = '{constraintMessage} ({constraintRule})', delimiter: string = '\n', ) /** age: should not be null or undefined (isDefined), must not be less than 18 (min) email.value: should not be empty (isNotEmpty), must be an email (isEmail)

License

MIT License (c) 2025

About

Convert array of ValidationError objects from class-validator to multiline string

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •