Skip to content

Bug with field named 'type' definition in '$defs' #317

@nicotrozzo

Description

@nicotrozzo

Hi,
I think I've reached a bug when using a definition called type.

This is the minimal example I could build to reproduce the situation:

Input

test.json file:

{
    "$schema": "test.schema.json",
    "type": "test"
}

Failing schema

test.schema.json

{
    "$schema": "http://json-schema.org/schema",
    "properties": {
        "type": {
            "$ref": "#/$defs/definitionGroup/type"
        }
    },
    "$defs": {
        "definitionGroup": {
            "type": {
                "type": "string",
                "description": "'type' description"
            }
        }
    }
}

Running check-jsonschema produces the following output:

$ check-jsonschema test.json --schemafile test.schema.json 
Error: schemafile was not valid: {'type': 'string', 'description': "'type' description"} is not valid under any of the given schemas

Failed validating 'anyOf' in metaschema['allOf'][0]['properties']['$defs']['additionalProperties']['$dynamicRef']['allOf'][3]['properties']['type']:
    {'anyOf': [{'$ref': '#/$defs/simpleTypes'},
               {'items': {'$ref': '#/$defs/simpleTypes'},
                'minItems': 1,
                'type': 'array',
                'uniqueItems': True}]}

On schema['$defs']['definitionGroup']['type']:
    {'description': "'type' description", 'type': 'string'}

SchemaError: {'type': 'string', 'description': "'type' description"} is not valid under any of the given schemas

Failed validating 'anyOf' in metaschema['allOf'][0]['properties']['$defs']['additionalProperties']['$dynamicRef']['allOf'][3]['properties']['type']:
    {'anyOf': [{'$ref': '#/$defs/simpleTypes'},
               {'items': {'$ref': '#/$defs/simpleTypes'},
                'minItems': 1,
                'type': 'array',
                'uniqueItems': True}]}

On schema['$defs']['definitionGroup']['type']:
    {'description': "'type' description", 'type': 'string'}
  in "/home/ntrozzo/.local/lib/python3.10/site-packages/check_jsonschema/checker.py", line 52
  >>> return self._schema_loader.get_validator(

If the definition is not inside definitionGroup it does not fail.

Schema fixed

Changing the definition name to types instead of type makes the validation pass:
test.schema.json:

{
    "$schema": "http://json-schema.org/schema",
    "properties": {
        "type": {
            "$ref": "#/$defs/definitionGroup/types"
        }
    },
    "$defs": {
        "definitionGroup": {
            "types": {
                "type": "string",
                "description": "'type' description"
            }
        }
    }
}

The output changes to:

$ check-jsonschema test.json --schemafile test.schema.json 
ok -- validation done

To be honest, I have not checked the JSON Schema spec to know if this is a valid schema or not, but it works fine using the JSON Schema VScode extension and it fails when using check-jsonschema.
It took me a while to realise where the error was, and I think it's possibly a bug because the type keyword could be easily hardcoded somewhere.

check-jsonschema version

$ check-jsonschema --version
check-jsonschema, version 0.23.2

I'm running this on Ubuntu 22.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions