[Python-modules-commits] [python-schema] 01/01: New upstream version 0.6.6
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Sat May 6 14:52:02 UTC 2017
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch upstream/latest
in repository python-schema.
commit 42bededa04bb5813b1939e24e40e3683065b6a94
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date: Sat May 6 14:40:26 2017 +0100
New upstream version 0.6.6
---
PKG-INFO | 8 +++++---
README.rst | 6 ++++--
schema.egg-info/PKG-INFO | 8 +++++---
schema.py | 35 +++++++++++++++++++++++++----------
setup.cfg | 1 -
test_schema.py | 43 +++++++++++++++++++++++++++++++++++++++++++
6 files changed, 82 insertions(+), 19 deletions(-)
diff --git a/PKG-INFO b/PKG-INFO
index de053ee..ac5ea3a 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: schema
-Version: 0.6.5
+Version: 0.6.6
Summary: Simple data validation library
Home-page: https://github.com/keleshev/schema
Author: Vladimir Keleshev
@@ -218,7 +218,8 @@ Description: Schema validation just got Pythonic
... 10: 'not None here'})
Traceback (most recent call last):
...
- SchemaError: None does not match 'not None here'
+ SchemaError: Key '10' error:
+ None does not match 'not None here'
This is useful if you want to check certain key-values, but don't care
about other:
@@ -269,7 +270,8 @@ Description: Schema validation just got Pythonic
>>> Schema({'password': And(str, lambda s: len(s) > 6)}).validate({'password': 'hai'})
Traceback (most recent call last):
...
- SchemaError: <lambda>('hai') should evaluate to True
+ SchemaError: Key 'password' error:
+ <lambda>('hai') should evaluate to True
>>> Schema(And(Or(int, float), lambda x: x > 0)).validate(3.1415)
3.1415
diff --git a/README.rst b/README.rst
index 9e71dc8..0bcb0f7 100644
--- a/README.rst
+++ b/README.rst
@@ -210,7 +210,8 @@ You can specify keys as schemas too:
... 10: 'not None here'})
Traceback (most recent call last):
...
- SchemaError: None does not match 'not None here'
+ SchemaError: Key '10' error:
+ None does not match 'not None here'
This is useful if you want to check certain key-values, but don't care
about other:
@@ -261,7 +262,8 @@ for the same data:
>>> Schema({'password': And(str, lambda s: len(s) > 6)}).validate({'password': 'hai'})
Traceback (most recent call last):
...
- SchemaError: <lambda>('hai') should evaluate to True
+ SchemaError: Key 'password' error:
+ <lambda>('hai') should evaluate to True
>>> Schema(And(Or(int, float), lambda x: x > 0)).validate(3.1415)
3.1415
diff --git a/schema.egg-info/PKG-INFO b/schema.egg-info/PKG-INFO
index de053ee..ac5ea3a 100644
--- a/schema.egg-info/PKG-INFO
+++ b/schema.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: schema
-Version: 0.6.5
+Version: 0.6.6
Summary: Simple data validation library
Home-page: https://github.com/keleshev/schema
Author: Vladimir Keleshev
@@ -218,7 +218,8 @@ Description: Schema validation just got Pythonic
... 10: 'not None here'})
Traceback (most recent call last):
...
- SchemaError: None does not match 'not None here'
+ SchemaError: Key '10' error:
+ None does not match 'not None here'
This is useful if you want to check certain key-values, but don't care
about other:
@@ -269,7 +270,8 @@ Description: Schema validation just got Pythonic
>>> Schema({'password': And(str, lambda s: len(s) > 6)}).validate({'password': 'hai'})
Traceback (most recent call last):
...
- SchemaError: <lambda>('hai') should evaluate to True
+ SchemaError: Key 'password' error:
+ <lambda>('hai') should evaluate to True
>>> Schema(And(Or(int, float), lambda x: x > 0)).validate(3.1415)
3.1415
diff --git a/schema.py b/schema.py
index 779bf39..5101906 100644
--- a/schema.py
+++ b/schema.py
@@ -4,9 +4,9 @@ parsing, converted from JSON/YAML (or something else) to Python data-types."""
import re
-__version__ = '0.6.5'
+__version__ = '0.6.6'
__all__ = ['Schema',
- 'And', 'Or', 'Regex', 'Optional',
+ 'And', 'Or', 'Regex', 'Optional', 'Use',
'SchemaError',
'SchemaWrongKeyError',
'SchemaMissingKeyError',
@@ -66,8 +66,11 @@ class And(object):
"""
def __init__(self, *args, **kw):
self._args = args
- assert list(kw) in (['error'], [])
+ assert set(kw).issubset(['error', 'schema', 'ignore_extra_keys'])
self._error = kw.get('error')
+ self._ignore_extra_keys = kw.get('ignore_extra_keys', False)
+ # You can pass your inherited Schema class.
+ self._schema = kw.get('schema', Schema)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__,
@@ -80,7 +83,9 @@ class And(object):
:param data: to be validated with sub defined schemas.
:return: returns validated data
"""
- for s in [Schema(s, error=self._error) for s in self._args]:
+ for s in [self._schema(s, error=self._error,
+ ignore_extra_keys=self._ignore_extra_keys)
+ for s in self._args]:
data = s.validate(data)
return data
@@ -96,7 +101,9 @@ class Or(And):
:return: return validated data if not validation
"""
x = SchemaError([], [])
- for s in [Schema(s, error=self._error) for s in self._args]:
+ for s in [self._schema(s, error=self._error,
+ ignore_extra_keys=self._ignore_extra_keys)
+ for s in self._args]:
try:
return s.validate(data)
except SchemaError as _x:
@@ -216,12 +223,14 @@ class Schema(object):
return _priority(s)
def validate(self, data):
+ Schema = self.__class__
s = self._schema
e = self._error
+ i = self._ignore_extra_keys
flavor = _priority(s)
if flavor == ITERABLE:
data = Schema(type(s), error=e).validate(data)
- o = Or(*s, error=e)
+ o = Or(*s, error=e, schema=Schema, ignore_extra_keys=i)
return type(data)(o.validate(d) for d in data)
if flavor == DICT:
data = Schema(dict, error=e).validate(data)
@@ -237,10 +246,16 @@ class Schema(object):
except SchemaError:
pass
else:
- nvalue = Schema(svalue, error=e).validate(value)
- new[nkey] = nvalue
- coverage.add(skey)
- break
+ try:
+ nvalue = Schema(svalue, error=e,
+ ignore_extra_keys=i).validate(value)
+ except SchemaError as x:
+ k = "Key '%s' error:" % nkey
+ raise SchemaError([k] + x.autos, [e] + x.errors)
+ else:
+ new[nkey] = nvalue
+ coverage.add(skey)
+ break
required = set(k for k in s if type(k) is not Optional)
if not required.issubset(coverage):
missing_keys = required - coverage
diff --git a/setup.cfg b/setup.cfg
index ddba5d3..87637b8 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -7,5 +7,4 @@ version_variable = schema.py:__version__
[egg_info]
tag_build =
tag_date = 0
-tag_svn_revision = 0
diff --git a/test_schema.py b/test_schema.py
index f994a00..d040b83 100644
--- a/test_schema.py
+++ b/test_schema.py
@@ -200,11 +200,22 @@ def test_dict_keys():
def test_ignore_extra_keys():
assert Schema({'key': 5}, ignore_extra_keys=True).validate(
{'key': 5, 'bad': 4}) == {'key': 5}
+ assert Schema({'key': 5, 'dk': {'a': 'a'}}, ignore_extra_keys=True).validate(
+ {'key': 5, 'bad': 'b', 'dk': {'a': 'a', 'bad': 'b'}}) == \
+ {'key': 5, 'dk': {'a': 'a'}}
+ assert Schema([{'key': 'v'}], ignore_extra_keys=True).validate(
+ [{'key': 'v', 'bad': 'bad'}]) == [{'key': 'v'}]
+ assert Schema([{'key': 'v'}], ignore_extra_keys=True).validate(
+ [{'key': 'v', 'bad': 'bad'}]) == [{'key': 'v'}]
def test_ignore_extra_keys_validation_and_return_keys():
assert Schema({'key': 5, object: object}, ignore_extra_keys=True).validate(
{'key': 5, 'bad': 4}) == {'key': 5, 'bad': 4}
+ assert Schema({'key': 5, 'dk': {'a': 'a', object: object}},
+ ignore_extra_keys=True).validate(
+ {'key': 5, 'dk': {'a': 'a', 'bad': 'b'}}) == \
+ {'key': 5, 'dk': {'a': 'a', 'bad': 'b'}}
def test_dict_optional_keys():
@@ -240,6 +251,22 @@ def test_dict_subtypes():
# is dropped!
+def test_dict_key_error():
+ try:
+ Schema({'k': int}).validate({'k': 'x'})
+ except SchemaError as e:
+ assert e.code == "Key 'k' error:\n'x' should be instance of 'int'"
+ try:
+ Schema({'k': {'k2': int}}).validate({'k': {'k2': 'x'}})
+ except SchemaError as e:
+ code = "Key 'k' error:\nKey 'k2' error:\n'x' should be instance of 'int'"
+ assert e.code == code
+ try:
+ Schema({'k': {'k2': int}}, error='k2 should be int').validate({'k': {'k2': 'x'}})
+ except SchemaError as e:
+ assert e.code == 'k2 should be int'
+
+
def test_complex():
s = Schema({'<file>': And([Use(open)], lambda l: len(l)),
'<path>': os.path.exists,
@@ -511,3 +538,19 @@ def test_copy():
s2 = copy.deepcopy(s1)
assert s1 is not s2
assert type(s1) is type(s2)
+
+
+def test_inheritance():
+ def convert(data):
+ if isinstance(data, int):
+ return data + 1
+ return data
+
+ class MySchema(Schema):
+ def validate(self, data):
+ return super(MySchema, self).validate(convert(data))
+
+ s = {'k': int, 'd': {'k': int, 'l': [{'l': [int]}]}}
+ v = {'k': 1, 'd': {'k': 2, 'l': [{'l': [3, 4, 5]}]}}
+ d = MySchema(s).validate(v)
+ assert d['k'] == 2 and d['d']['k'] == 3 and d['d']['l'][0]['l'] == [4, 5, 6]
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-schema.git
More information about the Python-modules-commits
mailing list