@t2ee/validation

A form input validation tool set

Introducation

This library fully uses the advantages of decorators, make it smooth to write validation rules. This projects aims to be foundation component for the rest of @t2ee projects. It provides dependency injection and auto configuration functionalities.

Installation

npm i reflect-metadata @t2ee/core @t2ee/validation -S

Usage

Example usage with @t2ee/vader

class Message {
    @NotNull
    @Min(4)
    message: string;
}

@Path('/')
@Component
class Controller {

    @POST
    @Path('/say')
    say(@Valid @Body message: Message) {
        // message should be at least 4 characters long
    }
}

Example Custom Rule

const IsEmail = CustomRule('IsEmail', 'should be valid email');
@ValidationRule('IsEmail')
class IsEmailRule implements Rule {
    validate(value: any, parameter: any, meta, AutoWireMeta, args: any[]): boolean {
        return /[\d\w]*\@[\d\w]*\.[\w]*/.test(value);
    }
}

OR

const IsEmail = Pattern(/[\d\w]*\@[\d\w]*\.[\w]*/);

API

@Valid

@Valid is an essential decorator, it validates all the rules associated with a type. Without @Valid, validations will not be triggered.

@CustomRule

CustomRule(name: string, error: string, parameter?: any) , returns a PropertyDecorator. This binds necessary metadata to the class

@ValidationRule and Rule

@ValidationRule(name: string) inject your implementation of a rule to the ValidationProvider, where your rules are resolved, parsed and validated.

Rule is a interface guides you how should it be implmeented for a ValidationRule.

Rules

@Length, @Max and @Min

dataType description
null or undefined not checked
String checks the length of the string
Number checks Number.toString() length
Array checks array length
Object checks keys’ length

@NotNull

Any value shuold pass validation if the value is not null nor undefined.

@Pattern

It only validates the regex when value is string and not null nor undefined.