Home > Blockchain >  JSONSchema keyword "type" when encased inside "items" fails to validate
JSONSchema keyword "type" when encased inside "items" fails to validate

Time:03-03

I'm trying to write a json validator to check files before runtime but there's a really odd issue happening with using "type".

I know "type" is a reserved word but jsonSchema doesn't have an issue with it if it doesn't have a value pairing as I found in this other question: Key values of 'key' and 'type' in json schema.

The solution to their problem was encasing "type": { "type": "string"} inside "properties" and it does work. However, my implementation requires it to be inside an array. Here's the snippet of my code:

{
    "type": "object",
    "additionalProperties": false,
    "properties":{
        "method":{
            "type": "array",
            "items":{
                "type": {
                    "type": "string"
                },
                "name":{
                    "type": "string"
                },
                "provider": {
                    "type": "array"
                }
            }
        }
    }
}

Oddly enough, VScode doesn't have a problem with it in a file when it's isolated, but when it's included in the main code, it doeesn't like it and yields no solution. Regardless, validating it with python yields:

...
raise exceptions.SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: {'type': 'string'} is not valid under any of the given schemas

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

On schema['properties']['method']['items']['type']:
    {'type': 'string'}

What further confuses me is that https://www.jsonschemavalidator.net/ tells me Expected array or string for 'type', got StartObject. Path 'properties.method.items.type', line 8, position 17. yet JSON Schema Faker is able to generate a fake file without any problems. The generated fake json also returns the same error when validated with python and JSONSchemaValidator.

I'm a beginner and any help or insight will be greatly appreciated, thanks for your time.

Edit: here's the snippet of the input data as requested.

{
...
    "method": [
        {
            "type": "action",
            "name": "name of the chaos experiment to use here",
            "provider": [
                ]
            }
        }
    ]
}

CodePudding user response:

Arrays don't have properties; arrays have items. The schema as you have included it is not valid; are you sure you don't mean to have this?

{
    "type": "object",
    "additionalProperties": false,
    "properties":{
        "method":{
            "type": "array",
            "items":{
                "type": "object",
                "properties": {
                    "type": {
                        "type": "string"
                    },
                    "name":{
                        "type": "string"
                    },
                    "provider": {
                        "type": "array"
                    }
                }
            }
        }
    }
}
  • Related