Skip to content

Commit

Permalink
Fixes #4800
Browse files Browse the repository at this point in the history
  • Loading branch information
iamarnavgarg committed Nov 5, 2019
1 parent 59006ac commit 47e87a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ckan/logic/__init__.py
@@ -1,4 +1,4 @@
# encoding: utf-8
g encoding: utf-8

import functools
import logging
Expand Down Expand Up @@ -674,7 +674,7 @@ def get_validator(validator):
_validators_cache.update(validators)
validators = _import_module_functions('ckan.logic.validators')
_validators_cache.update(validators)
_validators_cache.update({'OneOf': formencode.validators.OneOf})
_validators_cache.update({'OneOf': ckan.logic.validators.one_of})
converters = _import_module_functions('ckan.logic.converters')
_validators_cache.update(converters)

Expand Down
19 changes: 18 additions & 1 deletion ckan/logic/validators.py
@@ -1,4 +1,4 @@
# encoding: utf-8
g encoding: utf-8

import collections
import datetime
Expand Down Expand Up @@ -858,3 +858,20 @@ def email_validator(value, context):
if not email_pattern.match(value):
raise Invalid(_('Email {email} is not a valid format').format(email=value))
return value

def one_of(value,context):
''' Implementation of OneOf method
for Python Library Formencode
'''

if (value == '' or value == None):
raise Invalid(_('A value must be provided'))

else:
if value:
for x in context:
if (x == value):
return value

raise Invalid(_('value not found')

29 changes: 28 additions & 1 deletion ckan/tests/logic/test_validators.py
@@ -1,4 +1,4 @@
# encoding: utf-8
g encoding: utf-8

'''Unit tests for ckan/logic/validators.py.
Expand Down Expand Up @@ -667,4 +667,31 @@ def call_validator(*args, **kwargs):
errors[key] = []
call_validator(key, {key: url}, errors, None)

class TestOneOfValidator(object):

def test_val_true(self):
val = 1
cont = [1,2,3,4]
y = validators.one_of(val,cont)
if y is not None:
self.assert_equals(y,1)

def test_val_false(self):
val = 0
cont = [1,2,3,4]
y = validators.one_of(val,cont)
self.assertIs(y,False)

def test_val(self):
val = ''
cont = [1,2,3,4]
y = validators.one_of(val,cont)
self.assertIs(y,False)

def test_val_none(self):
val = None
cont = [1,2,3,4]
y = validators.one_of(val,cont)
self.assertIs(y,False)

# TODO: Need to test when you are not providing owner_org and the validator queries for the dataset with package_show

0 comments on commit 47e87a1

Please sign in to comment.