class-validator-message-formatter
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Class-validator-message-formatter

If you failed validation using class-validator, you'll be received messages. But it is very difficult to read. This library modifies the message to make it easier to handle.

Installation

$ npm install --save class-validator-message-formatter

Hot to use

class User {
    @IsString()
    name: string;
 
    @IsString()
    address: string;
 
    constructor(name: string, address: string) {
        this.name = name;
        this.address = address;
    }
}
 

Without class-validator-message-formatter

import { validateSync } from 'class-validator';
  
const user:User = new User();
const errors = validateSync(user);
console.log(errors);
 
 [ ValidationError {
        target: User { name: null, address: null },
        value: null,
        property: 'name',
        children: [],
        constraints: { isString: 'name must be a string' } },
    ValidationError {
        target: User { name: null, address: null },
        value: null,
        property: 'address',
        children: [],
        constraints: { isString: 'address must be a string' } } ]

with class-validator-message-formatter

import {MessageFormatter} from 'class-validator-message-formatter';
import { validateSync } from 'class-validator';
 
const user:User = new User();
const errors = validateSync(user);
console.log(MessageFormatter.format(errors));
 
{ field: 'name', message: 'name must be a string' },
  { field: 'address', message: 'address must be a string' } ]

Package Sidebar

Install

npm i class-validator-message-formatter

Weekly Downloads

437

Version

0.0.3

License

MIT

Unpacked Size

5.04 kB

Total Files

6

Last publish

Collaborators

  • loveloper