File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 1
1
import json
2
-
3
2
import requests
4
3
from assertpy import assert_that , soft_assertions
5
4
from cerberus import Validator
6
-
7
5
from config import BASE_URI
8
6
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
+ '''
9
14
schema = {
10
15
"fname" : {'type' : 'string' },
11
16
"lname" : {'type' : 'string' },
17
22
def test_read_one_operation_has_expected_schema ():
18
23
response = requests .get (f'{ BASE_URI } /1' )
19
24
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
+ '''
21
31
validator = Validator (schema , require_all = True )
22
32
is_valid = validator .validate (person )
23
33
You can’t perform that action at this time.
0 commit comments