-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gain access to the current hook object inside the validateSchema async validator #209
Comments
@beeplin, does ajv have any dynamic features that would do this, or allow us to do this? |
All I found was this:
Can be done this way, but imho using |
In theory, this would require that the |
https://github.com/epoberezkin/ajv#asynchronous-validation However, since we are already using feathers, I doubt whether it is a good idea to put everything into Ajv whenever possible. Ajv and feathers have some shared functions, but since Ajv is a static JSON validating tool, it always gets more complicated (and maybe awkward/hacking) to achieve same things via Ajv compared with using feathers hooks. My opinion is to let Ajv do those things it is originally designed for (validating data structure, etc.), and let hooks do complicated and async tasks. |
That sounds very reasoned to me, @beeplin. @nikolakanacki, does this make sense to you? |
@beeplin I see where you're coming from, but I fail to see a corelation between "gaining access to the hook" and "hacky / awkward / more complicated". The use case I've put as an example is a bit more "business-logic-domain" kind of an issue which should probably live in the custom hook either way, I agree with you on that one, but I'm pretty sure it is not the only use case when you would like an acces to the hook inside a validator. Async validation is well documented in Ajv, I do not think we shoud decide what features to support / encaurage, and to what extent. What would be your way of passing in a hook with the current Ajv API? I'm failing to see one (except having a wrapper function that has access to the hook through context or using |
I think this use case can be handled with a small custom hook: hook => {
const jsonSchema = { ... }; // includes use of `hook` properties
return validateSchema(jsonSchema, ajv)(hook);
}; @nikolakanacki, feel free to reopen the issue if you want to. |
@eddyystop how to use the above context in the validate function ? edit: never mind |
Any way we could somehow add access to the
hook
object inside the async validation callback? Wouldn't mind exploring this myself but wanted to hear the opinions first.Example use case: Allow field value based on current field value (could be accomplished with a call to
hook.service.get
and inspecting the resolved document). I know this could technically be implemented as a standalone hook somewhere before / after thevalidateSchema
hook, but this is definitely more convenient.The text was updated successfully, but these errors were encountered: