-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateData.py
More file actions
37 lines (30 loc) · 1.36 KB
/
ValidateData.py
File metadata and controls
37 lines (30 loc) · 1.36 KB
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
#!/usr/bin/env python
import traceback
def validate(data, MessageFormats):
'''Returns True if data fits MessageFormat, else returns an error string.
data is a JSON-parsed dictionary. MessageFormats is a dictionary keyed by event ID.
The value is a dictionary where the key is the required data field, and the value is either
a function that returns true on a correctly formatted data or a data type. '''
try:
if 'EventID' not in data:
return 'No EventID.'
MessageFormat = MessageFormats[data['EventID']]
check = validate_message_format(data, MessageFormat)
if check is not True:
return check
except Exception as e:
return 'Message malformed: ' + str(e) + ' ' + traceback.format_exc()
return True
def validate_message_format(data, MessageFormat):
# Validator is either the expected variable type or a function that returns true if
# the variable is correctly formed.
for key, validator in MessageFormat.items():
# validator is a validating function.
try:
if not validator(data_to_validate = data[key]):
return "Key '%s' is not formatted correctly."%(key,)
# validator is a variable type
except:
if not type(data[key]) == validator:
return 'Key %s is not formatted correctly.'%(key,)
return True