Skip to content

Commit

Permalink
Add documentation for Length validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mansam committed May 13, 2014
1 parent c90413e commit 2a6364c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
26 changes: 22 additions & 4 deletions docs/index.rst
Expand Up @@ -33,7 +33,7 @@ About
"qux": 101
}
>>> validate(rules, passes)
(True, {})
(True, {})
# but this one would fail
fails = {
Expand Down Expand Up @@ -71,7 +71,7 @@ To create a validation, you insert a list of callables into a validation diction
"foo": "bar"
}
validation = {
"foo": [lambda x: x == "bar"]
"foo": [lambda x: x == "bar"]
}
>>> validate(validation, dictionary)
Expand Down Expand Up @@ -303,7 +303,7 @@ If the value isn't an instance of the base class or one of its subclasses:
The ``SubclassOf`` validator
----------------------------

The ``SubclassOf`` validator checks that the dictionary value is inherits from the base class passed to it. To be clear, this means that the dictionary value is expected to be a class, not an instance of a class.
The ``SubclassOf`` validator checks that the dictionary value inherits from the base class passed to it. To be clear, this means that the dictionary value is expected to be a class, not an instance of a class.

.. code-block:: python
Expand All @@ -326,6 +326,24 @@ If the value isn't a subclass of base class or one of its subclasses (e.g. if it
>>> validate(validation, failure)
(False, {"foo": ["must be a subclass of basestring"]})
The ``Length`` validator
------------------------

The ``Length`` validator checks that value the must have at least `minimum` elements and optionally at most `maximum` elements.

.. code-block:: python
dictionary = {
"foo": [1, 2, 3, 4, 5]
}
validation = {
"foo": [Length(0, maximum=5)]
}
>>> validate(validation, dictionary)
(True, {})
# Success!
Conditional Validations
-----------------------

Expand Down Expand Up @@ -389,7 +407,7 @@ You can nest validation dictionaries within each other in order to accommodate m
}
}
The above example says that the ``bar`` key represents a dictionary that also has its own set of validations. For good measure, this example has yet another dictionary under the ``qux`` key. As long as everything checks out, ``validate`` will return the normal ``(True, {})`` response indicating success.
The above example says that the ``bar`` key represents a dictionary that also has its own set of validations. For good measure, this example has yet another dictionary under the ``qux`` key. As long as everything checks out, ``validate`` will return the normal ``(True, {})`` response indicating success.

In the event of failure, you get an appropriately nested error message like those produced by the conditional validator. Here's an example of what such an error might look like:

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='validator.py',
version='1.0.0',
version='1.0.1',
author='Samuel "mansam" Lucidi',
author_email="mansam@csh.rit.edu",
packages=['validator'],
Expand Down
36 changes: 31 additions & 5 deletions validator/__init__.py
Expand Up @@ -29,7 +29,7 @@
"""

__version__ = "1.0.0"
__version__ = "1.0.1"

import re
from collections import defaultdict
Expand All @@ -48,6 +48,13 @@ def _isstr(s):
return isinstance(s, str)

class Validator(object):
"""
Abstract class that advanced
validators can inherit from in order
to set custom error messages and such.
"""

__metaclass__ = ABCMeta

err_message = "failed validation"
Expand Down Expand Up @@ -99,6 +106,22 @@ def __call__(self, value):
return not self.validator(value)

class Range(Validator):
"""
Use to specify that the value of the
key being validated must fall between
the start and end values. By default
the range is inclusive, though the
range can be made excusive by setting
inclusive to false.
# Example:
validations = {
"field": [Range(0, 10)]
}
passes = {"field": 10}
fails = {"field" : 11}
"""

def __init__(self, start, end, inclusive=True):
self.start = start
Expand Down Expand Up @@ -180,11 +203,11 @@ class Truthy(Validator):
fails = {"field": 0}
"""
"""

def __init__(self):
self.err_message = "must be True-equivalent value"
self.not_message = "must be False-equivalent value"
self.not_message = "must be False-equivalent value"

def __call__(self, value):
if value:
Expand Down Expand Up @@ -253,6 +276,7 @@ class SubclassOf(Validator):
}
passes = {"field": str} # is a subclass of basestring
fails = {"field": int}
"""

def __init__(self, base_class):
Expand Down Expand Up @@ -340,7 +364,7 @@ def __call__(self, value, dictionary):
conditional = True
dependent = self.then_clause(dictionary)
return conditional, dependent

class Length(Validator):
"""
Use to specify that the
Expand All @@ -350,7 +374,7 @@ class Length(Validator):
at most `maximum` elements.
At least one of the parameters
to this validator must be non-zero,
to this validator must be non-zero,
and neither may be negative.
# Example:
Expand Down Expand Up @@ -459,6 +483,8 @@ def validate(validation, dictionary):
def _validate_and_store_errs(validator, dictionary, key, errors):
valid = validator(dictionary[key])
if not valid:
# set a default error message for things like lambdas
# and other callables that won't have an err_message set.
msg = getattr(validator, "err_message", "failed validation")
errors[key].append(msg)

Expand Down
2 changes: 0 additions & 2 deletions validator/ext/__init__.py
Expand Up @@ -26,9 +26,7 @@
"""

from validator import Pattern
from inspect import getargspec
import string

def ArgSpec(*args, **kwargs):
"""
Expand Down

0 comments on commit 2a6364c

Please sign in to comment.