swaggest / php-json-schema

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

processingPath/schemaPointer #150

Open massadm opened 1 year ago

massadm commented 1 year ago

Hello @vearutop and all contributors! I have a question.

https://github.com/swaggest/json-diff/releases/tag/v3.10.4 https://github.com/swaggest/php-json-schema/releases/tag/v0.12.41 PHP 5.6.33

JSON Schema:

{
    "$schema": "https://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "items": {
            "type": "array",
            "items": {
                "allOf": [
                    { "type": "number" },
                    { "type": "integer", "minimum": 1 },
                    { "type": "integer", "maximum": 2 }
                ]
            }
        }
    }
}

Data:

{
    "items": [
        1,
        "z"
    ]
}

Expected error:

Swaggest\JsonSchema\Exception\Error Object
(
    [error] => Number expected, "z" received
    [schemaPointers] => Array
        (
            [0] => /properties/items/items/1/allOf/0
        )

    [dataPointer] => /items/1
    [processingPath] => #->properties:items->items[1]:1->allOf[0]
    [subErrors] => 
)

But what does :1 mean in processingPath and why exception 'Swaggest\JsonDiff\JsonPointerException' with message 'Key not found: 1' in ./vendor/swaggest/json-diff/src/JsonPointer.php:226 happening on trying JsonPointer::getByPointer() with JSON Schema above and Error->getSchemaPointer() as a pointer?

massadm commented 1 year ago

In details.

JsonPointer::getByPointer(<JSON Schema from example above>, '/properties/items/items/1/allOf/0') result 'Key not found: 1'.

Stack trace:
#0 ./vendor/swaggest/json-diff/src/JsonPointer.php(243): Swaggest\JsonDiff\JsonPointer::get(Object(Swaggest\JsonSchema\Schema), Array)
#1 ./SchemaService.php(37): **Swaggest\JsonDiff\JsonPointer::getByPointer**(Object(Swaggest\JsonSchema\Schema), '/properties/ite...')
#2 ./DebugValidate.php(97): SchemaService->validate(Object(stdClass), Object(stdClass))
...

While JsonPointer::getByPointer(<JSON Schema from example above>, preg_replace('/\/items\/items\/\d+\//', '/items/items/', '/properties/items/items/1/allOf/0')) works.