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.

how to compile/validate group of json-schema files that might reference each other in v4+

See original GitHub issue

Scenario: Directory with several JSON-schema files, some of which might $reference the others. Each schema file defines a unique $id property. I want to compile/validate them all at once

This command worked in ajv-cli v3.x: ajv -s 'src/models/*.json' -r 'src/models/*.json'

But with 4.x and 5.0, I get errors like this: error: schema with key or id "https://mycompany.com/this/schema/model.json" already exists

But the only file that has that id is the file itself in that directory. It would seem that the -r flag is not excluding the file’s self when checking for duplicate ids in that referenced list.

How can I accomplish validating a group of schemas like this with the newer version?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
xmedekocommented, Oct 4, 2021

I agree, the -r should omit file specified by the -s flag.

My scenario is

ajv -s 'src/models/root.json' -r 'src/models/*.json' -d 'data.json'

and got the error schema with key or id ...already exists. I have to specify -r multiple times, which is uncomfortable.

Same problem with ajv compile.

2reactions
vmaurincommented, Dec 6, 2021

I am not sure the approach to filter out file will be the proper one. With glob, there is an order from which files are processed, thus depending on the order, depending schema might not be there yet. A more reliable fix would be to load everything with -r and then, on the -s files/schemas check if they are loaded or not yet

Read more comments on GitHub >

github_iconTop Results From Across the Web

Structuring a complex schema — Understanding JSON ...
You can set the base URI by using the $id keyword at the root of the schema. The value of $id is a...
Read more >
Combining schemas - Ajv JSON schema validator
Combining schemas with $ref. You can structure your validation logic across multiple schema files and have schemas reference each other using $ref keyword....
Read more >
How to Validate Your JSON Using JSON Schema
JSON Schema is a JSON-based format for defining the structure of JSON data. It provides a contract for what JSON data is required...
Read more >
Tiny Validator for v4 JSON Schema
If schemas are referenced ( $ref ) but not known, then validation will return true and the missing schema(s) will be listed in...
Read more >
How to manage multiple JSON schema files? - Stack Overflow
In JSON Schemas, you can either put a schema per file and then access them using their URL (where you stored them), or...
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