Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations on properties with $ref are ignored #337

Open
mathijskolkhuistanke opened this issue Nov 26, 2024 · 0 comments
Open

Validations on properties with $ref are ignored #337

mathijskolkhuistanke opened this issue Nov 26, 2024 · 0 comments

Comments

@mathijskolkhuistanke
Copy link

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
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant