Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including typescript-param-validator with all npm packages installed. Try it out:

var typescriptParamValidator = require("typescript-param-validator")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

typescript-param-validator v1.1.0

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

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) {
    
  } 
}

const instance = new TestClass();

// Will throw class-validator errors on runtime
instance.methodName({
  birthDate: new Date({
    description: '123',
    email: 'fakemail'
  })
});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free