Skip to content

Commit

Permalink
Merge pull request #3865 from ckan/3865-validator-override
Browse files Browse the repository at this point in the history
Allow IValidator to override existing validators
  • Loading branch information
amercader committed Nov 10, 2017
2 parents 6f6276b + cb8dd57 commit 6edfaa3
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 293 deletions.
38 changes: 37 additions & 1 deletion ckan/lib/navl/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ckan.lib.navl.dictization_functions as df

from ckan.common import _
from ckan.common import _, json

missing = df.missing
StopOnError = df.StopOnError
Expand Down Expand Up @@ -123,3 +123,39 @@ def unicode_only(value):
if not isinstance(value, unicode):
raise Invalid(_('Must be a Unicode string value'))
return value

def unicode_safe(value):
'''
Make sure value passed is treated as unicode, but don't raise
an error if it's not, just make a reasonable attempt to
convert other types passed.
This validator is a safer alternative to the old ckan idiom
of using the unicode() function as a validator. It tries
not to pollute values with Python repr garbage e.g. when passed
a list of strings (uses json format instead). It also
converts binary strings assuming either UTF-8 or CP1252
encodings (not ASCII, with occasional decoding errors)
'''
if isinstance(value, unicode):
return value
if hasattr(value, 'filename'):
# cgi.FieldStorage instance for uploaded files, show the name
value = value.filename
if value is missing or value is None:
return u''
if isinstance(value, bytes):
# bytes only arrive when core ckan or plugins call
# actions from Python code
try:
return value.decode(u'utf8')
except UnicodeDecodeError:
return value.decode(u'cp1252')
try:
return json.dumps(value, sort_keys=True, ensure_ascii=False)
except Exception:
# at this point we have given up. Just don't error out
try:
return unicode(value)
except Exception:
return u'\N{REPLACEMENT CHARACTER}'
6 changes: 1 addition & 5 deletions ckan/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,8 @@ def get_validator(validator):
converters = _import_module_functions('ckan.logic.converters')
_validators_cache.update(converters)

for plugin in p.PluginImplementations(p.IValidators):
for plugin in reversed(list(p.PluginImplementations(p.IValidators))):
for name, fn in plugin.get_validators().items():
if name in _validators_cache:
raise NameConflict(
'The validator %r is already defined' % (name,)
)
log.debug('Validator function {0} from plugin {1} was inserted'
.format(name, plugin.name))
_validators_cache[name] = fn
Expand Down
Loading

0 comments on commit 6edfaa3

Please sign in to comment.