Menu

#115 Schema with UnicodeString item behaves unclearly

open
nobody
None
5
2011-10-15
2011-02-13
Max
No

Here's an example.

# =====================
from formencode import Schema, Invalid
from formencode.validators import UnicodeString, Int
from webob import Request

class StrictSchema(Schema):
allow_extra_fields = False

class IntegerTestSchema(StrictSchema):
testfield = Int(not_empty=True)

class StringTestSchema(StrictSchema):
testfield = UnicodeString(not_empty=True)

# Testing.
# =====================
req = Request.blank('/?testfield=111')
print IntegerTestSchema.to_python(req.params)

# This raises an exception
req = Request.blank('/?testfield=111&testfield=222')
try:
IntegerTestSchema.to_python(req.params)
except Invalid as e:
print "Caught Exception: {0}".format(e)

req = Request.blank('/?testfield=aaa')
print StringTestSchema.to_python(req.params)

# This will be passed successfully (!)
# The output will be {'testfield': u"[u'aaa', u'bbb']"}
req = Request.blank('/?testfield=aaa&testfield=bbb')
print StringTestSchema.to_python(req.params)

# ========================

Please note we do not use formencode.ForEach() or formencode.Set()
here. I think this is very unclear behaviour.

This behavior happens because mixed is used here - https://bitbucket.org/ianb/formencode/src/d95237b33f3c/formencode/api.py#cl-403
For example, if we'd have an unified behaviour for all single-value validators (Int, UnicodeString, Bool etc.) we could get "[u'John', u'Mike']" result only for "/?username=[u'John', u'Mike']" request. But now, we can get the same result by the two different requests - by "/?username=[u'John', u'Mike']" and by "/? username=John&username=Mike". This shouldn't be allowed.

Original discussion has been started here - http://groups.google.com/group/pylons-discuss/browse_thread/thread/ccbfeac4c12d3a9b

Discussion

  • Chris Lambacher

    Chris Lambacher - 2011-10-15
    • labels: --> 2534855
     
  • Chris Lambacher

    Chris Lambacher - 2011-10-15
    • labels: 2534855 -->
     

Log in to post a comment.