Add an example of cross-attribute validation
This commit is contained in:
parent
08ff764de7
commit
1abfb5dad8
|
@ -189,32 +189,35 @@ A validator is simply a callable that takes three arguments:
|
||||||
#. The *attribute* that it's validating
|
#. The *attribute* that it's validating
|
||||||
#. and finally the *value* that is passed for it.
|
#. and finally the *value* that is passed for it.
|
||||||
|
|
||||||
If the value does not pass the validator's standards, it just raises an appropriate exception:
|
If the value does not pass the validator's standards, it just raises an appropriate exception.
|
||||||
|
Since the validator runs *after* the instance is initialized, you can refer to other attributes while validating :
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
>>> def smaller_than_5(instance, attribute, value):
|
>>> def x_smaller_than_y(instance, attribute, value):
|
||||||
... if value >= 5:
|
... if value >= instance.y:
|
||||||
... raise ValueError("'{name}' has to be smaller than 5!"
|
... raise ValueError("'x' has to be smaller than 'y'!")
|
||||||
... .format(name=attribute.name))
|
|
||||||
>>> @attr.s
|
>>> @attr.s
|
||||||
... class C(object):
|
... class C(object):
|
||||||
... x = attr.ib(validator=smaller_than_5)
|
... x = attr.ib(validator=x_smaller_than_y)
|
||||||
>>> C(42)
|
... y = attr.ib()
|
||||||
|
>>> C(x=3, y=4)
|
||||||
|
C(x=3, y=4)
|
||||||
|
>>> C(x=4, y=3)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'x' has to be smaller than 5!
|
ValueError: 'x' has to be smaller than 'y'!
|
||||||
|
|
||||||
``attrs`` won't intercept your changes to those attributes but you can always call :func:`attr.validate` on any instance to verify, that it's still valid:
|
``attrs`` won't intercept your changes to those attributes but you can always call :func:`attr.validate` on any instance to verify, that it's still valid:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
>>> i = C(4)
|
>>> i = C(4, 5)
|
||||||
>>> i.x = 5 # works, no magic here
|
>>> i.x = 5 # works, no magic here
|
||||||
>>> attr.validate(i)
|
>>> attr.validate(i)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'x' has to be smaller than 5!
|
ValueError: 'x' has to be smaller than 'y'!
|
||||||
|
|
||||||
``attrs`` ships with a bunch of validators, make sure to :ref:`check them out <api_validators>` before writing your own:
|
``attrs`` ships with a bunch of validators, make sure to :ref:`check them out <api_validators>` before writing your own:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue