{"id":4790,"library":"swagger-spec-validator","title":"Swagger Spec Validator","description":"Swagger Spec Validator is a Python library that validates Swagger Specs against the Swagger 1.2 or Swagger 2.0 specification. The validator aims to check for full compliance with the Specification. It is currently in active maintenance, with version 3.0.4 released in June 2024.","status":"active","version":"3.0.4","language":"en","source_language":"en","source_url":"https://github.com/Yelp/swagger_spec_validator","tags":["swagger","openapi","validation","api-specification"],"install":[{"cmd":"pip install swagger-spec-validator","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for underlying JSON schema validation.","package":"jsonschema","optional":false},{"reason":"Used for parsing YAML-formatted Swagger specifications.","package":"PyYAML","optional":false},{"reason":"Required for Python 3.9+ for resource loading.","package":"importlib_resources","optional":false}],"imports":[{"symbol":"validate_spec_url","correct":"from swagger_spec_validator import validate_spec_url"},{"note":"Used for validating a Swagger specification provided as a Python dictionary.","symbol":"validate_spec","correct":"from swagger_spec_validator import validate_spec"},{"symbol":"SwaggerValidationError","correct":"from swagger_spec_validator.common import SwaggerValidationError"}],"quickstart":{"code":"import yaml\nfrom swagger_spec_validator import validate_spec\nfrom swagger_spec_validator import validate_spec_url\nfrom swagger_spec_validator.common import SwaggerValidationError\n\n# Example 1: Validate a spec from a URL\ntry:\n    validate_spec_url('http://petstore.swagger.io/v2/swagger.json')\n    print(\"Petstore Swagger 2.0 spec from URL is valid!\")\nexcept SwaggerValidationError as e:\n    print(f\"Validation failed for URL spec: {e}\")\n\n# Example 2: Validate a spec from a Python dictionary (YAML content)\nswagger_spec_content = \"\"\"\nswagger: '2.0'\ninfo:\n  title: 'My Simple API'\n  version: '1.0.0'\nhost: 'api.example.com'\nbasePath: '/v1'\nschemes:\n  - https\npaths:\n  /users:\n    get:\n      summary: 'Get all users'\n      responses:\n        '200':\n          description: 'A list of users'\n\"\"\"\n\ntry:\n    spec_dict = yaml.safe_load(swagger_spec_content)\n    validate_spec(spec_dict)\n    print(\"Local Swagger 2.0 spec (dictionary) is valid!\")\nexcept SwaggerValidationError as e:\n    print(f\"Validation failed for local spec: {e}\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to validate a Swagger 2.0 specification using both a URL and a Python dictionary. It uses `validate_spec_url` for remote specifications and `validate_spec` for in-memory dictionary representations, which can be loaded from local YAML/JSON files using PyYAML."},"warnings":[{"fix":"Ensure your specification is Swagger 1.2 or 2.0. If using OpenAPI 3.x, switch to a compatible validator like `openapi-spec-validator`.","message":"This library explicitly supports Swagger 1.2 and Swagger 2.0 specifications only. It does NOT support OpenAPI 3.x. For OpenAPI 3.x validation, consider `openapi-spec-validator` (a separate library).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin the library version in your `requirements.txt` or `pyproject.toml` and review changelogs when upgrading.","message":"The API is not considered stable and may introduce non-backwards-compatible changes in minor versions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure network connectivity for URL validation or external references. For offline validation of `$ref` pointers, consider tools that bundle or dereference the spec first, or ensure all referenced schemas are local and resolvable within the provided spec dictionary.","message":"`validate_spec_url` requires network access to fetch the specification. If your specification contains external `$ref` pointers (e.g., to other URLs or local files not bundled with the primary spec), `validate_spec` will also require access to resolve these references.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of potential limitations when using complex JSON Schema constructs within Swagger specifications. Manual review or supplementary validation might be necessary for such cases.","message":"Swagger specifications (and by extension, this validator) rely on a subset of JSON Schema. Some advanced JSON Schema features like `oneOf` or `patternProperties` might not be fully supported or correctly validated.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}