|
7 | 7 | import json |
8 | 8 | import os |
9 | 9 | from yaml import safe_load, safe_dump |
10 | | -from jsonschema import validate, Draft202012Validator |
| 10 | +from jsonschema import validate, Draft202012Validator, ValidationError |
11 | 11 | from pod_porter.render.render import Render |
12 | 12 | from pod_porter.util.directories import create_temp_working_directory, delete_temp_working_directory |
13 | 13 | from pod_porter.util.file_read_write import write_file, extract_tar_gz_file |
14 | 14 | from pod_porter.util.schemas import MapSchema |
15 | 15 |
|
16 | 16 | JSON_SCHEMA_FORMAT_CHECKERS = Draft202012Validator.FORMAT_CHECKER |
| 17 | +COMPOSE_SPEC: Path = Path(__file__).parent.joinpath("render").joinpath("compose_spec").joinpath("compose-spec.json") |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class _PorterMap: # pylint: disable=too-many-instance-attributes |
@@ -72,7 +73,8 @@ def __repr__(self) -> str: |
72 | 73 | """ |
73 | 74 | return f'PorterMap(path="{self._path}", release_name="{self._release_name}")' |
74 | 75 |
|
75 | | - def validate_value_json_schema(self, values_data: dict, values_path: str) -> None: |
| 76 | + @staticmethod |
| 77 | + def validate_value_json_schema(values_data: dict, values_path: str) -> None: |
76 | 78 | """Validate data against a JSON schema. |
77 | 79 |
|
78 | 80 | :type values_data: dict |
@@ -341,8 +343,6 @@ class PorterMapsRunner: # pylint: disable=too-many-instance-attributes |
341 | 343 | :returns: Nothing |
342 | 344 | """ |
343 | 345 |
|
344 | | - COMPOSE_SPEC: Path = Path(__file__).parent.joinpath("render").joinpath("compose_spec").joinpath("compose-spec.json") |
345 | | - |
346 | 346 | def __init__(self, path: str, release_name: Optional[str] = None, values_override: Optional[str] = None) -> None: |
347 | 347 | self._path = path |
348 | 348 | self._release_name = release_name or "release-name" |
@@ -421,22 +421,32 @@ def _collect_maps(self) -> List[Dict]: |
421 | 421 |
|
422 | 422 | return maps |
423 | 423 |
|
424 | | - def validate_compose_json_schema(self, compose_data: dict) -> None: |
| 424 | + @staticmethod |
| 425 | + def validate_compose_json_schema(compose_data: dict) -> None: |
425 | 426 | """Validate the created compose data against the compose spec JSON schema |
| 427 | + https://github.com/compose-spec/compose-spec/blob/main/schema/compose-spec.json |
426 | 428 |
|
427 | 429 | :type compose_data: dict |
428 | 430 | :param compose_data: The compose data |
429 | 431 |
|
430 | 432 | :rtype: None |
431 | 433 | :returns: Nothing it validates the JSON data against the JSON schema |
432 | 434 | """ |
433 | | - if not self.COMPOSE_SPEC.is_file(): |
434 | | - raise FileNotFoundError(f"compose file json spec not found {self.COMPOSE_SPEC.as_posix()}") |
| 435 | + if not COMPOSE_SPEC.is_file(): |
| 436 | + raise FileNotFoundError(f"compose file json spec not found {COMPOSE_SPEC.as_posix()}") |
435 | 437 |
|
436 | | - with open(self.COMPOSE_SPEC.as_posix(), "r", encoding="utf-8") as compose_schema_file: |
| 438 | + with open(COMPOSE_SPEC.as_posix(), "r", encoding="utf-8") as compose_schema_file: |
437 | 439 | compose_json_schema = json.load(compose_schema_file) |
438 | 440 |
|
439 | | - validate(instance=compose_data, schema=compose_json_schema, format_checker=JSON_SCHEMA_FORMAT_CHECKERS) |
| 441 | + try: |
| 442 | + validate(instance=compose_data, schema=compose_json_schema, format_checker=JSON_SCHEMA_FORMAT_CHECKERS) |
| 443 | + |
| 444 | + except ValidationError as err: |
| 445 | + readable_error = ( |
| 446 | + f"Error in validating compose file!\nArgument Error: {err.args}\n" |
| 447 | + f"In Schema Path: {'.'.join(err.absolute_path)}" |
| 448 | + ) |
| 449 | + raise ValueError(readable_error) |
440 | 450 |
|
441 | 451 | def render_compose(self) -> str: |
442 | 452 | """Render the compose file |
|
0 commit comments