extra_validator/field_validation/validator.py
def _clean_required_and_optional_fields(self, exclude=None, context=None):
        """Provide extra validation for fields that are required and single selection fields."""
        exclude = exclude or []
        if any(
            [
                self.REQUIRED_TOGGLE_FIELDS,
                self.REQUIRED_MIN_FIELDS,
                self.REQUIRED_FIELDS,
                self.OPTIONAL_TOGGLE_FIELDS,
            ]
        ):
            error_class = self.ERROR_HANDLERS.get(context, ValueError)
            found = []
            errors = {}
            optional = []

            for f in self._meta.fields:
                if f.name not in exclude:
                    raw_value = getattr(self, f.attname)
                    if f.name in self.REQUIRED_FIELDS:
                        if raw_value in f.empty_values and not f.has_default():
                            msg = 'Please provide a value for: "{field_name}".'.format(
                                field_name=f.name
                            )
                            errors.update(self._error_as_dict(f.name, msg))

                    for required_min_field in self.REQUIRED_MIN_FIELDS:
                        # Multiple selection of at least one required.
                        if f.name in required_min_field:
                            if raw_value not in f.empty_values:
                                found.append({f.name: raw_value})
                            elif raw_value in f.empty_values and f.has_default():
                                if (
                                    isinstance(f, models.CharField)
                                    and f.get_default() not in f.empty_values
                                ):
                                    found.append({f.name: f.get_default()})

                    for required_toggle_field in self.REQUIRED_TOGGLE_FIELDS:
                        # Single selection of at most one required.
                        if f.name in required_toggle_field:
                            if raw_value not in f.empty_values:
                                found.append({f.name: raw_value})
                            elif raw_value in f.empty_values and f.has_default():
                                if (
                                    isinstance(f, models.CharField)
                                    and f.get_default() not in f.empty_values
                                ):
                                    found.append({f.name: f.get_default()})

                    for optional_toggle_field in self.OPTIONAL_TOGGLE_FIELDS:
                        if (
                            f.name in optional_toggle_field
                            and raw_value not in f.empty_values
                        ):
                            optional.append({f.name: raw_value})

            if self.REQUIRED_MIN_FIELDS:
                if not found:
                    fields_str = "\n, ".join(
                        [field_to_str(fields) for fields in self.REQUIRED_MIN_FIELDS]
                    )
                    fields_str = (
                        "\n {fields}".format(fields=fields_str)
                        if len(self.REQUIRED_MIN_FIELDS) > 1
                        else fields_str
                    )
                    msg = "Please provide a valid value for any of the following fields: {fields}".format(
                        fields=fields_str
                    )
                    errors.update(self._error_as_dict(NON_FIELD_ERRORS, msg))

            if self.REQUIRED_TOGGLE_FIELDS:
                if not found:
                    fields_str = "\n, ".join(
                        [field_to_str(fields) for fields in self.REQUIRED_TOGGLE_FIELDS]
                    )
                    fields_str = (
                        "\n {fields}".format(fields=fields_str)
                        if len(self.REQUIRED_TOGGLE_FIELDS) > 1
                        else fields_str
                    )
                    msg = "Please provide a valid value for any of the following fields: {fields}".format(
                        fields=fields_str
                    )
                    errors.update(self._error_as_dict(NON_FIELD_ERRORS, msg))
                else:
                    found_keys = [k for item in found for k in item.keys()]
                    errors.update(
                        self._validate_only_one_option(
                            self.REQUIRED_TOGGLE_FIELDS, found_keys
                        ),
                    )

            if self.OPTIONAL_TOGGLE_FIELDS:
                if optional:
                    optional_keys = [k for item in optional for k in item.keys()]
                    errors.update(
                        self._validate_only_one_option(
                            self.OPTIONAL_TOGGLE_FIELDS, optional_keys
                        ),
                    )

            if errors:
                raise error_class(errors)
