You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often need to validate a field based on some information outside of any of the fields being validated. That's what I mean by "contextual" data in this issue's title. For instance, in a web app that manages books, you may want the books to have a unique name. So to validate the name of one book, the validation method needs all books to know if there is a duplicate.
To do this, would you recommend putting this "extra" data in the object to validated, and then not adding any validation on that key within combineValidators?
To give an example, using the dog example from the docs:
constdogValidator=combineValidators({name: composeValidators(isAlphabetic,createValidator(message=>(value,allValues)=>{if(allValues.dogNames.indexOf(value)!==-1){returnmessage;}},field=>`${field} must be unique`))('Name'),age: isNumeric('Age')});constresult=dogValidator({name: 'larry',age: 'abc',dogNames: ['larry','kathryn']});
Do you think this is fine, or do you have another suggestion on how best to do this? Thanks!
At first, I tried to do...
createValidator(message=>(value,allValues,dogNames)=>{if(dogNames.indexOf(value)!==-1){returnmessage;}},field=>`${field} must be unique`)constresult=dogValidator({name: 'larry',age: 'abc'},['larry','kathryn']);
but that didn't work :)
The text was updated successfully, but these errors were encountered:
I think your suggested workaround is fine, albeit a little cumbersome from an API standpoint. I do think that there could be value in passing contextual data as well. Ideally, it would easy to use manually like your example and also work well with redux-form.
I've been a little busy finishing up some features for another OSS project, but I hope to come back and work on revalidate some more soon. If you have any ideas, I'd love to see them and maybe a PR too :)
👋 @jfairbank !
I often need to validate a field based on some information outside of any of the fields being validated. That's what I mean by "contextual" data in this issue's title. For instance, in a web app that manages books, you may want the books to have a unique name. So to validate the name of one book, the validation method needs all books to know if there is a duplicate.
To do this, would you recommend putting this "extra" data in the object to validated, and then not adding any validation on that key within
combineValidators
?To give an example, using the dog example from the docs:
Do you think this is fine, or do you have another suggestion on how best to do this? Thanks!
At first, I tried to do...
but that didn't work :)
The text was updated successfully, but these errors were encountered: