Skip to content

Commit

Permalink
[#1894] Prevent free extras to have the same key as a schema field
Browse files Browse the repository at this point in the history
Added a new validator to the 'key' field of the extras schema that
checks if the value is present on the 'schema_fields' list that was
added to the context on the previous commit.
  • Loading branch information
amercader committed Aug 25, 2014
1 parent 5161e15 commit 8bf7e00
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ckan/logic/schema.py
Expand Up @@ -54,6 +54,7 @@
if_empty_guess_format,
clean_format,
no_loops_in_hierarchy,
extra_key_not_in_root_schema,
)
from ckan.logic.converters import (convert_user_name_or_id_to_id,
convert_package_name_or_id_to_id,
Expand Down Expand Up @@ -373,7 +374,7 @@ def default_extras_schema():

schema = {
'id': [ignore],
'key': [not_empty, unicode],
'key': [not_empty, extra_key_not_in_root_schema, unicode],
'value': [not_missing],
'state': [ignore],
'deleted': [ignore_missing],
Expand Down
6 changes: 6 additions & 0 deletions ckan/logic/validators.py
Expand Up @@ -784,3 +784,9 @@ def no_loops_in_hierarchy(key, data, errors, context):
raise Invalid(_('This parent would create a loop in the '
'hierarchy'))


def extra_key_not_in_root_schema(key, data, errors, context):

for schema_key in context.get('schema_keys', []):
if schema_key == data[key]:
raise Invalid(_('There is a schema field with the same name'))
26 changes: 25 additions & 1 deletion ckan/new_tests/logic/test_conversion.py
Expand Up @@ -100,7 +100,7 @@ def test_convert_to_extras_field_can_be_combined_with_more_extras(self):
eq_(sorted([e['value'] for e in data['extras']]),
['Bye', 'Bye2', 'Hi'])

def test_convert_to_extras_field_can_be_combined_with_more_extras_deleted(self):
def test_convert_to_extras_field_can_be_combined_with_extras_deleted(self):

data_dict = {
'name': 'test-dataset',
Expand All @@ -127,3 +127,27 @@ def test_convert_to_extras_field_can_be_combined_with_more_extras_deleted(self):
['custom_text', 'proper_extra', 'proper_extra2'])
eq_(sorted([e['value'] for e in data['extras']]),
['Bye', 'Bye2', 'Hi'])

def test_convert_to_extras_free_extra_can_not_have_the_same_key(self):

data_dict = {
'name': 'test-dataset',
'custom_text': 'Hi',
'extras': [
{'key': 'custom_text', 'value': 'Bye'},
]
}

context = {
'model': model,
'session': model.Session,
}

package_plugin = lib_plugins.lookup_package_plugin('dataset')
schema = package_plugin.create_package_schema()

data, errors = validate(data_dict, schema, context)

assert 'extras' in errors
eq_(errors['extras'],
[{'key': [u'There is a schema field with the same name']}])

0 comments on commit 8bf7e00

Please sign in to comment.