Skip to content

Commit c6a640b

Browse files
committed
Add optional JSON schema validation in JSONConfig constructor
1 parent 5c861bf commit c6a640b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

config/config/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ class JSONConfig(FileConfig):
325325
JSON configuration management class.
326326
This class provides methods to load, save, and manage configuration settings in JSON format.
327327
"""
328-
329-
def __init__(self, file_path: str):
328+
329+
def __init__(self, file_path: str, require_validation: bool = False):
330330
super().__init__(file_path)
331-
self.__validate()
331+
self.__validate(require_validation)
332332

333333
def _to_string(self) -> str:
334334
"""
@@ -347,7 +347,7 @@ def _from_string(self, config_string: str) -> None:
347347
if not isinstance(self._config, dict):
348348
raise ValueError("Invalid JSON format: expected a dictionary.")
349349

350-
def __validate(self):
350+
def __validate(self, required: bool):
351351
"""
352352
Validate the configuration against a JSON schema if provided in the config.
353353
The url to the schema must be provided in the "$schema" key of the config (https://json-schema.org/)
@@ -368,6 +368,8 @@ def __validate(self):
368368
_trace("Configuration validated successfully against schema.")
369369
else:
370370
_trace("No JSON schema provided for validation.")
371+
if required:
372+
raise ValueError("JSON schema validation is required but no '$schema' key found in configuration.")
371373

372374
class TOMLConfig(FileConfig):
373375
"""

0 commit comments

Comments
 (0)