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 Dict, Union
26 class ParameterValidator:
28 Class validating the configuration as input for the generator.
31 __config_file: str = "config.json"
32 __configuration: dict = {}
33 __configuration_schema_file: str = os.path.dirname(os.path.realpath(
34 __file__)) + "/../model/jsonSchema/configuration.schema.json"
35 __config_schema: dict = {}
36 __error_messsage: str = ""
37 __is_valid: bool = False
40 def __init__(self, args):
43 if len(self.args) > 1:
44 self.__config_file = args[1]
46 if os.path.isfile(self.__config_file) is False:
47 print("File", self.__config_file, "does not exist.")
49 with open(self.__config_file) as content:
50 self.__configuration = json.load(content)
52 if os.path.isfile(self.__configuration_schema_file) is False:
53 print("File", self.__configuration_schema_file, "does not exist.")
55 with open(self.__configuration_schema_file) as content:
56 self.__config_schema = json.load(content)
57 self.__is_valid = self.__is_json_valid(
58 self.__configuration, self.__config_schema)
60 def configuration_file(self) -> str:
62 Getter for the configuration filename.
63 :return Filename (path) for the init configuration.
65 return self.__config_file
67 def configuration(self) -> Dict[str, Union[str, Dict[str, int]]]:
69 Getter for the configuration as input parameter.
70 :return Init configuration as Dict.
72 return self.__configuration
74 def is_valid(self) -> bool:
76 Getter for the validation result.
77 :return Init configuration as Dict.
79 return self.__is_valid
81 def error_message(self) -> str:
83 Getter for the error message after validation process or an empty sting,
84 when configuration is valid.
85 :return Errormessage as string.
87 return self.__error_messsage
91 def __is_json_valid(self, json_data, json_schema) -> bool:
93 Method validating json against a schema
96 jsonschema.validate(instance=json_data, schema=json_schema)
97 self.__error_messsage = ""
98 except jsonschema.exceptions.ValidationError as err:
99 self.__error_messsage = err