diff options
author | Martin Kosek <mkosek@redhat.com> | 2012-09-27 14:18:02 +0200 |
---|---|---|
committer | Martin Kosek <mkosek@redhat.com> | 2012-10-01 13:39:22 +0200 |
commit | 988ea368272822f2153563ad34554240e3377d60 (patch) | |
tree | da2cdc48f8631e244ffc244efc6a230c5dbcf49f /ipalib/parameters.py | |
parent | c16c257145e44599df5b833954fc84f946546ea1 (diff) | |
download | freeipa-988ea368272822f2153563ad34554240e3377d60.tar.gz freeipa-988ea368272822f2153563ad34554240e3377d60.tar.xz freeipa-988ea368272822f2153563ad34554240e3377d60.zip |
Improve StrEnum validation error message
Do not print list of possible values as "%r" but simply as a list
of quoted values which should make it easier to read for users.
Also add a special case when there is just one allowed value.
https://fedorahosted.org/freeipa/ticket/2869
Diffstat (limited to 'ipalib/parameters.py')
-rw-r--r-- | ipalib/parameters.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 53756a80a..b3a75f288 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -1595,12 +1595,17 @@ class Enum(Param): TYPE_ERROR % (n, self.type, v, type(v)) ) + if len(self.values) < 1: + raise ValueError( + '%s: list of values must not be empty' % self.nice) + def _rule_values(self, _, value, **kw): if value not in self.values: - return _('must be one of %(values)r') % dict( - values=self.values, - ) - + if len(self.values) == 1: + return _("must be '%(value)s'") % dict(value=self.values[0]) + else: + values = u', '.join("'%s'" % value for value in self.values) + return _('must be one of %(values)s') % dict(values=values) class BytesEnum(Enum): """ @@ -1622,7 +1627,7 @@ class StrEnum(Enum): >>> enum.validate(u'Four', 'cli') Traceback (most recent call last): ... - ValidationError: invalid 'my_enum': must be one of (u'One', u'Two', u'Three') + ValidationError: invalid 'my_enum': must be one of 'One', 'Two', 'Three' """ type = unicode |