Debian Codegraph @

gavo/formal/validation.py

raw
1 import re
2 from zope.interface import implementer
3 from . import iformal
4
5
6 class FormsError(Exception):
7 """
8 Base class for all Forms errors. A single string, message, is accepted and
9 stored as an attribute.
10
11 The message is not passed on to the Exception base class because it doesn't
12 seem to be able to handle unicode at all.
13 """
14 def __init__(self, message):
15 Exception.__init__(self, message)
16 self.message = message
17
18
19 class FormError(FormsError):
20 """
21 Form validation error. Raise this, typically from a submit callback, to
22 signal that the form (not an individual field) failed to validate.
23 """
24 pass
25
26
27 class FieldError(FormsError):
28 """
29 Base class for field-related exceptions. The failure message and the failing
30 field name are stored as attributes.
31 """
32 def __init__(self, message, fieldName=None):
33 FormsError.__init__(self, message)
34 self.fieldName = fieldName
35
36
37 class FieldValidationError(FieldError):
38 """
39 Exception that signals that a field failed to validate.
40 """
41 pass
42
43
44 class FieldRequiredError(FieldValidationError):
45 """
46 Exception that signals that a field that is marked as required was not
47 entered.
48 """
49 pass
50
51
52 @implementer(iformal.IValidator)
53 class RequiredValidator(object):
54
55 def validate(self, field, value):
56 if value is None:
57 raise FieldRequiredError('Required')
58
59
60 @implementer(iformal.IValidator)
61 class LengthValidator(object):
62 """Validate the length of the value is within a given range.
63 """
64
65 def __init__(self, min=None, max=None):
66 self.min = min
67 self.max = max
68 assert self.min is not None or self.max is not None
69
70 def validationErrorMessage(self, field):
71 if self.min is not None and self.max is None:
72 return 'Must be longer than %r characters'%(self.min,)
73 if self.min is None and self.max is not None:
74 return 'Must be shorter than %r characters'%(self.max,)
75 return 'Must be between %r and %r characters'%(self.min, self.max)
76
77 def validate(self, field, value):
78 if value is None:
79 return
80 length = len(value)
81 if self.min is not None and length < self.min:
82 raise FieldValidationError(self.validationErrorMessage(field))
83 if self.max is not None and length > self.max:
84 raise FieldValidationError(self.validationErrorMessage(field))
85
86
87 @implementer(iformal.IValidator)
88 class RangeValidator(object):
89 """Validate the size of the value is within is given range.
90 """
91
92 def __init__(self, min=None, max=None):
93 self.min = min
94 self.max = max
95 assert self.min is not None or self.max is not None
96
97 def validationErrorMessage(self, field):
98 if self.min is not None and self.max is None:
99 return 'Must be greater than %r'%(self.min,)
100 if self.min is None and self.max is not None:
101 return 'Must be less than %r'%(self.max,)
102 return 'Must be between %r and %r'%(self.min, self.max)
103
104 def validate(self, field, value):
105 if value is None:
106 return
107 if self.min is not None and value < self.min:
108 raise FieldValidationError(self.validationErrorMessage(field))
109 if self.max is not None and value > self.max:
110 raise FieldValidationError(self.validationErrorMessage(field))
111
112
113 @implementer(iformal.IValidator)
114 class PatternValidator(object):
115 """Validate the value is a certain pattern.
116
117 The required pattern is defined as a regular expression. The regex will be
118 compiled automatically if necessary.
119 """
120
121 def __init__(self, regex):
122 self.regex = regex
123
124 def validate(self, field, value):
125 if value is None:
126 return
127 # If it doesn't look like a regex object then compile it now
128 if not hasattr(self.regex, 'match'):
129 self.regex = re.compile(self.regex)
130 if self.regex.match(value) is None:
131 raise FieldValidationError('Invalid format')
132
133
134
135 @implementer(iformal.IValidator)
136 class CallableValidator(object):
137 """
138 A validator that delegates the validation of non-None values to a callable
139 with the same signature as IValidator.validate.
140 """
141
142
143
144 def __init__(self, callable):
145 self.callable = callable
146
147
148 def validate(self, field, value):
149 if value is None:
150 return
151 self.callable(field, value)
152
153
154
155 __all__ = [
156 'FormError', 'FieldError', 'FieldValidationError', 'FieldRequiredError',
157 'RequiredValidator', 'LengthValidator', 'RangeValidator', 'PatternValidator',
158 'CallableValidator',
159 ]
160