def validate_json(json_document, schema_path): """Validate a json document against a json schema. :param json_document: json document in the form of a list or dict. :param schema_path: package relative path of the json schema file. """ schema_path = resource_filename('swagger_spec_validator', schema_path) schema = read_file(schema_path) resolver = RefResolver( base_uri=get_uri_from_file_path(schema_path), referrer=schema, handlers=default_handlers, ) jsonschema.validate(json_document, schema, resolver=resolver)
def validate_json(spec_dict, schema_path, spec_url='', http_handlers=None): """Validate a json document against a json schema. :param spec_dict: json document in the form of a list or dict. :param schema_path: package relative path of the json schema file. :param spec_url: base uri to use when creating a RefResolver for the passed in spec_dict. :param http_handlers: used to download any remote $refs in spec_dict with a custom http client. Defaults to None in which case the default http client built into jsonschema's RefResolver is used. This is a mapping from uri scheme to a callable that takes a uri. :return: RefResolver for spec_dict with cached remote $refs used during validation. :rtype: :class:`jsonschema.RefResolver` """ schema_path = resource_filename('swagger_spec_validator', schema_path) schema = read_file(schema_path) schema_resolver = RefResolver( base_uri=get_uri_from_file_path(schema_path), referrer=schema, handlers=default_handlers, ) spec_resolver = RefResolver( base_uri=spec_url, referrer=spec_dict, handlers=http_handlers or default_handlers, ) ref_validators.validate( instance=spec_dict, schema=schema, resolver=schema_resolver, instance_cls=ref_validators.create_dereffing_validator(spec_resolver), cls=Draft4Validator, ) # Since remote $refs were downloaded, pass the resolver back to the caller # so that its cached $refs can be re-used. return spec_resolver
def test_read_file(): read_file('./tests/data/v2.0/petstore.json')