Skip to content

Override $ref in external reference #1076

@unional

Description

@unional

When I'm writing some json schema and try to organize them, I notice current we cannot override the reference of external references, resulting limitation in extending the schema.

For example, I have define this little functional programming language just-func: https://github.com/justland/just-func-json/blob/main/json-schema/just-func.jsonc

In the schema, one of the definition looks like this:

// just-func.jsonc
    "JustUnaryFunctions": {
      "type": "array",
      "items": [
        {
          "type": "string",
          "enum": ["not","ret"],
          "description": "Basic unary functions:\nnot: logical not\nret: retrieve value defined by `let`"
        },
        {
          "$ref": "#/definitions/JustFunc"
        }
      ],
      "additionalItems": false
    }

Notice I have a "$ref": "#/definitions/JustFunc" here.

When using this language, I would extend it by defining additional functions, e.g.:

// my-app.jsonc
{
  "$schema": "http://json-schema.org/draft-07/schema",
  "description": "Validation schema for my-app written in just-func",
  "definitions": {
    "MyJustFunc": {
      "oneOf": [{
        "$ref": "just-func.jsonc/#/definitions/JustFunc"
      }, {
        "$ref": "#/definitions/MyExtensions"
      }]
    },
    "MyExtensions": {
      // ... snip ...
    }
  }
}

The problem is, if I use one of the default definition provided in just-func.jsonc,
they does not recognize the functions I defined in MyExtensions because their reference is (just-func.jsonc)#/definitions/JustFunc instead of (my-app.jsonc)#/definitions/MyJustFunc.

To solve this, we need a way to override the reference something akin to:

{
  "$override": {
    "just-junc.jsonc": {
      "#/definitions/JustFunc": "/#/definitions/MyJustFunc" // using `/#` to indicate it is a local reference
    }
  }
}

🌷

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions