question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Is there a way to supply multiple schemas for compilation?

See original GitHub issue

I have a set of growing and somewhat redundant schemas that I’d like to break down into smaller “main” schemas with shareable “subschemas” using the $ref keyword. The schemas needed by the application would all be known at initialization time; I have no need for dynamic loading of schemas.

With my current (large) schemas, I call the compile function once at initialization/module load time as suggested here.

    const ajv = new Ajv({ allErrors: true })
    const validate = ajv.compile(myBigOriginalSchema1) 

After reading the documentation and the API reference I’m unclear (I’m sorry) on whether there is a way to have ajv compile multiple schemas at once (or whether that is even advisable). I notice the compile function only takes a single schema.

For example, I tried this, with addSchema:

    const ajv = new Ajv({ allErrors: true })
    ajv.addSchema(mySubschema1)
    ajv.addSchema(mySubschema2)
    const validate = ajv.compile(mySmallerMainSchema1) 

…and I looking at the source, I don’t believe it compiles the other schemas added via addSchema at the time of the compile call.

I also read here that the addSchema function itself doesn’t actually compile the schemas by design, until they are first needed. In my use case I KNOW these subschemas will very likely used on the first validation attempt. Breaking the large single schema down into a bunch (e.g. a dozen) subschemas will likely mean I am deferring most/all the compilation to the first validation attempt instead of compiling at initialization. Is my understanding correct? Is what I have above the most efficient approach? Is there a way to compile multiple schemas at initialization time instead?

I also experimented with the following, but I’m not sure it’s any different…I thought the constructor simply called addSchema on each schema given:

    const ajv = new Ajv({ allErrors: true, schemas: [mySubschema1, mySubschema2] })
    const validate = ajv.compile(mySmallerMainSchema1) 

Just to further try and illustrate what I’m asking about, I almost want to do something like this, but the compile function needs a schema argument:

    const ajv = new Ajv({ allErrors: true, schemas: [mySmallerMainSchema1, mySubschema1, mySubschema2] })
    const validate = ajv.compile() //needs a schema here, obviously

…OR like this, but I know this argument isn’t supported either:

    const ajv = new Ajv({ allErrors: true })
    const validate = ajv.compile({ schemas: [mySmallerMainSchema1, mySubschema1, mySubschema2] })  //only one allowed

Any advice is appreciated. And if I am missing the point of how subschemas and $ref are supposed to be used, please let me know.

Thank you for creating & maintaining a useful library in ajv.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wcaneiracommented, Jul 12, 2021

I have a sample working with addSchema/getSchema, so I can close this issue, but before I do, one related follow-up, please - are code functions appropriate for database/HTTP service call validation? I see your range example but I feel like that’s a schema-based validation rule with no external dependencies. I’m struggling to see how my use case would be a good fit with a function that ends with ctx.fail or ctx.pass and string templates. Is a validate function (what I’m currently using) more appropriate for database/HTTP service-based validation?

This link was broken so I couldn’t see the additional examples image

I assume it leads to the ajv-keyword examples you mentioned earlier, which I did look over, but I only see a couple examples there, and I’m still unclear. I’m sorry.

I’ll close this issue after receiving your reply.

0reactions
epoberezkincommented, Jul 24, 2021

This is the folder where you can see the examples: https://github.com/ajv-validator/ajv/tree/master/lib/vocabularies/validation

are code functions appropriate for database/HTTP service call validation

they are used for all standard keywords, but they are only used during schema compilation. HTTP service call validation is the main use case, it’s all ok.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiling multiple schemas into different packages using ...
Compiling multiple schemas into different packages using JAXB 2.1 - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational ...
Read more >
compile multiple schemas in parallel - Oracle Communities
Yes. If it does not work for you and thre are no dependent objects between the schemas then open an SR with Oracle....
Read more >
Multiple Schema - an overview | ScienceDirect Topics
Multiple schemas are grouped into catalogs, which can then be grouped into clusters. A catalog usually contains information describing all the schemas handled ......
Read more >
Deploy an Access application - Microsoft Support
One way to separate data and logic is by using the Access Database command (on the Database Tools tab, in the Move Data...
Read more >
Use with CSP & Unsafe-Eval #406 - ajv-validator/ajv - GitHub
I see that AJV uses eval in the source to compile the Schema object. ... Is there a recommended method for using AJV...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found