-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptional.py
More file actions
46 lines (35 loc) · 989 Bytes
/
Copy pathoptional.py
File metadata and controls
46 lines (35 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from simple_schema_validator import schema_validator, types
def validate(schema, data):
validation = schema_validator(schema, data)
if not validation:
print(f'Keys in data, but not in schema: {validation.additional_keys}')
print(f'Keys in schema, but not in data: {validation.missing_keys}')
print(f'Keys with different type from schema {validation.type_errors}')
else:
print('Valid.')
def main():
data_1 = {
'type': 'data',
'message': None,
'data': {
'foo': 'bar'
}
}
data_2 = {
'type': 'info',
'message': 'Some info',
'data': None
}
schema = {
'type': str,
'message': types.Optional[str],
'data': types.Optional[{
'foo': str
}]
}
print('Validating data_1 ...')
validate(schema, data_1)
print('Validating data_2 ...')
validate(schema, data_2)
if __name__ == '__main__':
main()