-
-
Notifications
You must be signed in to change notification settings - Fork 887
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
ajv best practice with koa #142
Comments
Not sure I understand... Compile doesn't return errors, it throws exceptions if the schema is not valid. In most cases it can be tested in some build process so shouldn't happen at run time.
What is getContext? If validation is synchronous then the errors are assigned to I see no reason to instantiate ajv every time. You have to precompile all schemas once (or compile them when they are first used if there are many of them) and reuse validation functions. |
Maybe i don't understand how async works. If 2 http request share the same instance of ajv Am i wrong ? |
var validate = ajv.getSchema(ref);
var valid = validate(data);
if (!valid) {
var errors = validate.errors;
// ...
} In many cases you don't even need to reassign errors (if you are using these errors immediately, e.g. sending in the response). That's safe because javascript is single threaded and while your code is executing nothing else can be executed. Please see the note in the end of Getting started and #65. |
thanks a lot for answer ! That's the first time i use github (and git too) so i don't know if i can start it, but a short example with koa and other framework would be usefull for noob to fastly see how to use it. |
@epoberezkin what if need a custom keyword of the same name, but with a different validation function for different schemas? For example the model in MVC architecture may have its specific validation functions with the same name as in the other model. Does it make sense in this case use the new instance of ajv per request or ajv has another solution? |
There are obviously use cases when you need to use multiple instances. I don't really see why you may want to have the same keyword defined differently every time, although if you must you can always have a wrapper function that would be calling different function based on some external condition. So while creating new instance every time is a possible option, you would be destroying the benefit of compiling schema once and using it multiple times |
i wonder how i should proceed to use ajv in koa.
I made some test and it seems that :
all compile call share the same error object for the same shema and same ajv instance.
all getContext call share the same error object for the same shema and same ajv instance.
Should i use the same ajv instance for all request ?
Should i instanciate ajv for each request ? What about perf ?
Should i instanciate ajv each time i need to use it ? What about perf ?
I tried to find examples of ajv and koa working together to be sure to proceed well but found nothing on google.
The text was updated successfully, but these errors were encountered: