Skip to content

Subtype validation via links with describedBy. #118

Open
@thatjpk

Description

@thatjpk

I'm trying to validate objects that are serialized from different types that share a common super-type. Every object has a collection of common fields, and then depending on the value of a type field, additional type-specific fields. I'm using a schema like this:

{
    "$schema": "http://json-schema.org/draft-04/hyper-schema#",
    "title": "Common Schema",
    "type": "object",
    "properties": {
        "type": {
            "enum": ["foo", "bar"]
        },
        "commonField": {
            "type": "string"
        }
    },
    "required": [
        "type", "commonField"
    ],

    "links": [
        {
            "rel": "describedBy",
            "href": "#/definitions/{type}"
        }
    ],

    "definitions": {
        "foo": {
            "$schema": "http://json-schema.org/draft-04/hyper-schema#",
            "title": "Foo Schema",
            "type": "object",
            "properties": {
                "fooField": {
                    "type": "string"
                }
            },

            "required": [ "fooField" ]
        },

        "bar": {
            "$schema": "http://json-schema.org/draft-04/hyper-schema#",
            "title": "Bar Schema",
            "type": "object",
            "properties": {
                "barField": {
                    "type": "string"
                }
            },

            "required": [ "barField" ]
        }
    }
}

And trying to validate an object like:

{
    "type": "foo",
    "commonField": "asdf",
    "fooField": 123
}

This message passes validation even though fooField is a number instead of a string. It seems that the common fields are validated just fine, but none of the type-specific schemas take effect. I see this behavior both in my application (using json-schema-validator v2.2.5), and in the online demo at http://json-schema-validator.herokuapp.com/.

Is my schema valid, and does it correctly reflect my intent? If so, is the validator's behavior here expected? Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions