<p align="center">
    <a href="http://t2ee.org">
        <img width="200" src="http://t2ee.org/img/logos/t2ee.png">
    </a>
</p>
<p align="center">
    <a href="http://validation.t2ee.org">
        <img width="200" src="http://t2ee.org/img/logos/validation.png">
    </a>
</p>

<p align="center">
    <a href="https://travis-ci.org/t2ee/validation">
        <img src="https://img.shields.io/travis/t2ee/validation/master.svg?style=flat-square">
    </a>
    <a href="https://coveralls.io/r/t2ee/validation?branch=master">
        <img src="https://img.shields.io/coveralls/t2ee/validation/master.svg?style=flat-square">
    </a>
</p>

# 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](https://github.com/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](https://github.com/t2ee/vader)

```typescript
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

```typescript
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__


```typescript
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`.
