JSON Schema: Using $ref to an internal $id is flagged as an error
See original GitHub issueDescribe the bug
When linting a self-contained JSON schema, using a $ref
to refer to an internal sub-schema by $id
is reported as an error.
To Reproduce
- Grab the “Using $id with $ref” example from Understanding JSON Schema
- Run
spectral lint -F hint -v schema.json
Expected behavior Should be a valid schema
Actual behavior
Found 52 rules (36 enabled)
Linting <file>
JSON Schema detected
JSON Schema (loose) detected
JSON Schema Draft 7 detected
<file>
28:15 error invalid-ref ENOENT: no such file or directory, open ''
✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)
Environment:
- Library version: 4.2.0 (I can’t test with latest due to #1097)
- OS: Mac OS Mokave
- Node: v13.13.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
JSON schema definitions validation for internal reference
I am creating thousands of definitions and making it available on remote so that any one can reuse the defined schema by referring...
Read more >Structuring a complex schema — Understanding JSON ...
A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's...
Read more >JSON Schema Serializer and Deserializer
json.schema.spec.version Indicates the specification version to use for JSON schemas derived from objects. Valid values are one of the following strings: ...
Read more >API Reference | Ajv JSON schema validator
This function has properties errors and schema . Errors encountered during the last validation are assigned to errors property (it is assigned null...
Read more >JSON Schema: A Media Type for Describing JSON Documents
JSON Schema uses keywords to assert constraints on JSON instances or annotate ... The "$id", "$ref", and "$dynamicRef" core keywords, and the "base"...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Spectral does not support resolving based on
$id
- it’s the property that’s simply unrecognized by us. If you want to reference a place in the document, you need to use a valid JSON Pointer, such as#/definitions/address
, as explained in the extending section of the article you linked.Spectral is technically not a JSON Schema validator, therefore we don’t support all of the resolving features that JSON Schema provides. I’m not sure whether I was helpful or not, but please do feel free to ask further questions if something is uncertain.
Context: Seeing as Spectral is a tool which aims to work with OpenAPI and JSON Schema, but those two formats are only mostly compatible, there are a few quirks like this which pop up.
OpenAPI does not allow $id, so resolving via $id is commonly not supported in tooling which works with OpenAPI, even though a lot of that tooling says it supports JSON Schema too. I know. This is a mess.
Seeing as OpenAPI v3.1 will be proper JSON Schema, $id will be allowed, and all JSON Ref tooling is now scrambling to add proper support. We’re ditching our own json-ref-resolver to help json-schema-ref-parser get better, and they’re adding $id support, so soon this problem will have gone away.
tldr: for now please try and avoid $ref’ing to $id and point to its location in the file using a more standard JSON Pointer instead.