0
votes

I want to allow a $role properties anywhere in a json schema document where type is allowed. In theory I should be able to extend it as below where I do allOf against both the json schema meta-schema and my extension for $role which includes additionalProperties to pick up my extension meta-schema recursively. What I find is that I get validation for a top-level $role, but not for any embedded one. Am I missing something? Is there some other way I should extend the JSON Schema meta-schema?

I've tried a bunch of different validators and they all fail at validating the second $role in the example.

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "role": {
            "type": "string",
            "pattern": "^((lg)|(unionType\\([a-zA-Z][a-zA-Z0-9.]*\\)))$"
        },
    },
    "allOf": [
        {
            "$ref": "http://json-schema.org/draft-07/schema#"
        },
        {
            "properties": {
                "additionalProperties": {
                    "$ref": "#"
                },
                "$role": {
                    "oneOf": [
                        {
                            "$ref": "#/definitions/role"
                        },
                        {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/role"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

Example using this schema:

{
    "$schema": "schema.json",
    "title": "Prompt",
    "$role": "unionType(Microsoft.IPrompt)",
    "properties": {
        "prompt": {
            "type": "string",
            "$role":"foo"
        }
    }
}

What I expect is that the second role should be invalid according to the schema. What I get is that it is valid even though it does not match the $role pattern. The first $role does validate successfully.

1

1 Answers

2
votes

Yep, extending a meta schema is more complicated than it seems. Checkout the the JSON Hyper-Schema meta schema for an example of how to extend a meta schema. JSON Hyper-Schema adds two keywords: base and `links. When extending a schema, you need to redefine any recursive reference used in the original schema.

JSON Schemas (including meta-schemas) are immutable. You can't selectively modify an existing schema. Your meta schema only validates the $role keyword, all other keywords are validated by the draft-07 meta schema. Because your meta schema doesn't modify the draft-07 schema, keywords like properties are validated entirely within the context of the draft-07 schema and without knowledge of the new keyword you added in another schema.

It's unfortunate that so much duplication is involved in extending schemas and it is a problem that is being worked on. A potential solution to make this easier is slated to be introduced in the next draft.