swaggest / php-json-schema

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

Better error notification on Schema in() #25

Closed franksl closed 6 years ago

franksl commented 6 years ago

Hello, It would be very useful to have a parseable or simplified notification for errors to allow for better recognition. This happens to me especially when I use many "oneOf" statements along with "patternProperties". For example given a schema similar to this:

{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
       "type": "object",
        "patternProperties": {
            "^[a-zA-Z0-9_]+$": {
                "oneOf": [
                    ** schema 1 **,
                    ** schema 2 **,
                    ** schema 3**
                ]
            }
        }
    }
}

I receive an error like:

Failed due to logical constraint: no valid results for oneOf { 0: Failed due to logical constraint: no valid results for oneOf { 0: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[0] 1: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[1] 2: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[2] 3: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[3] 4: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[4] 5: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[5] 6: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[6] 7: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[7] 8: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[8] 9: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[9] 10: Failed due to logical constraint: not at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[10] 11: Required property missing: name at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0]->oneOf[11] } at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[0] 1: Required property missing: name at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[1] 2: Required property missing: name at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof->oneOf[2] } at #->properties:actions->patternProperties[^[a-zA-Z0-9_]+$]:dsDocSof

It's very difficult to understand the exact property that was written wrong and where.

Thanks, Frank

vearutop commented 6 years ago

Error message for oneOf, anyOf and allOf is indented multi-line, so you should be able to see the problem on every step line by line:

Failed due to logical constraint: no valid results for oneOf {
 0: Enum failed, enum: ["a"], data: "f" at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[0]
 1: Enum failed, enum: ["b"], data: "f" at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[1]
 2: Failed due to logical constraint: no valid results for anyOf {
   0: Enum failed, enum: ["c"], data: "f" at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[2]->anyOf[0]
   1: Enum failed, enum: ["d"], data: "f" at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[2]->anyOf[1]
   2: Enum failed, enum: ["e"], data: "f" at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[2]->anyOf[2]
 } at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[2]
} at #->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo

at item shows the path to invalid data and schema

#->properties:root->patternProperties[^[a-zA-Z0-9_]+$]:zoo->oneOf[2]

Schema path: /properties/root/patternProperties/^[a-zA-Z0-9_]+$/oneOf/2 Data path: /root/zoo

I'm working on improving readability/debugability of error messages, will push update soon.

vearutop commented 6 years ago

Please try https://github.com/swaggest/php-json-schema/releases/tag/v0.10.3