python - voluptuous unable to handle unicode string? -


i'm trying use voluptuous validate json input http request. however, doesn't seem handle unicode string well.

from voluptuous import schema, required pprint import pprint  schema = schema({     required('name'): str,     required('www'): str, })  data = {     'name': 'foo',     'www': u'http://www.foo.com', }  pprint(data) schema(data) 

the above code generates following error:

 voluptuous.multipleinvalid: expected str dictionary value @ data['www'] 

however, if remove u notation url, works fine. bug or doing wrong?

ps. i'm using python 2.7 if has it.

there 2 string types in python 2.7: str , unicode. in python 2.7 str type not unicode string, byte string.

so value u'http://www.foo.com' indeed not instance of type str , you're getting error. if wish support both str , unicode strings in python 2.7 you'd need change schema be:

from voluptuous import any, schema, required  schema = schema({     required('name'): any(str, unicode),     required('www'): any(str, unicode), }) 

or, simplicity, if receive unicode strings can use:

schema = schema({     required('name'): unicode,     required('www'): unicode, }) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -