bluesky
bluesky

Reputation: 150

JSON Schema enum does not affect validation

I have a sub-schema defined in nested objects and cannot make the enum constraint work. See here....

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "Top level": {
            "type": "object",
            "properties": {
                "State": {
                    "type": "object",
                    "description": "stuff",
                    "properties": {
                        "Value": {
                            "type": "string",
                            "enum:": [
                                "A",
                                "B",
                                "C"
                            ]
                        },
                        "readOnly": true
                    },
                    "required": [
                        "Value"
                    ]
                }
            },
            "required": [
                "State"
            ]
        }
    },
    "required": [
        "Top level"
    ]
}

This should fail but instead it validates. Below...

{
    "Top level": {
        "State": {
            "Value": "not supposed to validate but does anyway"
        }
    }
}

Oddly, this schema appears to work and block the undesired strings but it does not have the deeper sub-schema structure...

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "Value": {
            "type": "string",
            "enum": [
                "A",
                "B",
                "C"
            ]
        }
    }
}

and this example properly gets rejected...

{
    "Value": "D"
}

What am I doing wrong ? It must be something fundamental about nested objects.I know if I change the Value name, it detects it is missing and rejects during validation in the first example... why does it not detect the invalid enum strings ?

Any help would be appreciated !

Upvotes: 1

Views: 1771

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

This was really hard to spot for some reason. I thought I was going nuts too. You've got an extra : in there.

"enum:": [
     ^

Upvotes: 3

Related Questions