Skip to content

No valid defaults for condiditonal values #17

@jpmaterial

Description

@jpmaterial

If a schema contains conditional values, no valid json will be produced from it.

I'm on Windows 10, jsonschema_default 1.5.0 installed via conda.

import jschon
import jsonschema_default

json_schema = {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "properties": {
        "mode": {
            "type": "string",
            "enum": ["S", "A"],
            "default": "S"
        }
    },
    "if": {
        "properties": {
            "mode": {
                "const": "S"
            }
        }
    },
    "then": {
        "properties": {
            "x": {
                "type": "number",
                "default": 0.0
            }
        },
        "required": ["x"]
    },
    "else": {
        "properties": {
            "y": {
                "type": "number",
                "default": 0.0
            }
        },
        "required": ["y"]
    }
}

defaults = jsonschema_default.create_from(json_schema)

jschon.create_catalog("2020-12")
jschon_schema = jschon.JSONSchema(json_schema)
validation_result = jschon_schema.evaluate(jschon.JSON(defaults))

print(f"Is valid: {validation_result.valid}")

In this example, the defaults dict contains a mode property, but no x property, which results in a negative validation result.

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