Skip to content

Commit

Permalink
Update README.rst.
Browse files Browse the repository at this point in the history
  • Loading branch information
suriya committed Apr 17, 2016
1 parent b018c8d commit f64f29b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,39 @@ date as follows:
:code:`XValidatedModel` ensures that this specification is maintained
invoking :code:`Event.clean()` and raises :code:`ValidationError` as
appropriate.

Why use django-xvalidate?
-------------------------
django-xvalidate allows you to specify how to validate your model instances
in a more declarative manner than writing imperative code within your
:code:`clean()` methods. Without django-xvalidate you would have to
implement the above example as

.. code:: python
def clean(self):
super(Event, self).clean()
if (self.start_date is not None) and (self.end_date is not None):
if (self.end_date < self.start_date):
raise ValidationError('The start date should precede the end date')
With a more declarative format we have the option at some point in the
future to automate the creation of test data that passes (or fails)
validation.

django-xvalidate comes some operator overloading that brings syntactic
sugar to your declarations making them very easy to read. For instance,
you could specify:

.. code:: python
((XF('end_date') - 'start_date') > datetime.timedelta(days=4)).message(
'Event should last at least 5 days'
)
django-xvalidate also allows the use of Django's double-underscore (`__`)
syntax to dereference related objects, enabling succinct definitions such
as the following

.. code:: python
(XF('registration_date') <= 'event__end_date').message(
'Must register before the event ends')

0 comments on commit f64f29b

Please sign in to comment.