Git Product home page Git Product logo

typescript-param-validator's Introduction

Typescript runtime param validation

A library for runtime param validations using typescript decorators that works with class-validator library.

Useful when you need to validate parameter data coming from untyped source(i.e http body, and etc...) or when you need to perform complex validations on the input params.

Using the param validation decorator you can declare your validation on the type annotation and leave the validation work to class-validator

Install

npm install --save typescript-param-validator class-validators

Examples

import { Validator, Validate } from 'typescript-param-validator';
import { IsDate, IsNotEmpty, MaxDate, IsEmail, Length } from 'class-validators';

class DataDto {
  @IsNotEmpty()
  @IsEmail()
  email: string;
  
  @Length(10, 200)
  @IsNotEmpty()
  description: string;
  
  @IsDate()
  @IsNotEmpty()
  @MaxDate(new Date())
  birthDate: Date;
}

class TestClass {
  @Validate()
  methodName(@Validator() data: DataDto) {
    
  }

  @Validate()
  serverControllerEndpoint(@Validator(DataDto, 'body') req: Request) {
    
  }
}

const instance = new TestClass();

// Will throw class-validator errors on runtime
instance.methodName({
  birthDate: new Date(),
  description: '123',
  email: 'fakemail'
});

instance.serverControllerEndpoint({
  body: {
    birthDate: new Date(),
    description: '123',
    email: 'fakemail'
  }
});

Catching the Validation errors

In order to catch the validation errors you need to check if the errors is instanceof ValidatorError;

import { ValidatorError } from 'typescript-param-validator';

try {
  // ... code
} catch (error) {
  if (error instanceof ValidatorError) {
    // here you have access to the validationErrors object that 
    // will contain array of `class-validator` error objects.
    console.log(error.validationErrors);
  }
}

typescript-param-validator's People

Contributors

scopsy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gugadev fpierre

typescript-param-validator's Issues

Tests vs Documentation

Hey There,

Your readme documentation uses class-validators but your tests uses class-validator (for the actual definition of IsEmail)

When I try to work with class-validators I get TypeError: class_validators_1.IsEmail is not a function but when I use the regular class-validator I don't get an error on bad input, so I'm a little confused.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.