1.0.4 • Published 2 years ago

pipe-validation v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Validation Pipe

If you are familiar with Nest JS, you know what validation pipes are. It's a way of validating requests using class decleration and decorators. This package is heavily influenced by Nestjs pipe validation. The aim is to make the validation module modular and useable in non-nestjs projects.

This package makes use of class-validator and its decorators for validation.

Usage

Here, we'll focus on real world use cases of the validation pipe.

Installation

    npm i validation-pipe

Basic Usage

    import { IsString, MaxLength, IsBoolean, IsOptional, validationPipe } from 'validation-pipe';

    class User {
        id: number;

        @IsString()
        firstName: string;

        @IsString()
        @MaxLength(4)
        lastName: string;

        @IsBoolean()
        @IsOptional()
        verified: boolean;
    }

    const fromPlainUser = {
        unkownProp: 'hello there',
        firstName: 'another one',
        lastName: 'Khudoiberdiev',
        verified: 'another one'
    };

    validationPipe(User, fromPlainUser, { pretty: false })
    .then((err) => {
        console.log(err);
  })