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

Import schema element using URI reference #200

Open
2 tasks done
nbuffon opened this issue Oct 31, 2024 · 1 comment
Open
2 tasks done

Import schema element using URI reference #200

nbuffon opened this issue Oct 31, 2024 · 1 comment
Assignees
Labels
Schema JSON schema files
Milestone

Comments

@nbuffon
Copy link
Member

nbuffon commented Oct 31, 2024

It is possible to reference schema definitions using URI but questions are:

  • does URI includes local path ? (e.g. ../common/position.json)
  • is is possible to reference a "subset" in a schema ?
@nbuffon nbuffon added the Schema JSON schema files label Oct 31, 2024
@nbuffon nbuffon added this to the Sprint 4 milestone Oct 31, 2024
@nbuffon
Copy link
Member Author

nbuffon commented Nov 5, 2024

Et sinon pour nos histoires de ref inter schéma JSON:

You can create a JSON Schema file that defines objects (like a library of reusable definitions), and then reference those definitions from other JSON Schema files. This is commonly done using the $ref keyword to refer to definitions within the same file or across different files.

Here's how it can be structured:

  1. Primary Schema File (definitions.json): This file contains the shared object definitions.

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "definitions": {
        "Person": {
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "age": { "type": "integer" }
          },
          "required": ["name", "age"]
        }
      }
    }
  2. Other Schema File (schema_using_definitions.json): This file references the definitions from definitions.json.

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "author": { "$ref": "definitions.json#/definitions/Person" },
        "title": { "type": "string" }
      },
      "required": ["author", "title"]
    }

Referencing Details

  • The $ref value "definitions.json#/definitions/Person" points to the Person definition in the definitions.json file.
  • Make sure that both files are accessible from wherever you're running the schema validation tool (or server).
  • You can use absolute URLs if these files are hosted somewhere or use relative paths if they are colocated in the same directory.

This approach provides a modular setup where each schema can leverage shared definitions, making it more manageable and consistent across multiple schema files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Schema JSON schema files
Projects
Status: Open
Development

No branches or pull requests

3 participants