validate icon indicating copy to clipboard operation
validate copied to clipboard

Is it possible to write async custom validators?

Open kevinrood opened this issue 6 years ago • 3 comments

First of all, thanks for making validate available 😄

I could not determine from the readme whether it is possible to write async custom validators (a validator which relies on looking up a database record, for example). Is that possible?

kevinrood avatar Apr 07 '18 16:04 kevinrood

No, not currently. For my use case it didn't make much sense because of concurrency issues etc. Do you have a specific use case in mind?

eivindfjeldstad avatar Apr 07 '18 22:04 eivindfjeldstad

It is fairly common in web apps I build to include database queries as a part of some validation rules.

Additionally, separate from async validation, object-level validation (validating a few fields together) is a common requirement.

kevinrood avatar Apr 07 '18 23:04 kevinrood

Yeah, I do the same thing, but I like to delegate that stuff to the db layer to avoid race conditions.

You can validate fields together (sort of) by doing something like this:

const something = (val, ctx) => val > ctx.some.other.path;
schema.path('some.path').use({ something })

eivindfjeldstad avatar Apr 07 '18 23:04 eivindfjeldstad