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.

JSON Schema: Using $ref to an internal $id is flagged as an error

See original GitHub issue

Describe 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

  1. Grab the “Using $id with $ref” example from Understanding JSON Schema
  2. 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
P0lipcommented, May 3, 2020

Would you mind explaining briefly why it’s not a valid pointer?

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.

Any ideas why it’s documented in this popular online guide and it works in 2 schema parsers I’m using when it’s not valid? Just trying to understand the landscape.

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.

0reactions
philsturgeoncommented, May 4, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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