Python object validation thanks to a Schema
Question: I want to validate a python object thanks to a schema. For this I found the
[`schema` framework](https://pypi.python.org/pypi/schema).

I would like to validate a numeric string:

    
    
    a = {
        'phone_number': '12233'
    }
    

Do you know how can I validate this string thanks to a regex?

At this time, I only know how to perform a string validation:

    
    
    Schema(str).validate('12')
    


Answer: `Schema` will call any callables; simply provide a function that uses a
regular expression:

    
    
    import re
    
    pattern = re.compile('^12\d+$')
    
    Schema(And(str, lambda x: pattern.match(x) is not None))
    

Demo:

    
    
    >>> import re
    >>> from schema import Schema, And
    >>> pattern = re.compile('^12\d+$')
    >>> s = Schema(And(str, lambda x: pattern.match(x) is not None))
    >>> s.validate('123234')
    '123234'
    >>> s.validate('42')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/schema.py", line 153, in validate
        raise SchemaError([None] + x.autos, [e] + x.errors)
    schema.SchemaError: <lambda>('42') should evaluate to True
    

