JVal icon indicating copy to clipboard operation
JVal copied to clipboard

Absolute schema URI is required even for local references

Open JanTvrdik opened this issue 9 years ago • 2 comments

Absolute schema URI is required to validate the following schema. Bug or feature?

{
  "type": "object",
  "properties": {
    "label": {"type": "string"},
    "children": {
      "type": "array",
      "items": {"$ref": "#"}
    }
  },
  "required": ["label"]
}

JanTvrdik avatar Apr 08 '16 16:04 JanTvrdik

Not a bug per se, more an annoying limitation. Resolution of relative URIs currently requires an absolute base url no matter what. However, if it cannot be avoided for most relative URIs, an exception could probably be made for pointers/fragments, which are relative but point to the same document.

stefk avatar Apr 10 '16 14:04 stefk

Assuming we make Resolver::registerSchema public, it would be nice support not-requiring absolute URLs everytime the target schema is known and therefore does not require fetching.

$validator = Validator::buildDefault();
$validator->getResolver()->registerSchema($commonSchema, new Uri('shared-definitions.json'));
$validator->validate($instance, json_decode('
    {
      "type": "object",
      "properties": {
        "name": {"type": "string"},
        "age": {"$ref": "shared-definitions.json#age"}
      },
      "required": ["label"]
    }
'));

JanTvrdik avatar Apr 10 '16 15:04 JanTvrdik