PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/distlib/wtforms/validators.py

https://github.com/sgammon/Young-Voter-Revolution
Python | 329 lines | 322 code | 4 blank | 3 comment | 3 complexity | bd00babb9b732786d3ec810cab0a421b MD5 | raw file
  1. import re
  2. __all__ = (
  3. 'Email', 'email', 'EqualTo', 'equal_to', 'IPAddress', 'ip_address',
  4. 'Length', 'length', 'NumberRange', 'number_range', 'Optional', 'optional',
  5. 'Required', 'required', 'Regexp', 'regexp', 'URL', 'url', 'AnyOf',
  6. 'any_of', 'NoneOf', 'none_of'
  7. )
  8. class ValidationError(ValueError):
  9. """
  10. Raised when a validator fails to validate its input.
  11. """
  12. def __init__(self, message=u'', *args, **kwargs):
  13. ValueError.__init__(self, message, *args, **kwargs)
  14. class StopValidation(Exception):
  15. """
  16. Causes the validation chain to stop.
  17. If StopValidation is raised, no more validators in the validation chain are
  18. called. If raised with a message, the message will be added to the errors
  19. list.
  20. """
  21. def __init__(self, message=u'', *args, **kwargs):
  22. Exception.__init__(self, message, *args, **kwargs)
  23. class EqualTo(object):
  24. """
  25. Compares the values of two fields.
  26. :param fieldname:
  27. The name of the other field to compare to.
  28. :param message:
  29. Error message to raise in case of a validation error. Can be
  30. interpolated with `%(other_label)s` and `%(other_name)s` to provide a
  31. more helpful error.
  32. """
  33. def __init__(self, fieldname, message=None):
  34. self.fieldname = fieldname
  35. self.message = message
  36. def __call__(self, form, field):
  37. try:
  38. other = form[self.fieldname]
  39. except KeyError:
  40. raise ValidationError(field.gettext(u"Invalid field name '%s'.") % self.fieldname)
  41. if field.data != other.data:
  42. d = {
  43. 'other_label': hasattr(other, 'label') and other.label.text or self.fieldname,
  44. 'other_name': self.fieldname
  45. }
  46. if self.message is None:
  47. self.message = field.gettext(u'Field must be equal to %(other_name)s.')
  48. raise ValidationError(self.message % d)
  49. class Length(object):
  50. """
  51. Validates the length of a string.
  52. :param min:
  53. The minimum required length of the string. If not provided, minimum
  54. length will not be checked.
  55. :param max:
  56. The maximum length of the string. If not provided, maximum length
  57. will not be checked.
  58. :param message:
  59. Error message to raise in case of a validation error. Can be
  60. interpolated using `%(min)d` and `%(max)d` if desired. Useful defaults
  61. are provided depending on the existence of min and max.
  62. """
  63. def __init__(self, min=-1, max=-1, message=None):
  64. assert min != -1 or max!=-1, 'At least one of `min` or `max` must be specified.'
  65. assert max == -1 or min <= max, '`min` cannot be more than `max`.'
  66. self.min = min
  67. self.max = max
  68. self.message = message
  69. def __call__(self, form, field):
  70. l = field.data and len(field.data) or 0
  71. if l < self.min or self.max != -1 and l > self.max:
  72. if self.message is None:
  73. if self.max == -1:
  74. self.message = field.ngettext(u'Field must be at least %(min)d character long.',
  75. u'Field must be at least %(min)d characters long.', self.min)
  76. elif self.min == -1:
  77. self.message = field.ngettext(u'Field cannot be longer than %(max)d character.',
  78. u'Field cannot be longer than %(max)d characters.', self.max)
  79. else:
  80. self.message = field.gettext(u'Field must be between %(min)d and %(max)d characters long.')
  81. raise ValidationError(self.message % dict(min=self.min, max=self.max))
  82. class NumberRange(object):
  83. """
  84. Validates that a number is of a minimum and/or maximum value, inclusive.
  85. This will work with any comparable number type, such as floats and
  86. decimals, not just integers.
  87. :param min:
  88. The minimum required value of the number. If not provided, minimum
  89. value will not be checked.
  90. :param max:
  91. The maximum value of the number. If not provided, maximum value
  92. will not be checked.
  93. :param message:
  94. Error message to raise in case of a validation error. Can be
  95. interpolated using `%(min)s` and `%(max)s` if desired. Useful defaults
  96. are provided depending on the existence of min and max.
  97. """
  98. def __init__(self, min=None, max=None, message=None):
  99. self.min = min
  100. self.max = max
  101. self.message = message
  102. def __call__(self, form, field):
  103. data = field.data
  104. if data is None or (self.min is not None and data < self.min) or \
  105. (self.max is not None and data > self.max):
  106. if self.message is None:
  107. # we use %(min)s interpolation to support floats, None, and
  108. # Decimals without throwing a formatting exception.
  109. if self.max is None:
  110. self.message = field.gettext(u'Number must be greater than %(min)s.')
  111. elif self.min is None:
  112. self.message = field.gettext(u'Number must be less than %(max)s.')
  113. else:
  114. self.message = field.gettext(u'Number must be between %(min)s and %(max)s.')
  115. raise ValidationError(self.message % dict(min=self.min, max=self.max))
  116. class Optional(object):
  117. """
  118. Allows empty input and stops the validation chain from continuing.
  119. If input is empty, also removes prior errors (such as processing errors)
  120. from the field.
  121. """
  122. field_flags = ('optional', )
  123. def __call__(self, form, field):
  124. if field.raw_data is None or not field.raw_data or not field.raw_data[0].strip():
  125. field.errors[:] = []
  126. raise StopValidation()
  127. class Required(object):
  128. """
  129. Validates that the field contains data. This validator will stop the
  130. validation chain on error.
  131. :param message:
  132. Error message to raise in case of a validation error.
  133. """
  134. field_flags = ('required', )
  135. def __init__(self, message=None):
  136. self.message = message
  137. def __call__(self, form, field):
  138. if not field.data or isinstance(field.data, basestring) and not field.data.strip():
  139. if self.message is None:
  140. self.message = field.gettext(u'This field is required.')
  141. field.errors[:] = []
  142. raise StopValidation(self.message)
  143. class Regexp(object):
  144. """
  145. Validates the field against a user provided regexp.
  146. :param regex:
  147. The regular expression string to use. Can also be a compiled regular
  148. expression pattern.
  149. :param flags:
  150. The regexp flags to use, for example re.IGNORECASE. Ignored if
  151. `regex` is not a string.
  152. :param message:
  153. Error message to raise in case of a validation error.
  154. """
  155. def __init__(self, regex, flags=0, message=None):
  156. if isinstance(regex, basestring):
  157. regex = re.compile(regex, flags)
  158. self.regex = regex
  159. self.message = message
  160. def __call__(self, form, field):
  161. if not self.regex.match(field.data or u''):
  162. if self.message is None:
  163. self.message = field.gettext(u'Invalid input.')
  164. raise ValidationError(self.message)
  165. class Email(Regexp):
  166. """
  167. Validates an email address. Note that this uses a very primitive regular
  168. expression and should only be used in instances where you later verify by
  169. other means, such as email activation or lookups.
  170. :param message:
  171. Error message to raise in case of a validation error.
  172. """
  173. def __init__(self, message=None):
  174. super(Email, self).__init__(r'^.+@[^.].*\.[a-z]{2,10}$', re.IGNORECASE, message)
  175. def __call__(self, form, field):
  176. if self.message is None:
  177. self.message = field.gettext(u'Invalid email address.')
  178. super(Email, self).__call__(form, field)
  179. class IPAddress(Regexp):
  180. """
  181. Validates an IP(v4) address.
  182. :param message:
  183. Error message to raise in case of a validation error.
  184. """
  185. def __init__(self, message=None):
  186. super(IPAddress, self).__init__(r'^([0-9]{1,3}\.){3}[0-9]{1,3}$', message=message)
  187. def __call__(self, form, field):
  188. if self.message is None:
  189. self.message = field.gettext(u'Invalid IP address.')
  190. super(IPAddress, self).__call__(form, field)
  191. class URL(Regexp):
  192. """
  193. Simple regexp based url validation. Much like the email validator, you
  194. probably want to validate the url later by other means if the url must
  195. resolve.
  196. :param require_tld:
  197. If true, then the domain-name portion of the URL must contain a .tld
  198. suffix. Set this to false if you want to allow domains like
  199. `localhost`.
  200. :param message:
  201. Error message to raise in case of a validation error.
  202. """
  203. def __init__(self, require_tld=True, message=None):
  204. tld_part = (require_tld and ur'\.[a-z]{2,10}' or u'')
  205. regex = ur'^[a-z]+://([^/:]+%s|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(\/.*)?$' % tld_part
  206. super(URL, self).__init__(regex, re.IGNORECASE, message)
  207. def __call__(self, form, field):
  208. if self.message is None:
  209. self.message = field.gettext(u'Invalid URL.')
  210. super(URL, self).__call__(form, field)
  211. class AnyOf(object):
  212. """
  213. Compares the incoming data to a sequence of valid inputs.
  214. :param values:
  215. A sequence of valid inputs.
  216. :param message:
  217. Error message to raise in case of a validation error. `%(values)s`
  218. contains the list of values.
  219. :param values_formatter:
  220. Function used to format the list of values in the error message.
  221. """
  222. def __init__(self, values, message=None, values_formatter=None):
  223. self.values = values
  224. self.message = message
  225. if values_formatter is None:
  226. values_formatter = lambda v: u', '.join(v)
  227. self.values_formatter = values_formatter
  228. def __call__(self, form, field):
  229. if field.data not in self.values:
  230. if self.message is None:
  231. self.message = field.gettext(u'Invalid value, must be one of: %(values)s.')
  232. raise ValueError(self.message % dict(values=self.values_formatter(self.values)))
  233. class NoneOf(object):
  234. """
  235. Compares the incoming data to a sequence of invalid inputs.
  236. :param values:
  237. A sequence of invalid inputs.
  238. :param message:
  239. Error message to raise in case of a validation error. `%(values)s`
  240. contains the list of values.
  241. :param values_formatter:
  242. Function used to format the list of values in the error message.
  243. """
  244. def __init__(self, values, message=None, values_formatter=None):
  245. self.values = values
  246. self.message = message
  247. if values_formatter is None:
  248. values_formatter = lambda v: u', '.join(v)
  249. self.values_formatter = values_formatter
  250. def __call__(self, form, field):
  251. if field.data in self.values:
  252. if self.message is None:
  253. self.message = field.gettext(u'Invalid value, can\'t be any of: %(values)s.')
  254. raise ValueError(self.message % dict(values=self.values_formatter(self.values)))
  255. email = Email
  256. equal_to = EqualTo
  257. ip_address = IPAddress
  258. length = Length
  259. number_range = NumberRange
  260. optional = Optional
  261. required = Required
  262. regexp = Regexp
  263. url = URL
  264. any_of = AnyOf
  265. none_of = NoneOf