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

1.2.1 • Public • Published

Class Validator Extension

github

https://github.com/MarkMatute/class-validator-ext

why do we have this library?

extension of class-validator

class validator returns errors like this

[{
    target: /* post object */,
    property: "title",
    value: "Hello",
    constraints: {
        length: "$property must be longer than or equal to 10 characters"
    }
}, {
    target: /* post object */,
    property: "text",
    value: "this is a great post about hell world",
    constraints: {
        contains: "text must contain a hello string"
    }
}]

which is hard to use, this library simplifies it to be a key:value pair of errors

Installation

yarn add class-validator-extension

Usage

import { IsNotEmpty } from 'class-validator';
import * as classValidatorExt from 'class-validator-ext';

class User {
  @IsNotEmpty()
  @IsEmail()
  email: string;

  @IsNotEmpty()
  password: string;
}

(async () => {
  const user = new User();
  const errors = await classValidatorExt.validateAndExtract(user);
  console.log(errors);
})();

This will return key: value errors

Invalid

   {
        isValid: false,
        errors: {
            "email": "email should not be empty",
            "password": "password should not be empty"
        }
   }

Valid

   {
        isValid: true,
        errors: { }
   }

Author

Mark Ernest R. Matute

License

[MIT License]

Package Sidebar

Install

npm i class-validator-ext

Weekly Downloads

6

Version

1.2.1

License

ISC

Unpacked Size

45.9 kB

Total Files

20

Last publish

Collaborators

  • markmatute