Flex

raw JSON →
6.14.1 verified Fri May 01 auth: no python

Flex is a Swagger/OpenAPI schema validation library for Python. It validates API responses against a Swagger specification. Current version is 6.14.1. Release cadence is irregular, with recent releases focusing on maintenance and compatibility updates.

pip install flex
error AttributeError: module 'flex' has no attribute 'validate_api'
cause Trying to call validate_api from the top-level module, but it's in flex.validation.
fix
Use: from flex.validation import validate_api
error flex.exceptions.NoSuchPath: No matching path found for /api/users/1
cause The path in the request does not match any path in the Swagger spec (or path templating mismatch).
fix
Ensure the path matches exactly, including parameter placeholders like {id}.
deprecated Flex uses collections.abc which may cause DeprecationWarning in Python 3.8+. Fixed in v6.14.1.
fix Upgrade to flex >=6.14.1
breaking Version 6.14.0 removed maximum version constraints on dependencies (e.g., PyYAML). This may introduce incompatibilities if you rely on older versions.
fix Pin PyYAML version if needed: pip install 'PyYAML<5.1'
gotcha flex.load() expects a file-like object or a string, not a raw dictionary.
fix Use flex.loads() for a string, or flex.load() for an open file object.

Validate an API response against a Swagger specification.

import flex
from flex.validation import validate_api

# Load a Swagger spec from a file
with open('swagger.yaml', 'r') as f:
    schema = flex.load(f)

# Example API response (dict)
response = {
    'status_code': 200,
    'headers': {'content-type': 'application/json'},
    'body': '{"id": 1, "name": "test"}'
}

# Validate response against a specific path and method
try:
    validate_api(schema, path='/users/{id}', method='get', response=response)
    print('Validation passed')
except flex.exceptions.Error as e:
    print(f'Validation failed: {e}')