def conflict(self, key, **kwargs): """ A helper method that simply raises a validation error. """ try: msg = self.error_messages[key] except KeyError: class_name = self.__class__.__name__ msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) message_string = msg.format(**kwargs) raise Conflict(message_string)
def fail_for_field(self, key, **kwargs): """ A helper method that simply raises a validation error for a field. """ try: field, msg = self.field_error_messages[key] except KeyError: class_name = self.__class__.__name__ msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) message_string = msg.format(**kwargs) message = {field: message_string} raise ValidationError(message, code=key)
def custom_fail(self, key, **kwargs): """ A helper method that simply raises a validation error. If we pass validators to a DRF serializer through the error_messages argument, this allows us to provide dictionary entries found in validation_errors.py, which are then raised as CustomValidationErrors. """ try: msg = self.error_messages[key] except KeyError: class_name = self.__class__.__name__ msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) if isinstance(msg, dict): # We have passed a dict containing the field, error_text, and error_summary, we want to # raise a CustomValidationError here raise CustomValidationError(**msg) message_string = msg.format(**kwargs) raise ValidationError(message_string, code=key)