Skip to content
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

Closed
nikolakanacki opened this issue Jun 2, 2017 · 8 comments

Comments

@nikolakanacki
Copy link

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 the validateSchema hook, but this is definitely more convenient.

@eddyystop
Copy link
Collaborator

@beeplin, does ajv have any dynamic features that would do this, or allow us to do this?

@nikolakanacki
Copy link
Author

All I found was this:

passContext: pass validation context to custom keyword functions. If this option is true and you pass some context to the compiled validation function with validate.call(context, data), the context will be available as this in your custom keywords. By default this is Ajv instance.

Can be done this way, but imho using this feels wrong.

@nikolakanacki
Copy link
Author

In theory, this would require that the validate function of ajv accepts extra arguments which would be passed to the registered callbacks for async validation. This would be the best case scenario, but it would definitely require some (non breaking) modifications to the ajv source.

@beeplin
Copy link
Contributor

beeplin commented Jun 2, 2017

https://github.com/epoberezkin/ajv#asynchronous-validation
https://github.com/epoberezkin/ajv-async
As far as I know, ajv does have APIs for async validation, shown above. Theoretically we are of course able to find a way to pass the hook object into Ajv.

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.

@eddyystop
Copy link
Collaborator

That sounds very reasoned to me, @beeplin.

@nikolakanacki, does this make sense to you?

@nikolakanacki
Copy link
Author

@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 passContext, both of which feel hacky).

@eddyystop
Copy link
Collaborator

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.

@noor-tg
Copy link

noor-tg commented Aug 16, 2020

@eddyystop how to use the above context in the validate function ?


edit: never mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants