jsonschema.exceptions.ValidationError: 'os-fault.json' is not of type 'object'

Viewed 62

I want to create JSON file the schema is 'http://json-schema.org/draft-04/schema#'. This is JSON file that I write.

{
    "cloud_management" : {
        "driver": "universal"
    },
    "node_discover": {
        "driver": "node_list",
        "args": [
            {
                "ip": "{my_ip}",
                "auth": {
                    "username": "{my_username}"
                }
            },
            {
                "ip": "{my_2nd_ip}",
                "auth": {
                    "username": "{my_username}"
                }
            }
        ]
    },
    "services": {
        "memcached": {
            "driver": "system_service",
            "args": {
                "service_name": "memcached",
                "grep": "memcached"
            }
        },
        "rabbitmq": {
            "driver": "system_service",
            "args": {
                "service_name": "rabbitmq",
                "grep": "rabbitmq"
            }
        }
    },
    "power_managements": [
        {
            "driver": "libvirt",
            "args": {
                "connection_uri": "qemu+unix:///system"
            }
        }
    ]
}

The {my_ip}, {my_2nd_ip} and {my_username} will be converted to real value. I check the validity of this file with JSON schema validator (the link of the site: https://www.jsonschemavalidator.net/), and it says 'No Error found. JSON validates against the schema'. However, when I run the program I got this error like this.

Traceback (most recent call last):
  File "kill_service.py", line 18, in <module>
    main()
  File "kill_service.py", line 8, in main
    cloud_management = os_faults.connect(cloud_config = 'os-fault.json')
  File "/usr/local/lib/python3.8/dist-packages/os_faults/__init__.py", line 147, in connect
    jsonschema.validate(cloud_config, CONFIG_SCHEMA)
  File "/usr/lib/python3/dist-packages/jsonschema/validators.py", line 934, in validate
    raise error
jsonschema.exceptions.ValidationError: 'os-fault.json' is not of type 'object'

Failed validating 'type' in schema:
    {'$schema': 'http://json-schema.org/draft-04/schema#',
     'properties': {'cloud_management': {'additionalProperties': False,
                                         'properties': {'args': {'type': 'object'},
                                                        'driver': {'type': 'string'}},
                                         'required': ['driver'],
                                         'type': 'object'},
                    'node_discover': {'additionalProperties': False,
                                      'properties': {'args': {},
                                                     'driver': {'type': 'string'}},
                                      'required': ['driver', 'args'],
                                      'type': 'object'},
                    'power_managements': {'items': {'additionalProperties': False,
                                                    'properties': {'args': {'type': 'object'},
                                                                   'driver': {'type': 'string'}},
                                                    'required': ['driver',
                                                                 'args'],
                                                    'type': 'object'},
                                          'minItems': 1,
                                          'type': 'array'},
                    'services': {'additionalProperties': False,
                                 'patternProperties': {'.*': {'additionalProperties': False,
                                                              'properties': {'args': {'type': 'object'},
                                                                             'driver': {'type': 'string'},
                                                                             'hosts': {'items': {'type': 'string'},
                                                                                       'minItems': 1,
                                                                                       'type': 'array'}},
                                                              'required': ['driver',
                                                                           'args'],
                                                              'type': 'object'}},
                                 'type': 'object'}},
     'required': ['cloud_management'],
     'type': 'object'}

On instance:
    'os-fault.json'

My JSON file name is 'os-fault.json'. When the program checks the validity of JSON file with

jsonschema.validate(cloud_config, CONFIG_SCHEMA),

it detect some errors in my JSON file. But I can't find out any error in that file.

Thanks for your help.

1 Answers

The error you're getting is misleading because it's not running verification against the contents of your JSON file because you haven't passed it what it expects.

os-faults expects a dict object for the cloud_config argument, which requires you to load the JSON file first. If you want to pass the .json file directly, you have to use the config_filename argument instead, like this.

import os_faults 

cloud_management = os_faults.connect(config_filename="os-fault.json")
cloud_management.verify()

If you want to do it by loading JSON and specifying the cloud_config parameter, you have to load your os-fault.json file first and then pass it like this.

import json

import os_faults

with open("os-fault.json", "r") as f:
    config = json.load(f)

cloud_management = os_faults.connect(cloud_config=config)
cloud_management.verify()
Related