Skip to content

Commit 7cff99b

Browse files
committed
better json schemas
1 parent 72ba28f commit 7cff99b

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

pypeec/lib_check/check_data_options.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ def check_tag_list(data_check, tag_list):
1414
Check the list of plots to be shown.
1515
"""
1616

17-
# allowed tags
18-
tag_allowed = list(data_check.keys())
19-
20-
# check fields
17+
# check tag_list with a schema
2118
schema = {
2219
"type": ["null", "array"],
2320
"items": {
2421
"type": "string",
2522
"minLength": 1,
2623
},
2724
}
28-
29-
# validate base schema
3025
scisave.validate_schema(tag_list, schema)
3126

32-
# check tag
27+
# get a list with the allowed tags
28+
tag_allowed = list(data_check.keys())
29+
30+
# check that the provided tags a subset of the allowed tags
3331
if tag_list is not None:
3432
for tag in tag_list:
3533
if tag not in tag_allowed:
@@ -41,14 +39,14 @@ def check_plot_options(plot_mode, folder, name):
4139
Check the plot mode (display or not the plots).
4240
"""
4341

44-
# check plot_mode
42+
# check plot_mode with a schema
4543
schema = {
4644
"type": ["null", "string"],
4745
"enum": [None, "qt", "nb_int", "nb_std", "png", "vtk", "debug"],
4846
}
4947
scisave.validate_schema(plot_mode, schema)
5048

51-
# check folder and name
49+
# check folder and name with a schema
5250
schema = {
5351
"type": ["null", "string"],
5452
"minLength": 1,

pypeec/main.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
LOGGER = scilogger.get_logger(__name__, "pypeec")
2121

2222

23-
def _create_data(datatype, timestamp, data):
23+
def _create_data(layout, timestamp, data):
2424
"""
2525
Create an output data structure.
2626
"""
@@ -32,10 +32,10 @@ def _create_data(datatype, timestamp, data):
3232
meta = {
3333
"name": pypeec.__name__,
3434
"version": pypeec.__version__,
35-
"datatype": datatype,
35+
"layout": layout,
36+
"date": date,
3637
"duration": duration,
3738
"seconds": seconds,
38-
"date": date,
3939
}
4040

4141
# assemble the output data
@@ -47,35 +47,59 @@ def _create_data(datatype, timestamp, data):
4747
return data_out
4848

4949

50-
def _load_data(datatype_out, data_out):
50+
def _load_data(layout_out, data_out):
5151
"""
5252
Load an output data structure.
5353
"""
5454

55-
# check fields
55+
# check the output data
5656
schema = {
5757
"type": "object",
5858
"required": [
5959
"meta",
6060
"data",
6161
],
62+
"properties": {
63+
"meta" : {"type": "object"},
64+
"data": {"type": "object"},
65+
}
6266
}
63-
64-
# validate base schema
6567
scisave.validate_schema(data_out, schema)
6668

6769
# extract the output data
6870
meta = data_out["meta"]
6971
data = data_out["data"]
7072

73+
# check the metata
74+
schema = {
75+
"type": "object",
76+
"required": [
77+
"name",
78+
"version",
79+
"layout",
80+
"seconds",
81+
"duration",
82+
"date",
83+
],
84+
"properties": {
85+
"meta" : {"type": "string", "minLength": 1},
86+
"version" : {"type": "string", "minLength": 1},
87+
"layout": {"type": "string", "minLength": 1},
88+
"date": {"type": "string", "minLength": 1},
89+
"duration": {"type": "string", "minLength": 1},
90+
"seconds": {"type": "number", "minimum": 0},
91+
}
92+
}
93+
scisave.validate_schema(meta, schema)
94+
7195
# extract meta
7296
name = meta["name"]
7397
version = meta["version"]
74-
datatype = meta["datatype"]
98+
layout = meta["layout"]
7599

76-
# check that the datatype is correct
77-
if datatype != datatype_out:
78-
raise ValueError("invalid data format: %s" % datatype)
100+
# check that the layout is correct
101+
if layout != layout_out:
102+
raise ValueError("invalid data format: %s" % layout)
79103

80104
# display a warning in case of a version mismatch
81105
if (name != pypeec.__name__) or (version != pypeec.__version__):

0 commit comments

Comments
 (0)