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

Source Code for Module muntjac.data.validators.abstract_string_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  """Validator base class for validating strings.""" 
17   
18  from muntjac.data.validators.abstract_validator import AbstractValidator 
19   
20   
21 -class AbstractStringValidator(AbstractValidator):
22 """Validator base class for validating strings. See L{AbstractValidator} 23 for more information. 24 25 To include the value that failed validation in the exception message you 26 can use "{0}" in the error message. This will be replaced with the failed 27 value (converted to string using L{__str__}) or "None" if the value is 28 C{None}. 29 30 @author: Vaadin Ltd. 31 @author: Richard Lincoln 32 @version: 1.1.2 33 """ 34
35 - def __init__(self, errorMessage):
36 """Constructs a validator for strings. 37 38 None and empty string values are always accepted. To reject empty 39 values, set the field being validated as required. 40 41 @param errorMessage: 42 the message to be included in an L{InvalidValueException} 43 (with "{0}" replaced by the value that failed validation). 44 """ 45 super(AbstractStringValidator, self).__init__(errorMessage)
46 47
48 - def isValid(self, value):
49 """Tests if the given value is a valid string. 50 51 None values are always accepted. Values that are not strings are 52 converted using L{__str__}. Then L{isValidString} is used to validate 53 the value. 54 55 @param value: 56 the value to check 57 @return: true if the value (or its __str__) is a valid string, false 58 otherwise 59 """ 60 if value is None: 61 return True 62 63 if not isinstance(value, str): 64 value = str(value) 65 66 return self.isValidString(value)
67 68
69 - def isValidString(self, value):
70 """Checks if the given string is valid. 71 72 @param value: 73 String to check. Can never be None. 74 @return: true if the string is valid, false otherwise 75 """ 76 pass
77