summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-01-12 16:14:46 -0700
committerJason Gerard DeRose <jderose@redhat.com>2009-01-12 16:14:46 -0700
commit5c7c0b35bb2484efad2a8776b42fbf4066618706 (patch)
treec47c304de26396dbe611556a48aaba7119490f53 /ipalib
parent5e6ea11178f3a784c9cd589e958ef752890f8a21 (diff)
downloadfreeipa-5c7c0b35bb2484efad2a8776b42fbf4066618706.tar.gz
freeipa-5c7c0b35bb2484efad2a8776b42fbf4066618706.tar.xz
freeipa-5c7c0b35bb2484efad2a8776b42fbf4066618706.zip
New Param: added Param.validate() and Param._validate_scalar() methods; added corresponding unit tests
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/errors2.py16
-rw-r--r--ipalib/parameter.py35
2 files changed, 51 insertions, 0 deletions
diff --git a/ipalib/errors2.py b/ipalib/errors2.py
index b052882da..81b1fb2ed 100644
--- a/ipalib/errors2.py
+++ b/ipalib/errors2.py
@@ -465,9 +465,17 @@ class OptionError(InvocationError):
class RequirementError(InvocationError):
"""
**3005** Raised when a required parameter is not provided.
+
+ For example:
+
+ >>> raise RequirementError(name='givenname')
+ Traceback (most recent call last):
+ ...
+ RequirementError: 'givenname' is required
"""
errno = 3005
+ format = _('%(name)r is required')
class ConversionError(InvocationError):
@@ -481,9 +489,17 @@ class ConversionError(InvocationError):
class ValidationError(InvocationError):
"""
**3007** Raised when a parameter value fails a validation rule.
+
+ For example:
+
+ >>> raise ValidationError(name='sn', error='can be at most 128 characters')
+ Traceback (most recent call last):
+ ...
+ ValidationError: invalid 'sn': can be at most 128 characters
"""
errno = 3007
+ format = _('invalid %(name)r: %(error)s')
diff --git a/ipalib/parameter.py b/ipalib/parameter.py
index 204fda66f..0890160a0 100644
--- a/ipalib/parameter.py
+++ b/ipalib/parameter.py
@@ -25,6 +25,7 @@ from types import NoneType
from util import make_repr
from request import ugettext
from plugable import ReadOnly, lock, check_name
+from errors2 import RequirementError, ValidationError
from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
@@ -207,6 +208,7 @@ class Param(ReadOnly):
('primary_key', bool, False),
('normalizer', callable, None),
('default_from', callable, None),
+ ('create_default', callable, None),
('flags', frozenset, frozenset()),
# The 'default' kwarg gets appended in Param.__init__():
@@ -432,6 +434,39 @@ class Param(ReadOnly):
:param value: A proposed value for this parameter.
"""
+ if value is None:
+ if self.required:
+ raise RequirementError(name=self.name)
+ return
+ if self.multivalue:
+ if type(value) is not tuple:
+ raise TypeError(
+ TYPE_ERROR % ('value', tuple, value, type(value))
+ )
+ if len(value) < 1:
+ raise ValueError('value: empty tuple must be converted to None')
+ for (i, v) in enumerate(value):
+ self._validate_scalar(v, i)
+ else:
+ self._validate_scalar(value)
+
+ def _validate_scalar(self, value, index=None):
+ if type(value) is not self.type:
+ if index is None:
+ name = 'value'
+ else:
+ name = 'value[%d]' % index
+ raise TypeError(
+ TYPE_ERROR % (name, self.type, value, type(value))
+ )
+ if index is not None and type(index) is not int:
+ raise TypeError(
+ TYPE_ERROR % ('index', int, index, type(index))
+ )
+ for rule in self.all_rules:
+ error = rule(ugettext, value)
+ if error is not None:
+ raise ValidationError(name=self.name, error=error, index=index)
class Bool(Param):