33import re
44from dataclasses import dataclass , field , Field
55from enum import Enum
6- from typing import Any , List
6+ import traceback
7+ from typing import Any , List , Optional
78
89from .epc import (
910 get_obj_identifier ,
1213from .introspection import (
1314 get_class_fields ,
1415 get_object_attribute ,
16+ is_primitive ,
1517 search_attribute_matching_type_with_path ,
1618 get_object_attribute_no_verif ,
1719 get_object_attribute_rgx ,
2022 get_obj_version ,
2123 get_content_type_from_class ,
2224 get_qualified_type_from_class ,
23- is_enum , get_object_uri ,
25+ is_enum ,
26+ get_object_uri ,
2427)
2528
29+
2630class ErrorType (Enum ):
2731 CRITICAL = "critical"
2832 DEBUG = "debug"
@@ -52,13 +56,16 @@ class ValidationObjectError(ValidationError):
5256
5357 target_obj : Any = field (default = None )
5458
55- attribute_dot_path : str = field (default = None )
59+ attribute_dot_path : Optional [ str ] = field (default = None )
5660
5761 def __str__ (self ):
5862 return f"{ ValidationError .__str__ (self )} \n \t { get_obj_identifier (self .target_obj )} : '{ self .attribute_dot_path } '"
5963
6064 def toJson (self ):
61- return super ().toJson () | {"target_obj" : str (get_object_uri (self .target_obj )), "attribute_dot_path" : self .attribute_dot_path }
65+ return super ().toJson () | {
66+ "target_obj" : str (get_object_uri (self .target_obj )),
67+ "attribute_dot_path" : self .attribute_dot_path ,
68+ }
6269
6370
6471@dataclass
@@ -69,7 +76,7 @@ def __str__(self):
6976
7077@dataclass
7178class MissingEntityError (ValidationObjectError ):
72- missing_uuid : str = field (default = None )
79+ missing_uuid : Optional [ str ] = field (default = None )
7380
7481 def __str__ (self ):
7582 return f"{ ValidationError .__str__ (self )} \n \t Missing entity in { get_obj_identifier (self .target_obj )} at path '{ self .attribute_dot_path } '. Missing entity uuid: { self .missing_uuid } "
@@ -122,8 +129,8 @@ def dor_validation(energyml_objects: List[Any]) -> List[ValidationError]:
122129 error_type = ErrorType .CRITICAL ,
123130 target_obj = obj ,
124131 attribute_dot_path = dor_path ,
125- missing_uuid = dor_uuid
126- # msg=f"[DOR ERR] has wrong information. Unkown object with uuid '{dor_uuid}'",
132+ missing_uuid = dor_uuid ,
133+ msg = f"[DOR ERR] has wrong information. Unknown object with uuid '{ dor_uuid } '" ,
127134 )
128135 )
129136 else :
@@ -133,7 +140,7 @@ def dor_validation(energyml_objects: List[Any]) -> List[ValidationError]:
133140 error_type = ErrorType .CRITICAL ,
134141 target_obj = obj ,
135142 attribute_dot_path = dor_path ,
136- msg = f"[DOR ERR] has wrong information. Unkown object version '{ dor_version } '. "
143+ msg = f"[DOR ERR] has wrong information. Unknown object version '{ dor_version } '. "
137144 f"Version must be one of { accessible_version } " ,
138145 )
139146 )
@@ -201,6 +208,8 @@ def _patterns_validation(obj: Any, root_obj: Any, current_attribute_dot_path: st
201208 """
202209 error_list = []
203210
211+ if is_primitive (obj ):
212+ return error_list
204213 if isinstance (obj , list ):
205214 cpt = 0
206215 for val in obj :
@@ -235,82 +244,105 @@ def validate_attribute(value: Any, root_obj: Any, att_field: Field, path: str) -
235244 attribute_dot_path = path ,
236245 )
237246 )
247+ elif isinstance (att_field , str ):
248+ errs .append (
249+ ValidationObjectError (
250+ error_type = ErrorType .WARNING ,
251+ target_obj = root_obj ,
252+ attribute_dot_path = path ,
253+ msg = f"Attribute '{ att_field } ' is a string but got value '{ value } '" ,
254+ )
255+ )
238256 elif not is_enum (value ): # sometimes enums values fails the validation
239- min_length = att_field .metadata .get ("min_length" , None )
240- max_length = att_field .metadata .get ("max_length" , None )
241- pattern = att_field .metadata .get ("pattern" , None )
242- min_occurs = att_field .metadata .get ("min_occurs" , None )
243- min_inclusive = att_field .metadata .get ("min_inclusive" , None )
244- # white_space
245-
246- if max_length is not None :
247- length = len (value )
248- if length > max_length :
249- errs .append (
250- ValidationObjectError (
251- error_type = ErrorType .CRITICAL ,
252- target_obj = root_obj ,
253- attribute_dot_path = path ,
254- msg = f"Max length was { max_length } but found { length } " ,
255- )
256- )
257-
258- if min_length is not None :
259- length = len (value )
260- if length < min_length :
261- errs .append (
262- ValidationObjectError (
263- error_type = ErrorType .CRITICAL ,
264- target_obj = root_obj ,
265- attribute_dot_path = path ,
266- msg = f"Max length was { min_length } but found { length } " ,
257+ try :
258+ min_length = att_field .metadata .get ("min_length" , None )
259+ max_length = att_field .metadata .get ("max_length" , None )
260+ pattern = att_field .metadata .get ("pattern" , None )
261+ min_occurs = att_field .metadata .get ("min_occurs" , None )
262+ min_inclusive = att_field .metadata .get ("min_inclusive" , None )
263+ # white_space
264+
265+ if max_length is not None :
266+ length = len (value )
267+ if length > max_length :
268+ errs .append (
269+ ValidationObjectError (
270+ msg = f"Max length was { max_length } but found { length } " ,
271+ error_type = ErrorType .CRITICAL ,
272+ target_obj = root_obj ,
273+ attribute_dot_path = path ,
274+ )
267275 )
268- )
269276
270- if min_occurs is not None :
271- if isinstance (value , list ) and min_occurs > len (value ):
272- errs .append (
273- ValidationObjectError (
274- error_type = ErrorType .CRITICAL ,
275- target_obj = root_obj ,
276- attribute_dot_path = path ,
277- msg = f"Min occurs was { min_occurs } but found { len (value )} " ,
277+ if min_length is not None :
278+ length = len (value )
279+ if length < min_length :
280+ errs .append (
281+ ValidationObjectError (
282+ msg = f"Max length was { min_length } but found { length } " ,
283+ error_type = ErrorType .CRITICAL ,
284+ target_obj = root_obj ,
285+ attribute_dot_path = path ,
286+ )
278287 )
279- )
280288
281- if min_inclusive is not None :
282- potential_err = ValidationObjectError (
283- error_type = ErrorType .CRITICAL ,
284- target_obj = root_obj ,
285- attribute_dot_path = path ,
286- msg = f"Min occurs was { min_inclusive } but found { value } " ,
287- )
288- if isinstance (value , list ):
289- for val in value :
290- if (isinstance (val , str ) and len (val ) > min_inclusive ) or (
291- (isinstance (val , int ) or isinstance (val , float )) and val > min_inclusive
292- ):
293- errs .append (potential_err )
294-
295- if pattern is not None :
296- if not isinstance (value , list ):
297- testing_value_list = [value ]
298- else :
299- testing_value_list = value
300-
301- for v in testing_value_list :
302- if is_enum (v ):
303- v = v .value
304- if re .match (pattern , v ) is None :
289+ if min_occurs is not None :
290+ if isinstance (value , list ) and min_occurs > len (value ):
305291 errs .append (
306292 ValidationObjectError (
293+ msg = f"Min occurs was { min_occurs } but found { len (value )} " ,
307294 error_type = ErrorType .CRITICAL ,
308295 target_obj = root_obj ,
309296 attribute_dot_path = path ,
310- msg = f"Pattern error. Value '{ v } ' was supposed to respect pattern '{ pattern } '" ,
311297 )
312298 )
313299
300+ if min_inclusive is not None :
301+ potential_err = ValidationObjectError (
302+ msg = f"Min occurs was { min_inclusive } but found { value } " ,
303+ error_type = ErrorType .CRITICAL ,
304+ target_obj = root_obj ,
305+ attribute_dot_path = path ,
306+ )
307+ if isinstance (value , list ):
308+ for val in value :
309+ if (isinstance (val , str ) and len (val ) > min_inclusive ) or (
310+ (isinstance (val , int ) or isinstance (val , float )) and val > min_inclusive
311+ ):
312+ errs .append (potential_err )
313+
314+ if pattern is not None :
315+ if not isinstance (value , list ):
316+ testing_value_list = [value ]
317+ else :
318+ testing_value_list = value
319+
320+ for v in testing_value_list :
321+ if is_enum (v ):
322+ v = v .value
323+ if re .match (pattern , v ) is None :
324+ errs .append (
325+ ValidationObjectError (
326+ msg = f"Pattern error. Value '{ v } ' was supposed to respect pattern '{ pattern } '" ,
327+ error_type = ErrorType .CRITICAL ,
328+ target_obj = root_obj ,
329+ attribute_dot_path = path ,
330+ )
331+ )
332+ except Exception as e :
333+ print (f"Error while validating attribute '{ att_field } ' with value '{ value } ': { str (e )} for { path } " )
334+ errs .append (
335+ ValidationObjectError (
336+ msg = f"Error while validating attribute '{ att_field } ' with value '{ value } ': { str (e )} " ,
337+ error_type = ErrorType .CRITICAL ,
338+ target_obj = root_obj ,
339+ attribute_dot_path = path ,
340+ )
341+ )
342+ traceback .print_exc ()
343+ exit (0 )
344+ return errs
345+
314346 return errs + _patterns_validation (
315347 obj = value ,
316348 root_obj = root_obj ,
0 commit comments