Add more functionality to external validators#2773
Add more functionality to external validators#2773asologor wants to merge 1 commit intohapijs:masterfrom
Conversation
|
Thanks, this is great, I just wanted to implement this too. |
|
Could this add (also part of
|
|
This is a great improvement over the current .external() implementation. @jonyeezs I think the current So I'd propose to rename the
|
|
Is there any real need for multiple calls to error? I feel like this breaks away from the convention in every other place where it's allowed (extensions and custom). |
With the current (main branch) version You may change this this behavior to mimic something like |
66367f5 to
babffbe
Compare
It might be handy sometimes. A few example cases:
Even if Currently, I don't have time to work on this PR. Maintainers are free to make any changes though. |
|
Hi! Someone know when will this PR will be merge? I need this fix ^^' |
|
First, I'd like to thank @asologor a lot for the efforts put into this PR, it helped me kick off this feature. I have implemented it in #2931, this is released as of 17.9.0. For now, |
|
I would love to see |
|
The ability to validate all parts of a schema, including properties that rely on externals, at the same time would be hugely impactful to my project. Please consider adopting this feature. Thanks! |
No breaking changes here. No existing tests were modified.
I know that many people want to see these features in Joi and I think this functionality is a must-have for any backend validator. If you do not merge this PR, consider implementing the same or similar interface yourself.
Added a bunch of helpers for externals:
prefs- this one existed before.path- ordered array where each element is the accessor to the value where the error happened.label- label of the value. If you are validating an object's property, it will contain the name of that property.root- the root object or primitive value under validation.context- same asroot, but contains only the closest parent object in case of nested objects validation.error- a function with signaturefunction(message). You can use it in a return statement (return error('Oops!')) or you can call it multiple times if you want to push more than one error message in a single external validator.Added a new setting
alwaysExecuteExternals(default isfalse) which in pair withabortEarly: falsewill make externals execute even after some synchronous validator failed. Chains of external validators will abort early anyway (e.g.Joi.any().external(...).external(...)- second external won't execute if the first one failed).Now externals throw the same ValidationError instance as all other validators.
A basic example from the
API.mddoc:See
API.mdchanges and test suites for more details.