Skip to content

v0.8.0 -- Extensions and More Validators

Compare
Choose a tag to compare
@mansam mansam released this 03 Apr 05:05
· 26 commits to master since this release

Extensions

The default module was getting a bit unwieldy, so I've created a new validator.ext submodule to contain validators that aren't common enough to be in the main namespace. Related validators should be grouped together in their own submodules under ext, e.g. all validators related to foo should live in a module called validator.ext.foo. One-offs can either go in validator.ext or in their own submodule, depending on the scope and likelihood of further expansion. I've started things off by putting the ArgSpec validator into the ext module since it was pretty obscure.

New Validators

This release features two new validators for working with collections, Contains and Length.

Contains

Contains is pretty straight forward, you can use it to ensure that a collection contains a particular value, like so:

   # Example:
   validation = {
       "field": [Contains(3)]
   }
   passes = {"field": [1, 2, 3]}
   fails  = {"field": [4, 5, 6]}

This'll work with any kind of collection that supports in, so strings, dictionaries, lists, sets, and more are all fair game.

Length

Length lets you ensure that a collection is of some minimum and optionally maximum length.

    # Example:
    validations = {
       "field": [Length(0, maximum=5)]
    }
    passes = {"field": "hello"}
    fails  = {"field": "hello world"}