1 # Copyright 2023 highstreet technologies GmbH
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 Module containing a class for parameter validation
22 from typing import Any
27 class ParameterValidator:
29 Class validating the configuration as input for the generator.
32 __config_file: str = "config.json"
33 __configuration: dict = {}
34 __configuration_schema_file: str = (
35 os.path.dirname(os.path.realpath(__file__))
36 + "/model/jsonSchema/configuration.schema.json"
38 __config_schema: dict = {}
39 __error_message: str = ""
40 __is_valid: bool = False
43 def __init__(self, args: list[str]) -> None:
45 if len(self.args) > 1:
46 self.__config_file = args[1]
48 if os.path.isfile(self.__config_file) is False:
49 print("File", self.__config_file, "does not exist.")
51 with open(self.__config_file) as content:
52 self.__configuration = json.load(content)
54 if os.path.isfile(self.__configuration_schema_file) is False:
55 print("File", self.__configuration_schema_file, "does not exist.")
57 with open(self.__configuration_schema_file) as content:
58 self.__config_schema = json.load(content)
59 self.__is_valid = self.__is_json_valid(
60 self.__configuration, self.__config_schema
63 def configuration_file(self) -> str:
65 Getter for the configuration filename.
66 :return Filename (path) for the init configuration.
68 return self.__config_file
70 def configuration(self) -> dict[str, str | dict[str, int]]:
72 Getter for the configuration as input parameter.
73 :return Init configuration as dict.
75 return self.__configuration
77 def is_valid(self) -> bool:
79 Getter for the validation result.
80 :return Init configuration as dict.
82 return self.__is_valid
84 def error_message(self) -> str:
86 Getter for the error message after validation process or an
87 empty sting, when configuration is valid.
88 :return Error message as string.
90 return self.__error_message
95 self, json_data: dict[str, Any], json_schema: dict[str, Any]
98 Method validating json against a schema
101 jsonschema.validate(instance=json_data, schema=json_schema)
102 self.__error_message = ""
103 except jsonschema.exceptions.ValidationError as err:
104 self.__error_message = err