def validate_spec_json(spec, schema_path, spec_url='', http_handlers=None): schema_path = os.path.abspath(schema_path) with open(schema_path, 'r') as schema_file: schema = json.loads(schema_file.read()) schema_resolver = RefResolver('file://{}'.format(schema_path), schema) spec_resolver = RefResolver(spec_url, spec, handlers=http_handlers or {}) ref_validators.validate(spec, schema, resolver=schema_resolver, instance_cls=ref_validators.create_dereffing_validator(spec_resolver), cls=Draft4Validator) return spec_resolver
def validate_spec_json(spec, schema_path, spec_url='', http_handlers=None): schema_path = os.path.abspath(schema_path) with open(schema_path, 'r') as schema_file: schema = json.loads(schema_file.read()) schema_resolver = RefResolver('file://{}'.format(schema_path), schema) spec_resolver = RefResolver(spec_url, spec, handlers=http_handlers or {}) ref_validators.validate(spec, schema, resolver=schema_resolver, instance_cls=ref_validators.create_dereffing_validator(spec_resolver), cls=Draft4Validator) return spec_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 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