From 33802ab71262e01704d3342761215ea480354e88 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Mon, 11 Oct 2010 22:27:57 -0400 Subject: Use context to decide which name to return on RequirementsErrors When a Requirement fails we throw an exception including the name of the field that is missing. To make the command-line friendlier we have a cli_name defined which may or may not match the LDAP attribute. This can be confusing if you are using ipalib directly because the attribute name missing may not match what is actually required (desc vs description is a good example). If you use the context 'cli' then it will throw exceptions using cli_name. If you use any other context it will use the name of the attribute. ticket 187 --- ipalib/parameters.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'ipalib/parameters.py') diff --git a/ipalib/parameters.py b/ipalib/parameters.py index a06c8da2c..862c7593c 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -476,7 +476,10 @@ class Param(ReadOnly): value = self.get_default(**kw) else: value = self.convert(self.normalize(value)) - self.validate(value) + if hasattr(self, 'env'): + self.validate(value, self.env.context) + else: + self.validate(value) return value def kw(self): @@ -696,15 +699,19 @@ class Param(ReadOnly): error=ugettext(self.type_error), ) - def validate(self, value): + def validate(self, value, context=None): """ Check validity of ``value``. :param value: A proposed value for this parameter. + :param context: The context we are running in. """ if value is None: if self.required: - raise RequirementError(name=self.cli_name) + if context == 'cli': + raise RequirementError(name=self.cli_name) + else: + raise RequirementError(name=self.name) return if self.query: return @@ -1324,9 +1331,9 @@ class StrEnum(Enum): For example: >>> enum = StrEnum('my_enum', values=(u'One', u'Two', u'Three')) - >>> enum.validate(u'Two') is None + >>> enum.validate(u'Two', 'cli') is None True - >>> enum.validate(u'Four') + >>> enum.validate(u'Four', 'cli') Traceback (most recent call last): ... ValidationError: invalid 'my_enum': must be one of (u'One', u'Two', u'Three') -- cgit