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

[Help] fastify.validatorCompiler is null #374

Closed
giovanni-bertoncelli opened this issue Feb 18, 2021 · 5 comments
Closed

[Help] fastify.validatorCompiler is null #374

giovanni-bertoncelli opened this issue Feb 18, 2021 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@giovanni-bertoncelli
Copy link

💬 Question here

Is there a way to know exactly when fastify.validatorCompiler is not null and is ready? Since it seems not to be added through the decorate API and I have to use it internally for validation in a not-covered scenario sometimes I get the ajv instance, sometimes null... If there is no way how can I rely on the internal AJV instance of Fastify?

@giovanni-bertoncelli giovanni-bertoncelli added the help wanted Extra attention is needed label Feb 18, 2021
@giovanni-bertoncelli giovanni-bertoncelli changed the title [Help] fastify.validatorCompiler [Help] fastify.validatorCompiler is null Feb 18, 2021
@jsumners
Copy link
Member

Without an example to look at, all I can suggest is to explore the new https://www.fastify.io/docs/latest/Server/#schemacontroller option.

@giovanni-bertoncelli
Copy link
Author

Mmm I can try to write a snippet:

// myplugin.js
function plugin(fastify, opts, done) {
  fastify.decorate('customValidator', (objectToValidate, schema) => {
     const { validatorCompiler } = fastify;
     // there is no way to know if fastify.validatorCompiler is not null....
     const validator = validatorCompiler({
       schema
     });
    return validator(objectToValidate);
  })
}

@jsumners
Copy link
Member

Your plugin would need to be registered after fastify.setValidatorCompiler(aCompiler). I cannot assume you are doing that without a full minimal example. fastify/fastify#2446 is a good example of showing the full context of an issue and keeping it narrow.

@Eomm
Copy link
Member

Eomm commented Feb 22, 2021

The compilers are ready, after the ready event.

Here there are the tests that cover this behaviour:

https://github.com/fastify/fastify/blob/d1972a6ba6f06bd462f33dfd7a338eed969b51e5/test/schema-feature.test.js#L1074-L1077

This is needed because the ajv instance is built async and when needed and it is not created by default

@giovanni-bertoncelli
Copy link
Author

Thank you @Eomm. I figured that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants