-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjson_validate.py
64 lines (60 loc) · 2.26 KB
/
json_validate.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
import jsonschema
from jsonschema import validate, draft7_format_checker
subs_trial_generate_schema = {
"type": "object",
"properties": {
"experiment_id": {"type": "string"},
"operation": {
"enum": [
"EXP_TRIAL_GENERATE_SUBSEQUENT"
]
}
},
"required": ["experiment_id", "operation"],
"additionalProperties": False
}
trial_generate_schema = {
"type": "object",
"properties": {
"operation": {
"enum": [
"EXP_TRIAL_GENERATE_NEW"
]
},
"search_space":{
"type":"object",
"properties": {
"experiment_name": {"type": "string"},
"total_trials": {"type": "integer"},
"parallel_trials": {"type": "integer"},
"experiment_id": {"type": "string"},
"value_type": {"type": "string"},
"hpo_algo_impl": {"type": "string"},
"objective_function": {"type": "string"},
"tunables":{
"type":"array",
"value_type": {"type": "string"},
"name": {"type": "string"},
"lower_bound": {"type": "number"},
"upper_bound": {"type": "number"},
"step": {"type": "number"},
"choices":{"type":"array"}
},
"direction": {"type": "string"},
},
"required": ["experiment_name", "total_trials", "objective_function", "tunables", "direction"]
}
},
"required": ["search_space", "operation"],
"additionalProperties": False
}
def validate_trial_generate_json(trial_generate_json):
try:
if trial_generate_json["operation"] == "EXP_TRIAL_GENERATE_NEW":
validate(instance=trial_generate_json, schema=trial_generate_schema, format_checker=draft7_format_checker)
else:
validate(instance=trial_generate_json, schema=subs_trial_generate_schema, format_checker=draft7_format_checker)
except jsonschema.exceptions.ValidationError as err:
print(err)
return False
return True