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

Is there any way to reuse compiled schema #296

Closed
nomi-ramzan opened this issue Sep 6, 2016 · 5 comments
Closed

Is there any way to reuse compiled schema #296

nomi-ramzan opened this issue Sep 6, 2016 · 5 comments
Labels

Comments

@nomi-ramzan
Copy link

nomi-ramzan commented Sep 6, 2016

Hi
I am using AJV(latest) with express. here is current code

app.get('test', function (req, res, next) {
    var validate = ajv.compile(schema);
    if( !validate(req.body) ) return sendError(validate.errors)
    next();
})

Is there any way to do this. Like just compile it once and use clone/copy of it on each call.

var validate = ajv.compile(schema);

app.get('test', function (req, res, next) {
    if( !validate(req.body) ) return sendError(validate.errors)
    next();
})

I also tried clone, cloneDeep of lodash. But now working.
Thank you.

@epoberezkin
Copy link
Member

You should use your second code sample - that's exactly how it should be.

@nomi-ramzan
Copy link
Author

I tried this code. It will overwrite codes in case of multi calls at the same time. Let suppose two calls came and before sending errors of first call second call will overwrite errors then first call will send errors of second call instead of first call ( which i have lost now).

@nomi-ramzan
Copy link
Author

Can you please help me how can I clone deeply validate object ?

@epoberezkin
Copy link
Member

epoberezkin commented Sep 7, 2016

That's not correct. Validation is synchronous, JavaScript is single threaded. It could have only happened if you use the errors in the next execution block (in callback, nextTick, setTimeout etc.). Please see FAQ and #242 (comment)

So you don't need to clone, you just need to use your second sample, it doesn't have the problem you describe.

@nomi-ramzan
Copy link
Author

Thank you I got it.

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

No branches or pull requests

2 participants