Skip to content

Commit 4a52380

Browse files
updated file with some added text for understanding.
1 parent ed5532c commit 4a52380

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tests/schema_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import json
2-
32
import requests
43
from assertpy import assert_that, soft_assertions
54
from cerberus import Validator
6-
75
from config import BASE_URI
86

7+
'''
8+
Chapter 6 - API Response Schema Validation
9+
https://docs.python-cerberus.org/en/stable/
10+
So, the main CRUDs of the Cerberus is actually the Schema and it's a disctionary like structure as we can see below,
11+
that they define. Cerberus defines its own sort of mini language to define the schemas as well as their types.
12+
13+
'''
914
schema = {
1015
"fname": {'type': 'string'},
1116
"lname": {'type': 'string'},
@@ -17,7 +22,12 @@
1722
def test_read_one_operation_has_expected_schema():
1823
response = requests.get(f'{BASE_URI}/1')
1924
person = json.loads(response.text)
20-
25+
'''
26+
Below is the main logic, how Cerberus works. We create an object of the Validator class and passing the 'schema'
27+
dictionary that we've already created. Notice, the second argument 'require_all=True' , this is a global flag that
28+
we can set for this particular 'schema' specifying that all the keys in this response are required. So, if for
29+
some reason our API tomorrow skips any field we will get an error.
30+
'''
2131
validator = Validator(schema, require_all=True)
2232
is_valid = validator.validate(person)
2333

0 commit comments

Comments
 (0)