Validation error

Create issue
Issue #6 resolved
Jonathan Adami created an issue

Hi there,

First I'd like to say thanks for wheezy.web, it's an awesome tool that I really like using on a day to day basis! Good work ;)

I just have a problem with the validation. It assumes that everything is strings and try to convert it to the value_provider based on the Model's default value.


But if you have a request with content type application/json, loaded here:

you end up with real types, so there's some type error.

I solved the problem by replacing

    value = values[name]


    value = unicode(values[name])

Probably not the solution you'd want I don't know... If I'm doing something wrong and you can point me to what it is, it'd be much appreciated!


Comments (3)

  1. Andriy Kornatskyy repo owner

    Thank you for your interest.

    The idea behind try_update_model is to adapt form input to model based on default values. So basically adapter converts a string to some type. As you pointed the JSON input already comes with types other than string (potentially valid) so that cause errors in adapters.

    The issue is known and here are two possible alternatives.

    1. The try_update_model should be aware that input type might not be a string. This way the existing adapters need to be improved. While your approach works with json input, it will not work in case of multiple html form submit (value can be a list).

    2. Do not use try_update_model.

    Read once again why you need it? To adapt form input to real types... but in case of json input you potentially already got what you expect in terms of types. More over the input structure might be very close to your model (you are not limited to flat key-value). That is why this approach is not really "appropriate" however possible.

    I have provided a example of simple API. Please note that you can still enforce correct types with validation (see here). Some advises for the RESTful API design can be found here.

  2. Jonathan Adami reporter

    Thanks for your answer.

    I like the fact that we can do pretty much anything with wheezy.web. Takes a bit of a learning curve but once you're in, it's pretty cool!

    I ended up with a design close to the ApiHandler, handling also OPTIONS and CORS, I didn't wanted to update your code, in case of updates and all, so I added something like this:

    def try_update_model(self, model, values=None):
        values = values or self.request.form
        values = {k: unicode(v) for k, v in values.iteritems()}
        return WWBaseHandler.try_update_model(self, model, values)

    I don't want to trust the json query to define types, and prefer to use the validation in place.

    I'll look into your examples though, I'm pretty sure I'll learn something there hehe

    Thanks again for your time!

  3. Andriy Kornatskyy repo owner

    The model value providers have been improved (no more assumption of string type). Since version 0.1.135.

  4. Log in to comment