mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
errors for invalid validator use
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user