2222SOFTWARE.
2323"""
2424
25+ import abc
2526from . import errors , util
2627
2728
@@ -83,17 +84,20 @@ def check_source_key(entity: dict, source_keys: list) -> Key:
8384
8485 # check if current keys has at least, data or filter key
8586 if source_key is None :
86- raise errors .MissingKeyError (f"Missing { ', ' .join (map (str , source_keys ))} key(s)." )
87+ raise errors .MissingKeyError (
88+ f"Missing { ', ' .join (map (str , source_keys ))} key(s)." )
8789
8890 return source_key
8991
9092
9193def check_source_data (source_data , source_key : Key ):
9294 if not isinstance (source_data , dict ) and not isinstance (source_data , list ):
93- raise errors .InvalidTypeError (f"Invalid type_, { str (source_key )} should be either 'dict' or 'list'." )
95+ raise errors .InvalidTypeError (
96+ f"Invalid type_, { str (source_key )} should be either 'dict' or 'list'." )
9497
9598 if isinstance (source_data , list ) and len (source_data ) == 0 :
96- raise errors .EmptyDataError ("Empty list, 'data' or 'filter' list should not be empty." )
99+ raise errors .EmptyDataError (
100+ "Empty list, 'data' or 'filter' list should not be empty." )
97101
98102
99103def check_data_type (item , source_key : Key ):
@@ -102,37 +106,38 @@ def check_data_type(item, source_key: Key):
102106 f"Invalid type_, '{ source_key .name } ' should be '{ source_key .type_ } '" )
103107
104108
105- class SchemaValidator :
106- _source_keys = None
107- _ref_prefix = None
109+ class SchemaValidator (abc .ABC ):
110+
111+ def __init__ (self , source_keys , ref_prefix ):
112+ self ._source_keys = source_keys
113+ self ._ref_prefix = ref_prefix
108114
109115 @classmethod
110- def validate (cls , entities , ref_prefix = '!' , source_keys = None ):
111- if source_keys is None :
112- cls ._source_keys = [ Key . data (), Key . filter ()]
113- cls ._ref_prefix = ref_prefix
116+ def validate (cls , entities , source_keys , ref_prefix = '!' ):
117+ self = cls ( source_keys , ref_prefix )
118+ self ._source_keys = source_keys
119+ self ._ref_prefix = ref_prefix
114120
115- cls ._pre_validate (entities , entity_is_parent = True )
121+ self ._pre_validate (entities , entity_is_parent = True )
116122
117- @classmethod
118- def _pre_validate (cls , entities : dict , entity_is_parent = True ):
123+ def _pre_validate (self , entities : dict , entity_is_parent = True ):
119124 if not isinstance (entities , dict ) and not isinstance (entities , list ):
120- raise errors .InvalidTypeError ("Invalid type, should be list or dict" )
125+ raise errors .InvalidTypeError (
126+ "Invalid type, should be list or dict" )
121127 if len (entities ) == 0 :
122128 return
123129 if isinstance (entities , dict ):
124- return cls ._validate (entities , entity_is_parent )
130+ return self ._validate (entities , entity_is_parent )
125131 # iterate list
126132 for entity in entities :
127- cls ._pre_validate (entity , entity_is_parent )
133+ self ._pre_validate (entity , entity_is_parent )
128134
129- @classmethod
130- def _validate (cls , entity : dict , entity_is_parent = True ):
135+ def _validate (self , entity : dict , entity_is_parent = True ):
131136 check_max_length (entity )
132137 check_model_key (entity , entity_is_parent )
133138
134139 # get source key, either data or filter key
135- source_key = check_source_key (entity , cls ._source_keys )
140+ source_key = check_source_key (entity , self ._source_keys )
136141 source_data = entity [source_key ]
137142
138143 check_source_data (source_data , source_key )
@@ -141,13 +146,23 @@ def _validate(cls, entity: dict, entity_is_parent=True):
141146 for item in source_data :
142147 check_data_type (item , source_key )
143148 # check if item is a relationship attribute
144- cls .check_attributes (item )
149+ self .check_attributes (item )
145150 else :
146151 # source_data is dict
147152 # check if item is a relationship attribute
148- cls .check_attributes (source_data )
153+ self .check_attributes (source_data )
149154
150- @classmethod
151- def check_attributes (cls , source_data : dict ):
152- for _ , value in util .iter_ref_kwargs (source_data , cls ._ref_prefix ):
153- cls ._pre_validate (value , entity_is_parent = False )
155+ def check_attributes (self , source_data : dict ):
156+ for _ , value in util .iter_ref_kwargs (source_data , self ._ref_prefix ):
157+ self ._pre_validate (value , entity_is_parent = False )
158+
159+
160+ def validate (entities , ref_prefix = '!' ):
161+ SchemaValidator .validate (
162+ entities , ref_prefix = ref_prefix , source_keys = [Key .data ()])
163+
164+
165+ def hybrid_validate (entities , ref_prefix = '!' ):
166+ SchemaValidator .validate (entities ,
167+ ref_prefix = ref_prefix ,
168+ source_keys = [Key .data (), Key .filter ()])
0 commit comments