errors for invalid validator use

This commit is contained in:
Samuel Colvin
2017-11-08 15:36:19 +00:00
parent acf3128dcd
commit ed2b3f2afd
3 changed files with 30 additions and 0 deletions
+4
View File
@@ -3,6 +3,10 @@
History
-------
v0.6.2 (2017-11-XX)
...................
* errors for invalid validator use
v0.6.1 (2017-11-08)
...................
* prevent duplicate validators, #101
+6
View File
@@ -318,6 +318,12 @@ def validator(*fields, pre: bool=False, whole: bool=False, always: bool=False):
:param whole: for complex objects (sets, lists etc.) whether to validate individual elements or the whole object
:param always: whether this method and other validators should be called even if the value is missing
"""
if not fields:
raise ConfigError('validator with no fields specified')
elif isinstance(fields[0], FunctionType):
raise ConfigError("validators should be used with fields and keyword arguments, not bare. "
"E.g. usage should be `@validator('<field_name>', ...)`")
def dec(f):
ref = f.__module__ + '.' + f.__qualname__
if ref in _FUNCS:
+20
View File
@@ -177,6 +177,26 @@ def test_duplicates():
'"tests.test_validators.test_duplicates.<locals>.Model.duplicate_name"')
def test_use_bare():
with pytest.raises(ConfigError):
class Model(BaseModel):
a: str
@validator
def checker(cls, v):
return v
def test_use_no_fields():
with pytest.raises(ConfigError):
class Model(BaseModel):
a: str
@validator()
def checker(cls, v):
return v
def test_validate_always():
check_calls = 0