def _validate_choices_type(self): allowed_types = (list, tuple, set, ) # Prohibit user from giving any data type in choices attribute msg = u"choices: {} should of any one of the type {}".format( self.choices, allowed_types) if not isinstance(self.choices, allowed_types): return ValidationResult(is_success=False, reason=msg) return ValidationResult(is_success=True)
def validate(self, request, value): validation_result = super(BooleanField, self).validate(request, value) if not validation_result.is_success: return validation_result if self.nullable and value is None: return ValidationResult(is_success=True) if value == DEFAULT_FIELD_VALUE: return ValidationResult(is_success=True) if not isinstance(value, bool): return ValidationResult(is_success=False, reason='Expected type Boolean') return ValidationResult(is_success=True)
def validate(self, request, value): """ This is not for business validation This should be only used for type validation Hence the only input is the value of the field :param value: :return: """ if self.required and value == DEFAULT_FIELD_VALUE: return ValidationResult(is_success=False, reason='This field is required') if not self.nullable and value is None: return ValidationResult(is_success=False, reason='null is not a valid value') return ValidationResult(is_success=True)
def validate(self, request, value): validation_result = super(SchemaField, self).validate(request, value) if not validation_result.is_success: return validation_result if value == DEFAULT_FIELD_VALUE: return ValidationResult(is_success=True) if value is None and self.nullable: return ValidationResult(is_success=True) errors = self.validator.validate_data(request, value) if errors: return ValidationResult(is_success=False, reason=errors) return ValidationResult(is_success=True)
def validate(self, request, value): validation_result = super(UrlField, self).validate(request, value) if not validation_result.is_success: return validation_result if self.nullable and value is None: return ValidationResult(is_success=True) if value == DEFAULT_FIELD_VALUE: return ValidationResult(is_success=True) if not self.required and not value: return ValidationResult(is_success=True) if not UrlValidator().validate(url=value): return ValidationResult(is_success=False, reason=u"The url is not valid") return ValidationResult(is_success=True)
def validate(self, request, value): validation_result = super(ChoiceField, self).validate(request, value) if not validation_result.is_success: return validation_result if value == DEFAULT_FIELD_VALUE: return ValidationResult(is_success=True) # Check it is possible to do validation. result = self._validate_choices_type() if not result.is_success: return result if value in self.choices: return ValidationResult(is_success=True) return ValidationResult( is_success=False, reason=u"{} isn't valid choice".format(value))
def validate(self, request, value): errors = [] validation_result = super(ListField, self).validate(request, value) if not validation_result.is_success: return validation_result value = value if value not in (DEFAULT_FIELD_VALUE, None) else [] if self.nullable and value is None: return ValidationResult(is_success=True) for item in value: validation_result = self.field.validate(request, item) if not validation_result.is_success: errors.append(validation_result.reason) if errors: return ValidationResult(is_success=False, reason=errors) else: return ValidationResult(is_success=True)