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
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 = (
34 os.path.dirname(os.path.realpath(__file__))
35 + "/model/jsonSchema/configuration.schema.json"
37 __config_schema: dict = {}
38 __error_message: str = ""
39 __is_valid: bool = False
42 def __init__(self, args):
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 empty sting,
87 when configuration is valid.
88 :return Error message as string.
90 return self.__error_message
94 def __is_json_valid(self, json_data, json_schema) -> bool:
96 Method validating json against a schema
99 jsonschema.validate(instance=json_data, schema=json_schema)
100 self.__error_message = ""
101 except jsonschema.exceptions.ValidationError as err:
102 self.__error_message = err