Skip to content

Commit

Permalink
feat(validation):Add documentation for validation
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
zakhenry committed Jun 22, 2016
1 parent 93dbaa2 commit 3740823
Showing 1 changed file with 97 additions and 2 deletions.
99 changes: 97 additions & 2 deletions docs/guide/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,102 @@ date: 2016-06-09
collection: guide
collectionSort: 1
layout: guide.hbs
pendingTask: true
---

Testing infrastructure has been implemented, just need to document how to use it
## Overview

Validation is provided by [class-validator] (built by [Umed Khudoiberdiev][@pleerock]) validation works on both browser
side and server side, and is invoked with the `Store.validate` method.

## Usage
You can use any of the methods in [class-validator], but make sure to import the methods from `@ubiquits/core/validation`.
This is for two reasons:
1. You are importing from a location common to the core and your modules. This allows them to use the same `MetadataStorage` singleton
and custom functions register to the same store.
2. Any custom methods defined in `@ubiquits/core` will be available from the same source.

If you don't do this - property validation **will not register** and validation will pass on any field without a validator
listed, so don't forget!

## Custom validators
As described in [the class-validator docs][class-validator-custom] you can register custom validators.

#### `@Injectable` validator classes
The core has configured
the validator to use the current `Injector`, so your class validators can inject dependencies too.

For example:
```typescript
import { Injectable } from '@angular/core';
import { Model } from '../models/model';
import { Validate, ValidatorConstraint, ValidatorInterface } from '@ubiquits/core/validation';

@Injectable()
class TruthyService {

public isTruthy(value: any): boolean {
return !!value;
}

}

@Injectable()
@ValidatorConstraint()
class CustomTruthyValidator implements ValidatorInterface {

constructor(private validationService: TruthyService) {
}

public validate(value: any): boolean {

return this.validationService.isTruthy(value);
}

}

class Thing extends Model {

@Validate(CustomTruthyValidator)
public truthyValue: any;

}
```
In this *(overly complex)* example the `Thing` model has a custom validator `CustomTruthyValidator` assigned to it.
This validator implements the `ValidatorInterface` class-validator needs, which uses the injected `TruthyService`.

### Optional dependencies
Sometimes you will want to register a custom validator that uses a dependency that is not available in all environments.
If this is the case, make sure to decorate the injected paramater with `@Optional` so that the dependency injector know
it can fail quietly if there is no registered injectable class.
Make sure to return true in the `validate` function if the injector is not available:
```typescript
import { Injectable, Optional } from '@angular/core';
import { ValidatorConstraint, ValidatorInterface } from '@ubiquits/core/validation';

@Injectable()
@ValidatorConstraint()
class RequiresServerValidator implements ValidatorInterface {

constructor(@Optional private server: Server) {
}

public validate(value: any): boolean {
if (!this.server){
return true;
}
return this.server.checkSomething(value);
}

}
```

It is recommended that you structure you application to avoid this however, as in general, you should be able to validate
everything from the client side before sending data to the server. This makes for a significantly better user experience,
as forms can show problems before they are submitted, not after.

## Async validators
Currently async validators are not implemented. [This feature is planned](https://github.com/ubiquits/ubiquits/issues/86).

[@pleerock]: https://github.com/pleerock
[class-validator]: https://github.com/pleerock/class-validator
[class-validator-custom]: https://github.com/pleerock/class-validator#custom-validation-classes

0 comments on commit 3740823

Please sign in to comment.