Skip to main content

Schema Validator

vedro-valera-validator is a plugin for the Vedro framework that utilizes the valera validator, a package designed for data validation based on d42 (district42) schemas. Valera validator provides a simple yet powerful approach to ensure your data aligns perfectly with your expectations.

Setting Up

For a quick installation, you can use a plugin manager like so:

$ vedro plugin install vedro-valera-validator

Traditional Data Validation

Traditionally, you might have used standard assertion to compare your actual data with your expected data. Here, an HTTP GET request retrieves a user's information, which is then validated against the expected data through an assertion.

import vedro
import httpx

class Scenario(vedro.Scenario):
subject = "retrieve users"

def when_guest_retrieves_users(self):
self.response = httpx.get("/api/users/1")

def then_it_should_return_user(self):
assert self.response.json() == {
"id": 1,
"name": "Bob",
"created_at": "2020-01-01T00:00:00",
"is_deleted": False,
}

However, if the validation fails, standart AssertionError provides limited insight into why the test failed. You're often left with just a side-by-side comparison of the expected and actual data, which can be time-consuming to decipher, especially with complex data.


AssertionError: assert {'created_at': '2020-01-00T00:00:00', 'id': 1, 'is_deleted': True, 'name': 'Bob'} == {'created_at': '2020-01-01T00:00:00', 'id': 1, 'is_deleted': False, 'name': 'Bob'}
+ where {'created_at': '2020-01-00T00:00:00', 'id': 1, 'is_deleted': True, 'name': 'Bob'} = <scenarios.retrieve_users.Response object at 0x102da1120>.body

Leveling Up Data Validation

With the valera package, data validation becomes more comprehensive and insightful. By incorporating valera into your tests, you can enjoy more detailed feedback when validation fails. This is made possible by from_native function from the d42 package, which takes your expected data and transforms it into a d42 schema.

note

As d42 is a dependency of the vedro-valera-validator plugin, there's no need to install it separately. It's automatically installed alongside the plugin.

import vedro
import httpx
from d42 import from_native

class Scenario(vedro.Scenario):
subject = "retrieve users"

def when_guest_retrieves_users(self):
self.response = httpx.get("/api/users/1")

def then_it_should_return_user(self):
assert self.response.json() == from_native({
"id": 1,
"name": "Bob",
"created_at": "2020-01-01T00:00:00",
"is_deleted": False,
})
behind-the-scenes transformation
from d42 import from_native, schema

from_native({
"id": 1,
"name": "Bob",
"created_at": "2020-01-01T00:00:00",
"is_deleted": False,
})

# is equivalent to

schema.dict({
"id": schema.int(1),
"name": schema.str("Bob"),
"created_at": schema.str("2020-01-01T00:00:00"),
"is_deleted": schema.bool(False)
})

The schema is then compared with the actual data, and any discrepancies are highlighted. When a validation fails, valera provides a clear explanation of each mismatch between the expected and actual data, along with the location of each mismatch within the data structure.

ValidationException: 
- Value <class 'str'> at _['created_at'] must be equal to '2020-01-01T00:00:00', but '2020-01-00T00:00:00' given
- Value <class 'bool'> at _['is_deleted'] must be equal to False, but True given

Provided validation exception indicates that the created_at value and is_deleted values do not match the expected data. Such a detailed error message allows you to quickly understand the discrepancy saving you time and frustration.

More Info

You can find more about how to use valera and d42 (district42) schemas in your projects in the official documentation. With valera, data validation becomes less of a challenge and more of a strength.