Skip to content

Commit

Permalink
🛠 Fix code examples (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrevem authored Feb 16, 2024
1 parent d5ecf12 commit ad98484
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pip install pytest-schema

Here is a quick example of using **`schema`**:
```python
from pytest_schema import schema, exact, like
from pytest_schema import schema, exact_schema, like_schema

article_v1 = {
"id": int,
Expand All @@ -38,7 +38,7 @@ def test_article_v1_endpoint(test_client):
is in the correctly/expected format.
"""
response_v1 = test_client.get("/api/v1/article/1")
assert exact(article_v1) == response_v1
assert exact_schema(article_v1) == response_v1
# Same as:
# assert schema(article_v1) == response_v1

Expand All @@ -53,23 +53,24 @@ def test_article_v2_endpoint(test_client):
"""
response_v2 = test_client.get("/api/v2/article/1")

assert like(article_v1) == value
assert like_schema(article_v1) == response_v2

```
## Full Example

Here is a more complex example of using **`schema`**:

``` python
from pytest_schema import schema, And, Enum, Optional, Or, Regex
import pytest
from pytest_schema import schema, And, Enum, Optional, Or, Regex, SchemaError

# single user schema
user = {
# id must be int
"id": int,
# name must be type str
"name": str,
# email must be type str or nullable
# description must be type str or nullable
"description": Or(None, str),
# email valid str format
"email": Regex(r".*?@.*?\.[A-Za-z]{2,6}"),
Expand All @@ -78,7 +79,7 @@ user = {
# gender key is optional but must be str
Optional("gender"): str,
# role of enum values
"role": Enum(["user", "super-user", "admin"]),
"role": Enum("user", "super-user", "admin"),
# list of ids ref friends
"friends": [ int ],
# nested dict to valid as address
Expand All @@ -105,6 +106,7 @@ def test_users_endpoint():
{
"id": 2,
"name": "Sue",
"description": "Sue, the admin",
"age": 28,
"email": "[email protected]",
"gender": "female",
Expand All @@ -119,8 +121,9 @@ def test_users_endpoint():
},
# ✅ Valid
{
"id": 5
"id": 5,
"name": "Sam",
"description": "Sam, the user",
"age": 42,
"email": "[email protected]",
"role": "user",
Expand Down Expand Up @@ -170,4 +173,4 @@ If data is **`invalid`**, it will raise `SchemaError` exception.

## Supported validations

See: [keleshev/schema](https://github.com/keleshev/schema) full documentation.
See: [keleshev/schema](https://github.com/keleshev/schema) full documentation.

0 comments on commit ad98484

Please sign in to comment.