1010from flask_inputfilter .Condition import BaseCondition
1111from flask_inputfilter .Exception import ValidationError
1212from flask_inputfilter .Filter import BaseFilter
13- from flask_inputfilter .Mixin import ExternalApiMixin
13+ from flask_inputfilter .Mixin import ExternalApiMixin , FieldMixin
1414from flask_inputfilter .Model import ExternalApiConfig , FieldModel
1515from flask_inputfilter .Validator import BaseValidator
1616
@@ -197,17 +197,17 @@ def validateData(
197197 value = self .validated_data .get (copy )
198198
199199 if external_api :
200- value = ExternalApiMixin () .callExternalApi (
200+ value = ExternalApiMixin .callExternalApi (
201201 external_api , fallback , self .validated_data
202202 )
203203
204- value = InputFilter .applyFilters (filters , value )
204+ value = FieldMixin .applyFilters (filters , value )
205205 value = (
206- InputFilter .validateField (validators , fallback , value )
206+ FieldMixin .validateField (validators , fallback , value )
207207 or value
208208 )
209- value = InputFilter .applySteps (steps , fallback , value ) or value
210- value = InputFilter .checkForRequired (
209+ value = FieldMixin .applySteps (steps , fallback , value ) or value
210+ value = FieldMixin .checkForRequired (
211211 field_name , required , default , fallback , value
212212 )
213213
@@ -217,7 +217,7 @@ def validateData(
217217 errors [field_name ] = str (e )
218218
219219 try :
220- InputFilter .checkConditions (self .conditions , self .validated_data )
220+ FieldMixin .checkConditions (self .conditions , self .validated_data )
221221 except ValidationError as e :
222222 errors ["_condition" ] = str (e )
223223
@@ -252,30 +252,6 @@ def getConditions(self) -> List[BaseCondition]:
252252 """
253253 return self .conditions
254254
255- @staticmethod
256- def checkConditions (
257- conditions : List [BaseCondition ], validated_data : Dict [str , Any ]
258- ) -> None :
259- """
260- Checks if all conditions are met.
261-
262- This method iterates through all registered conditions and checks
263- if they are satisfied based on the provided validated data. If any
264- condition is not met, a ValidationError is raised with an appropriate
265- message indicating which condition failed.
266-
267- Args:
268- conditions (List[BaseCondition]):
269- A list of conditions to be checked against the validated
270- validated_data (Dict[str, Any]):
271- The validated data to check against the conditions.
272- """
273- for condition in conditions :
274- if not condition .check (validated_data ):
275- raise ValidationError (
276- f"Condition '{ condition .__class__ .__name__ } ' not met."
277- )
278-
279255 def setData (self , data : Dict [str , Any ]) -> None :
280256 """
281257 Filters and sets the provided data into the object's internal
@@ -291,7 +267,7 @@ def setData(self, data: Dict[str, Any]) -> None:
291267 self .data = {}
292268 for field_name , field_value in data .items ():
293269 if field_name in self .fields :
294- field_value = InputFilter .applyFilters (
270+ field_value = FieldMixin .applyFilters (
295271 filters = self .fields [field_name ].filters ,
296272 value = field_value ,
297273 )
@@ -349,7 +325,7 @@ def getRawValue(self, name: str) -> Any:
349325 Returns:
350326 Any: The raw value associated with the provided key.
351327 """
352- return self .data .get (name ) if name in self . data else None
328+ return self .data .get (name )
353329
354330 def getRawValues (self ) -> Dict [str , Any ]:
355331 """
@@ -634,98 +610,6 @@ def replace(
634610 copy = copy ,
635611 )
636612
637- @staticmethod
638- def applySteps (
639- steps : List [Union [BaseFilter , BaseValidator ]],
640- fallback : Any ,
641- value : Any ,
642- ) -> Any :
643- """
644- Apply multiple filters and validators in a specific order.
645-
646- This method processes a given value by sequentially applying a list of
647- filters and validators. Filters modify the value, while validators
648- ensure the value meets specific criteria. If a validation error occurs
649- and a fallback value is provided, the fallback is returned. Otherwise,
650- the validation error is raised.
651-
652- Args:
653- steps (List[Union[BaseFilter, BaseValidator]]):
654- A list of filters and validators to be applied in order.
655- fallback (Any):
656- A fallback value to return if validation fails.
657- value (Any):
658- The initial value to be processed.
659-
660- Returns:
661- Any: The processed value after applying all filters and validators.
662- If a validation error occurs and a fallback is provided, the
663- fallback value is returned.
664-
665- Raises:
666- ValidationError: If validation fails and no fallback value is
667- provided.
668- """
669- if value is None :
670- return
671-
672- try :
673- for step in steps :
674- if isinstance (step , BaseFilter ):
675- value = step .apply (value )
676- elif isinstance (step , BaseValidator ):
677- step .validate (value )
678- except ValidationError :
679- if fallback is None :
680- raise
681- return fallback
682- return value
683-
684- @staticmethod
685- def checkForRequired (
686- field_name : str ,
687- required : bool ,
688- default : Any ,
689- fallback : Any ,
690- value : Any ,
691- ) -> Any :
692- """
693- Determine the value of the field, considering the required and
694- fallback attributes.
695-
696- If the field is not required and no value is provided, the default
697- value is returned. If the field is required and no value is provided,
698- the fallback value is returned. If no of the above conditions are met,
699- a ValidationError is raised.
700-
701- Args:
702- field_name (str): The name of the field being processed.
703- required (bool): Indicates whether the field is required.
704- default (Any): The default value to use if the field is not
705- provided and not required.
706- fallback (Any): The fallback value to use if the field is required
707- but not provided.
708- value (Any): The current value of the field being processed.
709-
710- Returns:
711- Any: The determined value of the field after considering required,
712- default, and fallback attributes.
713-
714- Raises:
715- ValidationError: If the field is required and no value or fallback
716- is provided.
717- """
718- if value is not None :
719- return value
720-
721- if not required :
722- return default
723-
724- if fallback is not None :
725- return fallback
726-
727- raise ValidationError (f"Field '{ field_name } ' is required." )
728-
729613 def addGlobalFilter (self , filter : BaseFilter ) -> None :
730614 """
731615 Add a global filter to be applied to all fields.
@@ -748,28 +632,6 @@ def getGlobalFilters(self) -> List[BaseFilter]:
748632 """
749633 return self .global_filters
750634
751- @staticmethod
752- def applyFilters (filters : List [BaseFilter ], value : Any ) -> Any :
753- """
754- Apply filters to the field value.
755-
756- Args:
757- filters (List[BaseFilter]): A list of filters to apply to the
758- value.
759- value (Any): The value to be processed by the filters.
760-
761- Returns:
762- Any: The processed value after applying all filters.
763- If the value is None, None is returned.
764- """
765- if value is None :
766- return
767-
768- for filter in filters :
769- value = filter .apply (value )
770-
771- return value
772-
773635 def clear (self ) -> None :
774636 """
775637 Resets all fields of the InputFilter instance to
@@ -875,33 +737,3 @@ def getGlobalValidators(self) -> List[BaseValidator]:
875737 List[BaseValidator]: A list of global validators.
876738 """
877739 return self .global_validators
878-
879- @staticmethod
880- def validateField (
881- validators : List [BaseValidator ], fallback : Any , value : Any
882- ) -> Any :
883- """
884- Validate the field value.
885-
886- Args:
887- validators (List[BaseValidator]): A list of validators to apply
888- to the field value.
889- fallback (Any): A fallback value to return if validation fails.
890- value (Any): The value to be validated.
891-
892- Returns:
893- Any: The validated value if all validators pass. If validation
894- fails and a fallback is provided, the fallback value is
895- returned.
896- """
897- if value is None :
898- return
899-
900- try :
901- for validator in validators :
902- validator .validate (value )
903- except ValidationError :
904- if fallback is None :
905- raise
906-
907- return fallback
0 commit comments