diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 23:40:34 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 23:40:34 +0000 |
commit | 5da1d4bb86fadc12e2becf05239b0177d42ce454 (patch) | |
tree | 0c0e8d10cf62388c0cf2c9d3c9fe60e6bbd8f8e4 /ipalib/ipa_types.py | |
parent | 8fbc01ca864df332afe16ed51ea661ae88892d8b (diff) | |
download | freeipa-5da1d4bb86fadc12e2becf05239b0177d42ce454.tar.gz freeipa-5da1d4bb86fadc12e2becf05239b0177d42ce454.tar.xz freeipa-5da1d4bb86fadc12e2becf05239b0177d42ce454.zip |
207: Added Unicode.validate() method and corresponding unit tests
Diffstat (limited to 'ipalib/ipa_types.py')
-rw-r--r-- | ipalib/ipa_types.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py index f0baf1ba6..3cae7af57 100644 --- a/ipalib/ipa_types.py +++ b/ipalib/ipa_types.py @@ -83,6 +83,8 @@ class Int(Type): class Unicode(Type): + type = unicode + def __init__(self, min_length=None, max_length=None, pattern=None): check_min_max(min_length, max_length, 'min_length', 'max_length') if min_length is not None and min_length < 0: @@ -100,3 +102,17 @@ class Unicode(Type): self.regex = None else: self.regex = re.compile(pattern) + lock(self) + + def validate(self, value): + if type(value) is not self.type: + return 'Must be a string' + + if self.regex and self.regex.match(value) is None: + return 'Must match %r' % self.pattern + + if self.min_length is not None and len(value) < self.min_length: + return 'Must be at least %d characters long' % self.min_length + + if self.max_length is not None and len(value) > self.max_length: + return 'Can be at most %d characters long' % self.max_length |