Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ref and $id implementation #1

Closed
Eomm opened this issue Oct 21, 2023 · 1 comment · Fixed by #3
Closed

$ref and $id implementation #1

Eomm opened this issue Oct 21, 2023 · 1 comment · Fixed by #3

Comments

@Eomm
Copy link
Member

Eomm commented Oct 21, 2023

From the docs, it is not clear which version of the json schema standard this library is supported.

From a quick test, I think we have few use cases that are not covered:

Therefore, "$id" MUST NOT contain a non-empty fragment, and SHOULD NOT contain an empty fragment. The absolute-URI form MUST be considered the canonical URI, regardless of the presence or absence of an empty fragment. An empty fragment is currently allowed because older meta-schemas have an empty fragment in their $id (or previously, id). A future draft may outright forbid even empty fragments in "$id".

From spec: https://json-schema.org/draft/2020-12/json-schema-core#name-the-id-keyword

This example should behave in a different way:

const assert = require('node:assert')
const { RefResolver } = require('json-schema-ref-resolver')


const inputSchema = {
  "$id": "http://example.com/root.json",
  "definitions": {
    "A": { "$id": "#foo" },
    "B": {
      "$id": "other.json",
      "definitions": {
        "X": { "$id": "#bar", type: 'string' },
        "Y": { "$id": "t/inner.json" }
      }
    },
    "C": {
      "$id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f",
      type: 'object',
    }
  }
}

const addresSchema = {
  $id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com
  type: 'object',
  properties: {
    zip: { $ref: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' },
    city: { $ref: 'http://example.com/root.json#foo' }, // ❗️ should work
// city2: { $ref: '#foo' } // wrong syntax, ❗️ should say ref not found. triggers TypeError: Cannot read properties of null (reading '$ref')  }
}


const refResolver = new RefResolver()
refResolver.addSchema(inputSchema)
refResolver.addSchema(addresSchema)

refResolver.derefSchema('relativeAddress')
const derefSourceSchema = refResolver.getDerefSchema('relativeAddress')
console.log(derefSourceSchema)

I wrote almost the same lib: https://github.com/Eomm/json-schema-resolver
It is used in fastify-swagger, and we can get the tests to have a wider suite.
The code there can be optimized if we want to use it.

@ivan-tymoshenko
Copy link
Member

From the docs, it is not clear which version of the json schema standard this library is supported.

This package supports a json schema draft 7 format, but I think it's not a problem to make it work with all drafts.

This example should behave in a different way

Thanks, here is fix #3.

I wrote almost the same lib: https://github.com/Eomm/json-schema-resolver

json-schema-resolver works in a bit different way. Correct me if I'm wrong, but it takes all external schemas puts them into the definition section, and updates all shema refs accordingly. This package resolves all $refs, which means you don't have any $ref keyword in your schema after dereferencing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants