php-json-schema icon indicating copy to clipboard operation
php-json-schema copied to clipboard

JSON pointers not working in const using $data

Open alexeyinkin opened this issue 3 years ago • 1 comments

I want to test if two properties have equal values. We should be able to use pointers like this: https://stackoverflow.com/a/41192068/811451 using const and $data. But this is not working. I get an exception reading Const failed at #->properties:b

<?php

use Swaggest\JsonSchema\Schema;

include __DIR__ . '/../../../vendor/autoload.php';

$instanceJson = <<<'JSON'
{
    "a": "value",
    "b": "value"
}
JSON;
$instance = json_decode($instanceJson);

$schemaJson = <<<'JSON'
{
    "type": "object",
    "properties": {
        "a": {"type": "string"},
        "b": {
            "type": "string",
            "const": {"$data": "1/a"}
        }
    }
}
JSON;

$schema = Schema::import(json_decode($schemaJson));

$schema->in($instance);
echo 'OK';

I think that one of the following should be done:

  • Implement this.
  • Mention in the doc that it is not supported.
  • Give an example in the doc if something extra should be done for this to work.

alexeyinkin avatar Dec 11 '20 10:12 alexeyinkin

Mention in the doc that it is not supported.

As discussed in the stackoverflow question, $data is not a part JSON Schema spec (as of draft-07 at least), so it would be overly optimistic to expect it to work with any JSON Schema validator.

I agree that $data, although is not standardized yet, is a helpful and powerful tool. I think it makes sense to implement opt-in support for it.

vearutop avatar Dec 11 '20 10:12 vearutop