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

Add validateSchema "strict" mode #2463

Closed
relu91 opened this issue Jul 2, 2024 · 6 comments
Closed

Add validateSchema "strict" mode #2463

relu91 opened this issue Jul 2, 2024 · 6 comments

Comments

@relu91
Copy link

relu91 commented Jul 2, 2024

What version of Ajv you are you using?
8.12.0

What problem do you want to solve?
We have a server-side "repository" of schemas that we lazy-compile (and cache) only when they are really used. When adding a new schema to the repository the JSON payload is validated using the validateSchema function. My assumption was that if the schema is valid then the compile function should always work. However, this is not true if you enable the strict mode (which is enabled by default).

What do you think is the correct solution to problem?
I would like to have a function (or an additional option in the validateSchema) that can assure that the compile function will return the validate function.

Will you be able to implement it?
I've looked into the inner workings of avj in the past, I might know the places to touch but I probably need some guidance.

@jasoniangreen
Copy link
Collaborator

Hi @relu91 I am having trouble getting my head around the problem you are having, it feels like I'm missing context. Can you maybe provide an example, either with runkit like this or as a repo demonstrating what currently happens along with what you would like to happen?

@relu91
Copy link
Author

relu91 commented Jul 3, 2024

Hi @jasoniangreen thanks for the quick answer! I cloned your runkit and tried to create an example of what the problem is. What I would like to have is a function that evaluates a schema and tells me that the schema can be used in the compile function. So basically:

const isSchemaValid = ajv.validateSchema(schema); // if this returns true
const validate = ajv.compile(schema); // this should not throw

@jasoniangreen
Copy link
Collaborator

Oh interesting, I shall take a deeper look and get back to you.

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented Jul 3, 2024

So I understand the issue and it is something I will try to raise with @epoberezkin.

In the meantime I wanted to understand a few more things about your use case. Why do you need to lazy compile schemas? The usual pattern is to compile and cache all schemas during server boot (when performance is not usually an issue) and I also don't understand why you can't compile when adding a new schema as once again it is a one off task and not getting repeated with every request to the server.

Also why is it a problem to just use strict: false?

Just to be clear, I'm not saying there isn't an issue to consider here with validateSchema, but just curious about your use case.

@relu91
Copy link
Author

relu91 commented Jul 4, 2024

So I understand the issue and it is something I will try to raise with @epoberezkin.

Thank you!

In the meantime I wanted to understand a few more things about your use case. Why do you need to lazy compile schemas? The usual pattern is to compile and cache all schemas during server boot (when performance is not usually an issue) and I also don't understand why you can't compile when adding a new schema as once again it is a one-off task and not getting repeated with every request to the server.

So schemas are not known ahead of time but they are supplied by users dynamically. We could compile them right away but it might be a waste of resources because they might be used somewhen later. I'm aware that this might be an "early optimization problem" but given the existence of the validateSchema function, I thought it to be a valid option still.

Also why is it a problem to just use strict: false?

Given the schemas are user-supplied I'd prefer to keep the additional checks of strict mode as described by the documentation, but it is something we can evaluate to change.

@jasoniangreen
Copy link
Collaborator

So the answer I got was that validateSchema is not supposed to guarantee that it will compile, so I assume there are other things which could stop an otherwise valid schema from compiling. So I would say the best practice is just to attempt to compile them as they are added to truly verify they are ok. It shouldn't affect performance if it is only being done one extra time on user supplied schema upload.

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

No branches or pull requests

2 participants