Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1894] Pass a list of keys in the schema to validators
This is included in the context. We pass a copy of the one provided to validate.
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import nose | ||
from ckan.lib.navl.dictization_functions import validate | ||
|
||
|
||
eq_ = nose.tools.eq_ | ||
|
||
|
||
class TestValidate(object): | ||
|
||
def test_validate_passes_a_copy_of_the_context_to_validators(self): | ||
|
||
# We need to pass some keys on the context, otherwise validate | ||
# will do context = context || {}, which creates a new one, defeating | ||
# the purpose of this test | ||
context = {'foo': 'bar'} | ||
|
||
def my_validator(key, data, errors, context_in_validator): | ||
|
||
assert not id(context) == id(context_in_validator) | ||
|
||
data_dict = { | ||
'my_field': 'test', | ||
} | ||
|
||
schema = { | ||
'my_field': [my_validator], | ||
} | ||
|
||
data, errors = validate(data_dict, schema, context) | ||
|
||
def test_validate_adds_schema_keys_to_context(self): | ||
|
||
def my_validator(key, data, errors, context): | ||
|
||
assert 'schema_keys' in context | ||
|
||
eq_(context['schema_keys'], ['my_field']) | ||
|
||
data_dict = { | ||
'my_field': 'test', | ||
} | ||
|
||
schema = { | ||
'my_field': [my_validator], | ||
} | ||
|
||
context = {} | ||
|
||
data, errors = validate(data_dict, schema, context) |