Provide extra validation for fields that are required and single selection fields.
Provide extra validation for fields that are required and single selection fields.
Provide extra validation for fields that are required and single selection fields .
def _clean_required_and_optional_fields(self, exclude=None, context=None):
        exclude = exclude or []
        if any(
            [
                self.REQUIRED_TOGGLE_FIELDS,
                self.REQUIRED_MIN_FIELDS,
                self.REQUIRED_FIELDS,
                self.OPTIONAL_TOGGLE_FIELDS,
            ]
        ):
            error_class = self.ERROR_HANDLERS.get(context, ValueError)
            found = []
            errors = {}
            optional = []
            for f in self._meta.fields:
                if f.name not in exclude:
                    raw_value = getattr(self, f.attname)
                    if f.name in self.REQUIRED_FIELDS:
                        if raw_value in f.empty_values and not f.has_default():
                            msg = 'Please provide a value for: "{field_name}".'.format(
                                field_name=f.name
                            )
                            errors.update(self._error_as_dict(f.name, msg))
                    for required_min_field in self.REQUIRED_MIN_FIELDS:
                        if f.name in required_min_field:
                            if raw_value not in f.empty_values:
                                found.append({f.name: raw_value})
                            elif raw_value in f.empty_values and f.has_default():
                                if (
                                    isinstance(f, models.CharField)
                                    and f.get_default() not in f.empty_values
                                ):
                                    found.append({f.name: f.get_default()})
                    for required_toggle_field in self.REQUIRED_TOGGLE_FIELDS:
                        if f.name in required_toggle_field:
                            if raw_value not in f.empty_values:
                                found.append({f.name: raw_value})
                            elif raw_value in f.empty_values and f.has_default():
                                if (
                                    isinstance(f, models.CharField)
                                    and f.get_default() not in f.empty_values
                                ):
                                    found.append({f.name: f.get_default()})
                    for optional_toggle_field in self.OPTIONAL_TOGGLE_FIELDS:
                        if (
                            f.name in optional_toggle_field
                            and raw_value not in f.empty_values
                        ):
                            optional.append({f.name: raw_value})
            if self.REQUIRED_MIN_FIELDS:
                if not found:
                    fields_str = "\n, ".join(
                        [field_to_str(fields) for fields in self.REQUIRED_MIN_FIELDS]
                    )
                    fields_str = (
                        "\n {fields}".format(fields=fields_str)
                        if len(self.REQUIRED_MIN_FIELDS) > 1
                        else fields_str
                    )
                    msg = "Please provide a valid value for any of the following fields: {fields}".format(
                        fields=fields_str
                    )
                    errors.update(self._error_as_dict(NON_FIELD_ERRORS, msg))
            if self.REQUIRED_TOGGLE_FIELDS:
                if not found:
                    fields_str = "\n, ".join(
                        [field_to_str(fields) for fields in self.REQUIRED_TOGGLE_FIELDS]
                    )
                    fields_str = (
                        "\n {fields}".format(fields=fields_str)
                        if len(self.REQUIRED_TOGGLE_FIELDS) > 1
                        else fields_str
                    )
                    msg = "Please provide a valid value for any of the following fields: {fields}".format(
                        fields=fields_str
                    )
                    errors.update(self._error_as_dict(NON_FIELD_ERRORS, msg))
                else:
                    found_keys = [k for item in found for k in item.keys()]
                    errors.update(
                        self._validate_only_one_option(
                            self.REQUIRED_TOGGLE_FIELDS, found_keys
                        ),
                    )
            if self.OPTIONAL_TOGGLE_FIELDS:
                if optional:
                    optional_keys = [k for item in optional for k in item.keys()]
                    errors.update(
                        self._validate_only_one_option(
                            self.OPTIONAL_TOGGLE_FIELDS, optional_keys
                        ),
                    )
            if errors:
                raise error_class(errors)
def _clean_required_and_optional_fields ( self , exclude = None , context = None ) : exclude = exclude or [ ] if any ( [ self . REQUIRED_TOGGLE_FIELDS , self . REQUIRED_MIN_FIELDS , self . REQUIRED_FIELDS , self . OPTIONAL_TOGGLE_FIELDS , ] ) : error_class = self . ERROR_HANDLERS . get ( context , ValueError ) found = [ ] errors = { } optional = [ ] for f in self . _meta . fields : if f . name not in exclude : raw_value = getattr ( self , f . attname ) if f . name in self . REQUIRED_FIELDS : if raw_value in f . empty_values and not f . has_default ( ) : msg = 'Please provide a value for: "{field_name}".' . format ( field_name = f . name ) errors . update ( self . _error_as_dict ( f . name , msg ) ) for required_min_field in self . REQUIRED_MIN_FIELDS : if f . name in required_min_field : if raw_value not in f . empty_values : found . append ( { f . name : raw_value } ) elif raw_value in f . empty_values and f . has_default ( ) : if ( isinstance ( f , models . CharField ) and f . get_default ( ) not in f . empty_values ) : found . append ( { f . name : f . get_default ( ) } ) for required_toggle_field in self . REQUIRED_TOGGLE_FIELDS : if f . name in required_toggle_field : if raw_value not in f . empty_values : found . append ( { f . name : raw_value } ) elif raw_value in f . empty_values and f . has_default ( ) : if ( isinstance ( f , models . CharField ) and f . get_default ( ) not in f . empty_values ) : found . append ( { f . name : f . get_default ( ) } ) for optional_toggle_field in self . OPTIONAL_TOGGLE_FIELDS : if ( f . name in optional_toggle_field and raw_value not in f . empty_values ) : optional . append ( { f . name : raw_value } ) if self . REQUIRED_MIN_FIELDS : if not found : fields_str = "\n, " . join ( [ field_to_str ( fields ) for fields in self . REQUIRED_MIN_FIELDS ] ) fields_str = ( "\n {fields}" . format ( fields = fields_str ) if len ( self . REQUIRED_MIN_FIELDS ) > 1 else fields_str ) msg = "Please provide a valid value for any of the following fields: {fields}" . format ( fields = fields_str ) errors . update ( self . _error_as_dict ( NON_FIELD_ERRORS , msg ) ) if self . REQUIRED_TOGGLE_FIELDS : if not found : fields_str = "\n, " . join ( [ field_to_str ( fields ) for fields in self . REQUIRED_TOGGLE_FIELDS ] ) fields_str = ( "\n {fields}" . format ( fields = fields_str ) if len ( self . REQUIRED_TOGGLE_FIELDS ) > 1 else fields_str ) msg = "Please provide a valid value for any of the following fields: {fields}" . format ( fields = fields_str ) errors . update ( self . _error_as_dict ( NON_FIELD_ERRORS , msg ) ) else : found_keys = [ k for item in found for k in item . keys ( ) ] errors . update ( self . _validate_only_one_option ( self . REQUIRED_TOGGLE_FIELDS , found_keys ) , ) if self . OPTIONAL_TOGGLE_FIELDS : if optional : optional_keys = [ k for item in optional for k in item . keys ( ) ] errors . update ( self . _validate_only_one_option ( self . OPTIONAL_TOGGLE_FIELDS , optional_keys ) , ) if errors : raise error_class ( errors )
Provide extra validation for fields that are required and single selection fields.
Provide extra validation for fields that are required and single selection fields .
"""Provide extra validation for fields that are required and single selection fields.""" # Multiple selection of at least one required. # Single selection of at most one required.
self
exclude
context