Skip to content

Validations on properties with $ref are ignored #337

@mathijst-of

Description

@mathijst-of

If I have a JSON schema where the properties of an object are referenced by $ref and there are additional validations on such properties, then JSON's which do not fulfill the additional validations are considered valid. I want to do this to keep duplications of JSON objects to a minimum.

Visual Code does state that the JSON is invalid.

I'm using draft-07 for validation and version 2.3.0.

Here is an example of my schema. The workaround is copying everything from improvementNumber to improvementNumberNonNegative to remove the $ref and then validation will hold.

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "improvements": {
            "type": "object",
            "properties": {
                "improvementNumber": {
                    "$ref": "#/definitions/improvementNumber"
                },
                "improvementNumberNonNegative": {
                    "$ref": "#/definitions/improvementNumberNonNegative"
                }
            },
            "additionalProperties": false
        }
    },
    "additionalProperties": false,
    "definitions": {
            "renovationEffect": {
                "type": "string",
                "enum": [
                    "plus",
                    "new"
                ]
            },
            "improvementNumber": {
                "type": "object",
                "properties": {
                    "effectType": {
                        "$ref": "#/definitions/renovationEffect"
                    },
                    "value": {
                        "type": "number"
                    }
                },
                "additionalProperties": false,
                "required": [
                    "effectType",
                    "value"
                ]
            },
            "improvementNumberNonNegative": {
                "$ref": "#/definitions/improvementNumber",
                "if": {
                    "properties": {
                        "effectType": {
                            "enum": [
                                "new"
                            ]
                        }
                    }
                },
                "then": {
                    "properties": {
                        "value": {
                            "minimum": 0
                        }
                    }
                }
            }
    }
}

This JSON must be considered invalid:

{
    "improvements": {
        "improvementNumberNonNegative": {
            "effectType": "new",
            "value": -1
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions