Package muntjac :: Package data :: Module validator
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.validator

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Interface that implements a method for validating an object.""" 
 17   
 18  from muntjac.terminal.error_message import IErrorMessage 
 19   
 20  from muntjac.terminal.gwt.server.abstract_application_servlet \ 
 21      import AbstractApplicationServlet 
 22   
 23   
24 -class IValidator(object):
25 """Interface that implements a method for validating if an L{object} is 26 valid or not. 27 28 Implementors of this class can be added to any L{IValidatable} implementor 29 to verify its value. 30 31 L{isValid} and L{validate} can be used to check if a value is valid. 32 L{isValid} and L{validate} must use the same validation logic so that iff 33 L{isValid} returns false, L{validate} throws an L{InvalidValueException}. 34 35 Validators must not have any side effects. 36 37 @author: Vaadin Ltd. 38 @author: Richard Lincoln 39 @version: 1.1.2 40 """ 41
42 - def validate(self, value):
43 """Checks the given value against this validator. If the value is valid 44 the method does nothing. If the value is invalid, an 45 L{InvalidValueException} is thrown. 46 47 @param value: 48 the value to check 49 @raise InvalidValueException: 50 if the value is invalid 51 """ 52 raise NotImplementedError
53 54
55 - def isValid(self, value):
56 """Tests if the given value is valid. This method must be symmetric 57 with L{validate} so that L{validate} throws an error iff this method 58 returns false. 59 60 @param value: 61 the value to check 62 @return: C{True} if the value is valid, C{False} otherwise. 63 """ 64 raise NotImplementedError
65 66
67 -class InvalidValueException(RuntimeError, IErrorMessage):
68 """Exception that is thrown by a L{IValidator} when a value is invalid. 69 70 The default implementation of InvalidValueException does not support HTML 71 in error messages. To enable HTML support, override L{getHtmlMessage} and 72 use the subclass in validators. 73 74 @author: Vaadin Ltd. 75 @author: Richard Lincoln 76 @version: 1.1.2 77 """ 78
79 - def __init__(self, message, causes=None):
80 """Constructs a new C{InvalidValueException} with a set of causing 81 validation exceptions. The causing validation exceptions are included 82 when the exception is painted to the client. 83 84 @param message: 85 The detail message of the problem. 86 @param causes: 87 One or more C{InvalidValueException}s that caused 88 this exception. 89 """ 90 super(InvalidValueException, self).__init__(message) 91 92 # Array of one or more validation errors that are causing this 93 # validation error. 94 if causes is not None: 95 self._causes = causes 96 else: 97 self._causes = list()
98 99
100 - def isInvisible(self):
101 """Check if the error message should be hidden. 102 103 An empty (null or "") message is invisible unless it contains nested 104 exceptions that are visible. 105 106 @return: true if the error message should be hidden, false otherwise 107 """ 108 msg = self.message 109 110 if msg is not None and len(msg) > 0: 111 return False 112 113 if self._causes is not None: 114 for i in range(len(self._causes)): 115 if not self._causes[i].isInvisible(): 116 return False 117 118 return True
119 120
121 - def getErrorLevel(self):
122 return IErrorMessage.ERROR
123 124
125 - def paint(self, target):
126 target.startTag('error') 127 target.addAttribute('level', 'error') 128 129 # Error message 130 message = self.getHtmlMessage() 131 if message is not None: 132 target.addText(message) 133 134 # Paint all the causes 135 for i in range(len(self._causes)): 136 self._causes[i].paint(target) 137 138 target.endTag('error')
139 140
141 - def getHtmlMessage(self):
142 """Returns the message of the error in HTML. 143 144 Note that this API may change in future versions. 145 """ 146 return AbstractApplicationServlet.safeEscapeForHtml(self.message)
147 148
149 - def addListener(self, listener, iface=None):
150 pass
151 152
153 - def addCallback(self, callback, eventType=None, *args):
154 pass
155 156
157 - def removeListener(self, listener, iface=None):
158 pass
159 160
161 - def removeCallback(self, callback, eventType=None):
162 pass
163 164
165 - def requestRepaint(self):
166 pass
167 168
169 - def requestRepaintRequests(self):
170 pass
171 172
173 - def getDebugId(self):
174 return None
175 176
177 - def setDebugId(self, idd):
178 raise NotImplementedError('InvalidValueException cannot have ' 179 'a debug id')
180 181
182 - def getCauses(self):
183 """Returns the C{InvalidValueExceptions} that caused this 184 exception. 185 186 @return: An array containing the C{InvalidValueExceptions} that 187 caused this exception. Returns an empty array if this 188 exception was not caused by other exceptions. 189 """ 190 return self._causes
191 192
193 -class EmptyValueException(InvalidValueException):
194 """A specific type of L{InvalidValueException} that indicates that 195 validation failed because the value was empty. What empty means is up to 196 the thrower. 197 198 @author: Vaadin Ltd. 199 @author: Richard Lincoln 200 @version: 1.1.2 201 """ 202
203 - def __init__(self, message):
204 super(EmptyValueException, self).__init__(message)
205