max parameter for Email validator

Felix Schwarz <felix.schwarz-S0/[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
I just noticed that the Email validator does not have a "max" parameter to limit the
maximum string length. Attached is a patch which I quickly hacked together. At least
test case should be usable. Furthermore  I added a line to set the formencode translation
to en because the test fails otherwise on my system with a de_DE locale.

fs

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
email_max.patch (text/x-patch, 2.3 KB)
Index: formencode/validators.py
===================================================================
--- formencode/validators.py	(Revision 3126)
+++ formencode/validators.py	(Arbeitskopie)
@@ -1247,10 +1247,14 @@
         Invalid: The domain of the email address does not exist (the portion after the @: thisdomaindoesnotexistithinkforsure.com)
         >>> e = Email(not_empty=False)
         >>> e.to_python('')
-        
+        >>> e = Email(max=250)
+        >>> e.to_python('x'*300)
+        Traceback (most recent call last):
+        Invalid: Enter a value less than 250 characters long
     """ 
 
     resolve_domain = False
+    max = None
 
     usernameRE = re.compile(r"^[^ \t\n\r@<>()]+$", re.I)
     domainRE = re.compile(r"^[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$", re.I)
@@ -1280,6 +1284,8 @@
             raise Invalid(
                 self.message('empty', state),
                 value, state)
+        if max != None:
+            String(max=self.max).validate_other(value, state=state)
         value = value.strip()
         splitted = value.split('@', 1)
         try:
Index: tests/test_validators.py
===================================================================
--- tests/test_validators.py	(Revision 3126)
+++ tests/test_validators.py	(Arbeitskopie)
@@ -1,11 +1,13 @@
-from formencode.validators import String, UnicodeString, Invalid, Int
+from formencode.validators import String, UnicodeString, Invalid, Int, Email
 from formencode.validators import DateConverter
 from formencode.variabledecode import NestedVariables
 from formencode.schema import Schema
 from formencode.foreach import ForEach
 import datetime
-from formencode.api import NoDefault
+from formencode.api import NoDefault, set_stdtranslation
 
+set_stdtranslation(domain="FormEncode", languages=["en"])
+
 def validate(validator, value):
     try:
         return validator.to_python(value)
@@ -127,3 +129,13 @@
     else:
         raise Exception("Shouldn't be valid data", values, start_values)
 
+
+def test_email_max():
+    email = Email(resolve_domain=False, max=250)
+    try:
+        mail = '[email protected]' + "x"*260
+        email.to_python(mail)
+        raise Exception("Shouldn't be valid data", mail)
+    except Invalid, e:
+        assert 'Enter a value less than 250 characters long' in str(e)
+
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.