question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async validators & model passing

See original GitHub issue

Currently 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 passed ValidationMetadata should pass the skipMissingProperties 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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pleerockcommented, Jun 22, 2016

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.

0reactions
pleerockcommented, Jul 6, 2016

added in 0.4.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found