def validate_spec(spec_json, spec_url=None): """Validates a Swagger 2.0 API Specification given a Swagger Spec. :param spec_json: the json dict of the swagger spec. :type spec_json: dict :param spec_url: url serving the spec json. Used for dereferencing relative refs. eg: file:///foo/swagger.json :type spec_url: string :returns: `None` in case of success, otherwise raises an exception. :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` """ # Dereference all $refs so we don't have to deal with them fix_malformed_model_refs(spec_json) spec_json = jsonref.JsonRef.replace_refs(spec_json, base_uri=spec_url or '') replace_jsonref_proxies(spec_json) # Must validate after all refs in spec_json have been de-reffed because # jsonschema doesn't support validation of external refs validate_json(spec_json, 'schemas/v2.0/schema.json') apis = spec_json['paths'] definitions = spec_json.get('definitions', {}) validate_apis(apis) validate_definitions(definitions)
def validate_resource_listing(resource_listing): """Validate a Resource Listing (§5.1). :param resource_listing: a dictionary respresentation of a Resource Listing. Note that you will have to invoke `validate_api_declaration` on each linked API Declaration. :returns: `None` in case of success, otherwise raises an exception. :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` :raises: :py:class:`jsonschema.exceptions.ValidationError` """ validate_json(resource_listing, 'schemas/v1.2/resourceListing.json')
def validate_spec(spec_json): """Validates a Swagger 2.0 API Specification given a Swagger Spec. :param spec_json: the json dict of the swagger spec. :type spec_json: dict :returns: `None` in case of success, otherwise raises an exception. :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` :raises: :py:class:`jsonschema.exceptions.ValidationError` """ validate_json(spec_json, 'schemas/v2.0/schema.json') # TODO: Extract 'parameters', 'responses' from the spec as well. apis, definitions = spec_json['paths'], spec_json['definitions'] validate_apis(apis) validate_definitions(definitions)
def validate_api_declaration(api_declaration): """Validate an API Declaration (§5.2). :param api_declaration: a dictionary respresentation of an API Declaration. :returns: `None` in case of success, otherwise raises an exception. :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` :raises: :py:class:`jsonschema.exceptions.ValidationError` """ validate_json(api_declaration, 'schemas/v1.2/apiDeclaration.json') model_ids = get_model_ids(api_declaration) for api in api_declaration['apis']: validate_api(api, model_ids) for model_name, model in api_declaration.get('models', {}).iteritems(): validate_model(model, model_name, model_ids)
def validate_api_declaration(api_declaration): """Validate an API Declaration (§5.2). :param api_declaration: a dictionary respresentation of an API Declaration. :returns: `None` in case of success, otherwise raises an exception. :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` :raises: :py:class:`jsonschema.exceptions.ValidationError` """ validate_json(api_declaration, 'schemas/v1.2/apiDeclaration.json') model_ids = get_model_ids(api_declaration) for api in api_declaration['apis']: validate_api(api, model_ids) for model_name, model in six.iteritems(api_declaration.get('models', {})): validate_model(model, model_name, model_ids)
def test_success(): my_dir = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(my_dir, '../data/v2.0/petstore.json')) as petstore_file: petstore_spec = json.load(petstore_file) validate_json(petstore_spec, 'schemas/v2.0/schema.json')
def test_failure(): with pytest.raises(SwaggerValidationError): validate_json({}, 'schemas/v2.0/schema.json')