Version 2 (modified by amitu, 8 years ago) (diff)

--

Validation Helpers For newform

Please read this thread to get some background.

assure(expression, message)

This will raise ValidationError(message) if expression evaluates to false.

assure_false(expression, msg)

This will raise ValidationError(msg) if expression evalautes to true.

assure_equal(val1, val2, msg)

Raises ValidationError(msg) if val1 != val2.

assure_not_equal(val1, val2, msg)

Raises ValidationError(msg) if val1 == val2.

assure_almost_equal(first, second, msg, delta=0.1)

Tests if first and second differ by less than delta, raise ValidationError(msg) otherwise.

assure_not_almost_equal(first, second, msg, delat=0.1)

Tests if first and second are further than delta apart, otherwise raises ValidationError(msg).

assure_exception(exception, msg, callable, *args, kw)

Calls calls the callable: callable(*args, kw) and raises ValidationError if callable did not raise the exception. exception can optionally be a sequence contains more than one exceptions, in this case ValidationError will be raised if either no exception was raised by the callable or exception not in the list was raised.

assure_no_exception(exception, msg, callable, *args, kw)

Calls calls the callable: callable(*args, kw) and raises ValidationError if callable did not raise the exception. exception can optionally be a sequence contains more than one exceptions, in this case ValidationError will be raised if callable raised an exception that is an isntance of any exception class in the sequence.

assure_valid_email(data, msg)

Will raise ValdationError(msg) if data is not a valid email. This may be helpful when taking comma separated email addresses as input for example.

assure_valid_slug(data, msg)

Will raise ValidationError(msg) if data is not a valid slug. This may be helpful when generating slug based on other fields for example.

assure_valid_*

(assure_valid_int, assure_valid_float, assure_valid_url, etc)

Other validation shortcuts for django defined fields. This will be simpler than importing the regex from appropriate django module, and calling .match on it. Such functions are helpful when the field value is not being validated on as such, but when intermediate strings derived from field values are being validated.

assure_date_before(date, future_date, msg)

This will raise ValidationError(msg) if date is not before future_date.

assure_date_in_range(date, first_date, second_date, msg)

This will raise ValidationError(msg) if date is not withing first_date and second_date.

Back to Top