Newtonsoft.Json.Schema icon indicating copy to clipboard operation
Newtonsoft.Json.Schema copied to clipboard

Wrong properties evaluation while using "allOf"

Open SkyKnight opened this issue 1 year ago • 0 comments

This is my intentionally complicated example JSON schema:

{
    "$id": "configuration-entry",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "allOf": [
        {
            "if": {
                "properties": { "type": { "const": "form" } }
            },
            "then": {
                "allOf": [
                    {
                        "properties": {
                            "components": {
                                "type": "object",
                                "additionalProperties": {
                                    "type": "object",
                                    "allOf": [
                                        {
                                            "if": {
                                                "oneOf": [
                                                    {"properties": { "type": { "const": "text" } }},
                                                    {"properties": { "type": { "const": "text-plus" } }}
                                                ]
                                            },
                                            "then": {
                                                "allOf": [
                                                    {
                                                        "properties": {
                                                            "type": {
                                                                "type": "string"
                                                            }
                                                        }
                                                    },
                                                    {
                                                        "properties": {
                                                            "param1": {
                                                                "type": ["string", "number"]
                                                            },
                                                            "alignement": {
                                                                "type": "string"
                                                            }
                                                        }
                                                    }
                                                ],
                                                "unevaluatedProperties": false
                                            }
                                        },
                                        {
                                            "if": {
                                                "properties": { "type": { "const": "image" } }
                                            },
                                            "then": {
                                                "allOf": [
                                                    {
                                                        "properties": {
                                                            "type": {
                                                                "type": "string"
                                                            }
                                                        }
                                                    },
                                                    {
                                                        "properties": {
                                                            "meta": {
                                                                "type": "object"
                                                            },
                                                            "param1": {
                                                                "type": ["string", "number"]
                                                            }
                                                        }
                                                    }
                                                ],
                                                "unevaluatedProperties": false
                                            }
                                        }
                                    ],
                                    "properties": {
                                        "type": {
                                            "type": "string"
                                        }
                                    },
                                    "required": ["type"]
                                }
                            }
                        },
                        "required": ["components"]
                    }
                ]
            }
        }
    ],
    "properties": {
        "type": {
            "type": "string"
        }
    },
    "required": ["type"]
}

And my simple JSON document:

{
    "type": "form",
    "components": {
        "first": {
            "type": "text",
            "param1": 1,
            "alignement": "left"
        },
        "second": {
            "type": "image",
            "param1": "1",
            "meta": {
                "location": "nowhere",
                "date": "now"
            }
        },
        "third": {
            "type": "text-plus",
            "param1": 1,
            "alignement": "left"
        }
    }
}

Expected result: document is valid

Current situation:

        JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '', line 1, position 1, schema 'configuration-entry'
                JSON does not match schema from 'then'. Path '', line 1, position 1, schema 'configuration-entry#/allOf/0/then'
                        JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '', line 1, position 1, schema 'configuration-entry#/allOf/0/then'
                                JSON does not match all schemas from 'allOf'. Invalid schema indexes: 1. Path 'components.third', line 17, position 18, schema 'configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties'
                                        Property 'meta' has not been successfully evaluated and the schema does not allow unevaluated properties. Path 'components.third', line 17, position 18, schema 'configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/1'

ajv command line shows document is valid

$ ajv -s schema.json -d data.json --spec=draft2020
strict mode: use allowUnionTypes to allow union type keyword at "configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/0/then/allOf/1/properties/param1" (strictTypes)
strict mode: use allowUnionTypes to allow union type keyword at "configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/1/then/allOf/1/properties/param1" (strictTypes)
data.json valid

same story with https://json-schema.hyperjump.io/

SkyKnight avatar Aug 17 '23 13:08 SkyKnight