Skip to content

Commit 34b3202

Browse files
Changes made
1 parent 1fd2372 commit 34b3202

File tree

7 files changed

+44
-27
lines changed

7 files changed

+44
-27
lines changed

dpytools/config/config.py

-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99

1010

1111
class Config:
12-
1312
def __init__(self):
1413
self._properties_to_validate: List[BaseProperty] = []
1514

1615
@staticmethod
1716
def from_env(config_dict: Dict[str, Dict[str, Any]]) -> Config:
18-
1917
config = Config()
2018

2119
for env_var_name, value in config_dict.items():
22-
2320
value_for_property = os.environ.get(env_var_name, None)
2421
assert (
2522
value_for_property is not None
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .string import StringProperty
21
from .intproperty import IntegerProperty
2+
from .string import StringProperty

dpytools/config/properties/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ def name(self):
1414

1515
@name.setter
1616
def name(self, value):
17-
raise ValueError(f"Trying to change name property to value {value} but you cannot change a property name after instantiation.")
17+
raise ValueError(
18+
f"Trying to change name property to value {value} but you cannot change a property name after instantiation."
19+
)
1820

1921
@property
2022
def value(self):
2123
return self._value
2224

2325
@value.setter
2426
def value(self, value):
25-
raise ValueError(f"Trying to change value to {value} but you cannot change a property value after instantiation.")
27+
raise ValueError(
28+
f"Trying to change value to {value} but you cannot change a property value after instantiation."
29+
)
2630

2731
@abstractmethod
2832
def type_is_valid(self):
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Optional
21
from dataclasses import dataclass
2+
from typing import Optional
3+
34
from .base import BaseProperty
45

6+
57
@dataclass
68
class IntegerProperty(BaseProperty):
79
min_val: Optional[int]
@@ -10,12 +12,14 @@ class IntegerProperty(BaseProperty):
1012
def type_is_valid(self):
1113
"""
1214
Validate that the property looks like
13-
its of the correct type
15+
its of the correct type
1416
"""
1517
try:
1618
int(self._value)
1719
except Exception as err:
18-
raise Exception(f"Cannot cast {self._name} value {self._value} to integer.") from err
20+
raise Exception(
21+
f"Cannot cast {self._name} value {self._value} to integer."
22+
) from err
1923

2024
def secondary_validation(self):
2125
"""
@@ -26,7 +30,11 @@ def secondary_validation(self):
2630
raise ValueError(f"Integer value for {self._name} does not exist.")
2731

2832
if self.min_val and self._value < self.min_val:
29-
raise ValueError(f"Integer value for {self._name} is lower than allowed minimum.")
33+
raise ValueError(
34+
f"Integer value for {self._name} is lower than allowed minimum."
35+
)
3036

3137
if self.max_val and self._value > self.max_val:
32-
raise ValueError(f"Integer value for {self._name} is higher than allowed maximum.")
38+
raise ValueError(
39+
f"Integer value for {self._name} is higher than allowed maximum."
40+
)

dpytools/config/properties/string.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Optional
1+
import re
22
from dataclasses import dataclass
3-
from .base import BaseProperty
3+
from typing import Optional
44

5-
import re
5+
from .base import BaseProperty
66

77

88
@dataclass

dpytools/validation/json/validation.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
def validate_json_schema(
1111
schema_path: Union[Path, str],
1212
data_dict: Optional[Dict] = None,
13-
data_path: Union[Path, str, None] = None,
14-
error_msg: Optional[str] = "",
15-
indent: Optional[int] = 2,
13+
data_path: Optional[Union[Path, str]] = None,
14+
error_msg: Optional[str] = None,
15+
indent: Optional[int] = None,
1616
):
1717
"""
18-
Validate metadata.json files against schema.
18+
Validate a JSON file against a schema.
1919
2020
Either `data_dict` or `data_path` must be provided.
2121
22-
`error_msg` and `indent` are used to format the error message if validation fails.
22+
`error_msg` and `indent` can be used to format the error message if validation fails.
2323
"""
2424
# Confirm that *either* `data_dict` *or* `data_path` has been provided, otherwise raise ValueError
2525
if data_dict and data_path:
@@ -48,15 +48,19 @@ def validate_json_schema(
4848
# Load data to be validated
4949
if data_dict:
5050
if not isinstance(data_dict, Dict):
51-
raise ValueError("Invalid data format")
51+
raise ValueError(
52+
"Invalid data format, `data_dict` should be a Python dictionary"
53+
)
5254
data_to_validate = data_dict
5355

5456
if data_path:
5557
# Convert `data_path` to pathlib.Path
5658
if isinstance(data_path, str):
5759
data_path = Path(data_path).absolute()
5860
if not isinstance(data_path, Path):
59-
raise ValueError("Invalid data format")
61+
raise ValueError(
62+
"Invalid data format, `data_path` should be a pathlib.Path or string of file location"
63+
)
6064
# Check `data_path` exists
6165
if not data_path.exists():
6266
raise ValueError(f"Data path '{data_path}' does not exist")
@@ -82,5 +86,6 @@ def validate_json_schema(
8286
JSON data:
8387
{json.dumps(data_to_validate, indent=indent)}
8488
"""
89+
print(formatted_msg)
8590
raise ValidationError(formatted_msg) from err
8691
raise err

tests/test_json_validation.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def test_validate_json_schema_data_path():
1515
validate_json_schema(
1616
schema_path=pipeline_config_schema,
1717
data_path=pipeline_config,
18-
error_msg="Validating pipeline_config.json",
1918
)
2019
is None
2120
)
@@ -38,7 +37,6 @@ def test_validate_json_schema_data_dict():
3837
validate_json_schema(
3938
schema_path=pipeline_config_schema,
4039
data_dict=pipeline_config,
41-
error_msg="Validating pipeline_config dict",
4240
)
4341
is None
4442
)
@@ -88,15 +86,18 @@ def test_validate_json_schema_no_data_dict_or_data_path():
8886

8987
def test_validate_json_schema_invalid_data_path_format():
9088
"""
91-
Raise ValueError if data_path is not a file path
89+
Raise ValueError if data_path is not a string or file path
9290
"""
9391
pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
9492
pipeline_config = ["Invalid", "data", "format"]
9593
with pytest.raises(ValueError) as err:
9694
validate_json_schema(
9795
schema_path=pipeline_config_schema, data_path=pipeline_config
9896
)
99-
assert "Invalid data format" in str(err.value)
97+
assert (
98+
"Invalid data format, `data_path` should be a pathlib.Path or string of file location"
99+
in str(err.value)
100+
)
100101

101102

102103
def test_validate_json_schema_invalid_data_dict_format():
@@ -109,12 +110,14 @@ def test_validate_json_schema_invalid_data_dict_format():
109110
validate_json_schema(
110111
schema_path=pipeline_config_schema, data_dict=pipeline_config
111112
)
112-
assert "Invalid data format" in str(err.value)
113+
assert "Invalid data format, `data_dict` should be a Python dictionary" in str(
114+
err.value
115+
)
113116

114117

115118
def test_validate_json_schema_url():
116119
"""
117-
Raise NotImplementedError if schema path is a URL (i.e. not a local file)
120+
Raise NotImplementedError if `schema_path` is a URL (i.e. not a local file)
118121
"""
119122
pipeline_config_schema = "http://example.org"
120123
pipeline_config = "tests/test_cases/pipeline_config.json"

0 commit comments

Comments
 (0)