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
25 class ParameterValidator:
27 Class validating the configuration as input for the generator.
30 __config_file: str = "config.json"
31 __configuration: dict = {}
32 __configuration_schema_file: str = (
33 os.path.dirname(os.path.realpath(__file__))
34 + "/../model/jsonSchema/configuration.schema.json"
36 __config_schema: dict = {}
37 __error_message: str = ""
38 __is_valid: bool = False
41 def __init__(self, args):
44 if len(self.args) > 1:
45 self.__config_file = args[1]
47 if os.path.isfile(self.__config_file) is False:
48 print("File", self.__config_file, "does not exist.")
50 with open(self.__config_file) as content:
51 self.__configuration = json.load(content)
53 if os.path.isfile(self.__configuration_schema_file) is False:
54 print("File", self.__configuration_schema_file, "does not exist.")
56 with open(self.__configuration_schema_file) as content:
57 self.__config_schema = json.load(content)
58 self.__is_valid = self.__is_json_valid(
59 self.__configuration, self.__config_schema
62 def configuration_file(self) -> str:
64 Getter for the configuration filename.
65 :return Filename (path) for the init configuration.
67 return self.__config_file
69 def configuration(self) -> dict[str, str | dict[str, int]]:
71 Getter for the configuration as input parameter.
72 :return Init configuration as dict.
74 return self.__configuration
76 def is_valid(self) -> bool:
78 Getter for the validation result.
79 :return Init configuration as dict.
81 return self.__is_valid
83 def error_message(self) -> str:
85 Getter for the error message after validation process or an empty sting,
86 when configuration is valid.
87 :return Error message as string.
89 return self.__error_message
93 def __is_json_valid(self, json_data, json_schema) -> bool:
95 Method validating json against a schema
98 jsonschema.validate(instance=json_data, schema=json_schema)
99 self.__error_message = ""
100 except jsonschema.exceptions.ValidationError as err:
101 self.__error_message = err