ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.6k stars 869 forks source link

Add `validateSchema` "strict" mode #2463

Closed relu91 closed 1 week ago

relu91 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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

jasoniangreen commented 2 weeks ago

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 commented 2 weeks ago

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 commented 1 week ago

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.