Skip to main content

Vedro Spec Validator plugin

Project description

vedro-spec-validator is a Vedro plugin that allows to validate mocks via OpenAPI spec/docs.

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:

  1. Install the package using pip:
$ pip3 install vedro-spec-validator
  1. Next, activate the plugin in your vedro.cfg.py configuration file:
# ./vedro.cfg.py
import vedro
import vedro_spec_validator

class Config(vedro.Config):

    class Plugins(vedro.Config.Plugins):

        class VedroSpecValidator(vedro_spec_validator.VedroSpecValidator):
            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

  1. The is_strict key allows you to choose between strict and non-strict comparison. Non-strict comparison (False by 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)
  1. The prefix key 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`
    ...
  1. The is_raise_error key 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
  1. The force_strict key 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. False by default.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vedro_spec_validator-0.0.9.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

vedro_spec_validator-0.0.9-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file vedro_spec_validator-0.0.9.tar.gz.

File metadata

  • Download URL: vedro_spec_validator-0.0.9.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for vedro_spec_validator-0.0.9.tar.gz
Algorithm Hash digest
SHA256 c5407277ffd3d996302c13dbadd8f27fdedba9dbc7d6d29a1ba5f20424572535
MD5 55deaf98975ed573b874e85a9d133de2
BLAKE2b-256 de6000b131b1cd871b860e7157dedefa8859d59813335567fd234884439ddaf9

See more details on using hashes here.

File details

Details for the file vedro_spec_validator-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for vedro_spec_validator-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1253af350f916bcfb75b1dccc5904911be86690f05101b4b35741eb0dc8d1232
MD5 30c73b02760677f773bbc543023a6f40
BLAKE2b-256 4f6bb31c2d1007d9e0c150951d23d50d262b551998d44d0bad56a44350023f70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page