-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
118 lines (87 loc) · 2.47 KB
/
schema.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Schema used in message passing between components and validators used
by the comms module (and others) for asserting the structural correctness of
the messaging.
These schema are not the protocol used by the comms module, all messages
passed in that protocol are validated by these schema.
See the documentation for jsonschema to understand the syntax.
"""
from jsonschema import validate
permission_schema = {
"$id": "/schemas/permission",
"type": "object",
"properties": {
"perm": {"type": "string"},
"context": {"type": "object"}
},
"required": ["perm"]
}
request_schema = {
"$id": "/schemas/request",
"type": "object",
"properties": {
"source_id": {"type": "string"},
"target_id": {"type": "string"},
"permissions": {
"type": "array",
"items": permission_schema,
"minContains": 1,
}
},
"required": ["source_id", "target_id", "permissions"]
}
response_schema = {
"$id": "/schemas/response",
"type": "object",
"properties": {
"source_id": {"type": "string"},
"target_id": {"type": "string"},
"context": {"type": "object"},
"code": {"type": "integer"},
"msg": {"type": "string"}
},
"required": ["source_id", "target_id", "code", "msg"]
}
grant_schema = {
"$id": "/schemas/grant",
"type": "object",
"properties": {
"source_id": {"type": "string"},
"target_id": {"type": "string"},
"perm": permission_schema,
"grant": {"type": "boolean"},
"context": {"type": "object"}
},
"required": ["perm", "grant"]
}
error_schema = {
"$id": "/schemas/error",
"type": "object",
"properties": {
"error_code": {"type": "integer"},
"error_msg": {"type": "string"}
},
"required": ["error_code", "error_msg"]
}
event_schema = {
"$id": "/schemas/event",
"type": "object",
"properties": {
"src_id": {"type": "string"},
"event": {"type": "string"},
"context": {"type": "object"}
},
"required": ["src_id", "event"]
}
def validate_permission(data):
validate(data, permission_schema)
def validate_request(data):
validate(data, request_schema)
def validate_grant(data):
validate(data, grant_schema)
def validate_response(data):
validate(data, response_schema)
def validate_error(data):
validate(data, error_schema)
def validate_event(data):
validate(data, event_schema)