vedro-spec-validator is a Vedro plugin that allows to validate mocks via OpenAPI spec/docs.
Version Compatibility Notice
- For versions of this package 0.1.0 and later, only version 2 of the
d42package is compatible.
Installation
Quick
For a quick installation, you can use a plugin manager as follows:
$ vedro plugin install vedro-spec-validator
Manual
To install manually, follow these steps:
- Install the package using pip:
$ pip3 install vedro-spec-validator
- Next, activate the plugin in your
vedro.cfg.pyconfiguration file:
# ./vedro.cfg.py
import vedro
import vedro_spec_validator
class Config(vedro.Config):
class Plugins(vedro.Config.Plugins):
class SpecValidator(vedro_spec_validator.SpecValidator):
enabled = True
Usage
Decorate your mocked function with @validate_spec(), providing a link to a YAML or JSON OpenAPI spec.
import jj
from jj.mock import mocked
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml")
async def your_mocked_function():
matcher = jj.match("GET", "/users")
response = jj.Response(status=200, json=[])
mock = await mocked(matcher, response)
Keys
- The
is_strictkey allows you to choose between strict and non-strict comparison. Non-strict comparison (Falseby default) allows you to mock only some fields from the spec, even if they are required. Strict validation (True) requires you to mock all required fields from the spec.
Example OpenAPI spec with required fields:
openapi: 3.0.0
paths:
/user:
get:
...
properties:
user_name:
type: string
email:
type: string
age:
type: integer
required:
- user_name
- email
- age
With is_strict=True:
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml", is_strict=True)
async def your_mocked_function():
matcher = jj.match("GET", "/user")
payload = {
"user_name": "Foo",
"email": "test@test.com",
"age": 30 # All required fields from the spec must be present
}
response = jj.Response(json=payload)
return await mocked(matcher, response)
With is_strict=False:
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml", is_strict=False)
async def your_mocked_function():
matcher = jj.match("GET", "/user")
payload = {
"user_name": "Foo" # Required fields like "email" and "age" can be omitted
}
response = jj.Response(json=payload)
return await mocked(matcher, response)
- The
prefixkey allows you to specify a path prefix that should be removed from the mock function's paths before they are matched against the OpenAPI spec.
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml", prefix='/__mocked_api__') # Goes to validate `/user` instead of `/__mocked_api__/user`
async def your_mocked_function():
matcher = jj.match("GET", "/__mocked_api__/user")
# The prefix is removed, so the actual path matched against the OpenAPI spec is `/user`
...
- The
is_raise_errorkey allows you to control whether an error should be raised when a validation mismatch occurs. By default (False), no exception is raised, but mismatches will be logged to a file.
With is_raise_error=False:
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml", is_raise_error=False)
async def your_mocked_function():
...
Output will be written to a file:
# /spec_validator/validation_results/your_mocked_function/scenarios/path/to/test/test_scenario.py.txt
subject: scenario subject
valera.ValidationException
- ... # missmatches
With is_raise_error=True:
from vedro_spec_validator import validate_spec
@validate_spec(spec_link="http://example.com/api/users/spec.yml", is_raise_error=True)
async def your_mocked_function():
...
An exception will be raised on the first validation error, causing the test to fail with the following trace:
ValidationException: There are some mismatches in your_mocked_function:
valera.ValidationException
- ... # missmatches
# --seed ...
# 1 scenario, 0 passed, 1 failed, 0 skipped (2.35s)
Process finished with exit code 1
- The
force_strictkey enforces strict validation against the OpenAPI spec, treating all fields as required, even if they are marked as optional in the spec. This is useful in cases where the specification accidentally marks all fields as optional.Falseby default.
Metadata
Release files for vedro-spec-validator 0.1.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| vedro_spec_validator-0.1.15.tar.gz | 24.6 kB | Without provenance | Details for vedro_spec_validator-0.1.15.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| vedro_spec_validator-0.1.15-py3-none-any.whl | Python 3 | none | any | Details for vedro_spec_validator-0.1.15-py3-none-any.whl |
Total release size: 54.5 kB
Release files / vedro_spec_validator-0.1.15.tar.gz
| Download URL | vedro_spec_validator-0.1.15.tar.gz |
|---|---|
| Size | 24.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b43c652c9919e70f9219343ac2d3fe70795c82b03de24d544a5f2854ff1a967f
|
|
BLAKE2b-256 checksum How to use checksums |
92fd20c4f0d0e13656b8d2e05ed0bc2287177dfdeeed26ee1a2ff60edfcceccf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|
Release files / vedro_spec_validator-0.1.15-py3-none-any.whl
| Download URL | vedro_spec_validator-0.1.15-py3-none-any.whl |
|---|---|
| Size | 29.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
21993ce3977d767661d1ca653953a6300b7a5dd99bc9b9d63f1a9d9e484ab0bf
|
|
BLAKE2b-256 checksum How to use checksums |
0fca1a9555ca997255f461b7d886085b4f51257e6510b5657aee01b2144f3632
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|
Security for vedro-spec-validator 0.1.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.