validators.String validates []

Felix Schwarz <felix.schwarz-S0/[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Hi,

I just found an unexpected (at least be me) behavior of formencode:
String(not_empty=True).to_python passes with None, [] and even "".

I read the class documentation which says: "Converts things to string,
but treats empty things as the empty string."
But the docs are wrong in this regard:
   String().to_python(2) returns an int

Furthermore I expected that everything besides str and unicode would be
rejected.

Is this behavior really what the String validator should give us? If the
current implementation is considered correct, the validator is basically
useless to me (no offense meant! I think formencode is a really nice
piece of software!) .

Test case attached.

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
test_string_validator.py (application/x-crossover-py, 1 KB)
from formencode.validators import String, Invalid

def validate(validator, value):
    try:
        validator.to_python(value)
        return None
    except Invalid, e:
        return e.unpack_errors()

messages = String().message

def test_sv_min():
    sv = String(min=2)
    assert sv.to_python("foo") == "foo"
    assert validate(sv, "") == messages('tooShort', None, min=2)
    assert validate(sv, None) == messages('tooShort', None, min=2)
    # should be completely invalid?
    assert validate(sv, []) == messages('tooShort', None, min=2)
    assert validate(sv, ['', '']) == messages('tooShort', None, min=2)

def test_sv_not_empty():
    sv = String(not_empty=True)
    assert validate(sv, "") == messages('empty', None)
    assert validate(sv, None) == messages('empty', None)
    # should be completely invalid?
    assert validate(sv, []) == messages('empty', None)
    assert validate(sv, {}) == messages('empty', None)

def test_sv_string_conversion():
    sv = String(not_empty=False)
    assert sv.to_python(2) == "2"
    assert sv.to_python([]) == ""
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.