swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
437 stars 50 forks source link

JSON pointers not working in const using $data #112

Open alexeyinkin opened 3 years ago

alexeyinkin commented 3 years ago

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:

vearutop commented 3 years ago

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.