<commit_before>.. _ref-validators:

==========
Validators
==========

.. versionadded:: 1.2

Writing validators
==================

A validator is just a callable that takes a value, and raises a
``ValidationError`` if it doesn't meet some criteria. They can be useful for
re-using validation logic between different types of fields.

For example, lets write a validator that only allows even numbers::

    from django.core.exceptions import ValidationError
    
    def validate_even(value):
        if value % 2 != 0:
            raise ValidationError(u'%s is not an even number' % value)

You can then added this to your model fields via the field's ``validators``
argument::<commit_msg>Fixed a typo in ref/validators.txt.<commit_after>.. _ref-validators:

==========
Validators
==========

.. versionadded:: 1.2

Writing validators
==================

A validator is just a callable that takes a value, and raises a
``ValidationError`` if it doesn't meet some criteria. They can be useful for
re-using validation logic between different types of fields.

For example, lets write a validator that only allows even numbers::

    from django.core.exceptions import ValidationError
    
    def validate_even(value):
        if value % 2 != 0:
            raise ValidationError(u'%s is not an even number' % value)

You can then add this to your model fields via the field's ``validators``
argument::
{'fraction_non_alphanumeric': 0.12196861626248216, 'fraction_numerical': 0.005706134094151213, 'mean_word_length': 3.8715277777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7113844', 'n_tokens_mistral': 386, 'n_tokens_neox': 362, 'n_words': 169}