Skip to content

Commit

Permalink
feat(validation): Implement update to class-validator to handle async…
Browse files Browse the repository at this point in the history
… validators.
  • Loading branch information
zakhenry committed Jun 30, 2016
1 parent c61a879 commit b971a4a
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 8,075 deletions.
27 changes: 24 additions & 3 deletions docs/guide/validation.md
Expand Up @@ -75,11 +75,11 @@ 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';
import { ValidatorConstraint, ValidatorConstraintInterface } from '@ubiquits/core/validation';

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

constructor(@Optional private server: Server) {
}
Expand All @@ -99,7 +99,28 @@ everything from the client side before sending data to the server. This makes fo
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).
Any custom validator can return a `Promise<boolean>` for asynchronous validation. Combined with dependency injection,
custom validators can perform complex checks on a model.

Example:
```typescript
import { Injectable, Optional } from '@angular/core';
import { UserStore } from '../path/to/stores';
import { ValidatorConstraint, ValidatorConstraintInterface } from '@ubiquits/core/validation';

@Injectable()
@ValidatorConstraint()
class UsernameExistsValidator implements ValidatorConstraintInterface {

constructor(private userStore: UserStore) {
}

public validate(value: any): Promise<boolean> {
return this.userStore.verifyUsernameExists(value);
}

}
```

[@pleerock]: https://github.com/pleerock
[class-validator]: https://github.com/pleerock/class-validator
Expand Down

0 comments on commit b971a4a

Please sign in to comment.