Async validators & model passing
See original GitHub issueCurrently the signature for validator interface is
export interface ValidatorInterface {
validate(value: any): boolean;
}
This is fine for trivial validation, but more complex validation needs to be able to access the whole object, and also return promised booleans for async.
Proposed signature
export interface ValidatorInterface {
validate(value: any, meta:ValidationMetadata): boolean | Promise<boolean>;
}
This will allow for
- context-aware validators like
@IsLongerThan('propertyName')
,@RequiredIf('propertyName', 'value')
- async validators like
@UsernameAvailable
Notes:
- If
skipMissingProperties == true
then a context aware validator will fail. The passedValidationMetadata
should pass theskipMissingProperties
value through so that the validator can act appropriately. - Allowing promised validation will require a significant refactor to the Validator.validate method
- It’s probably best to refactor everything to be promise based - wrapping all calls with
Promise.resolve()
will allow both promised and basic validators to run - validateOrThrow would need a
timeout(() => throw new ValidationError())
in the promise chain so the thrown errors escape. (for backwards compatibility). - validate would need a call signature of
validate(object: any, validatorOptions?: ValidatorOptions): Promise<ValidationErrorInterface[]>
. This would have to be a breaking change.
- It’s probably best to refactor everything to be promise based - wrapping all calls with
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Using Custom Async Validators in Angular Reactive Forms
In Angular, you can do this using Async Validators. ... calling the createValidator method, and passing a reference to the UserService .
Read more >Angular: Custom Async Validators - Medium
It essentially checks to see if the value passed in can be found in our 'Database' of valid zip codes. We can now...
Read more >Async Validators | JET Developer Cookbook - Oracle
The async validator's validate method returns a Promise that resolves if validation passes and value is updated or rejects with an Error if...
Read more >Creating Angular Synchronous and Asynchronous Validators ...
The most common use case for async Validators is doing a server validation via an HTTP Callback. I'll look at creating the sync...
Read more >How to Add Async Validators to an Angular Reactive Form
Learn how you can add async validators to your reactive form in Angular.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I like this idea, and think it should be here in class validator. However I need to change validator interface - all methods will be async then. It will be a breaking change. I was planning to completely refactor class validator a while ago, but didn’t had time for this. Now its look like time has come. There is one big issue in class validator right now - since it uses validator.js and mostly usable in string validations. This is annoying and not usable, and I’m planning to either switch validation framework, either add another one. I’ll plan to refactor it on the next week, and will notify what other changes Im going to do. Until that provide as much proposals as you think I should include in the library.
added in 0.4.0