khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 30 forks source link

Boolean and JSON #22

Closed LittleFox94 closed 7 years ago

LittleFox94 commented 7 years ago

Using Boolean params with JSON does not work because the validation fails. JSON returns JSON::true/JSON::false for true/false values, so these may not be recognized by the validator.

Idea for solution: Convert JSON::true/JSON::false values in Bool type parameters to 1/0 before validating them.

khrt commented 7 years ago

It's hard to say something with no context but I think it shouldn't be fixed somewhere in the framework. If I understand your issue you should use proper type to validate your values.

You can extend a Bool type from Types::Standard, please check this manual https://metacpan.org/pod/Moose::Manual::Types

Hope it will help you.

LittleFox94 commented 7 years ago

OK I will check - thank you (also for the other bugs) :)

I still think this is something the framework should do (even if just giving a proper Bool type) because this will imho affect other users trying to have bool parameters with JSON serialization.

Or is this something that only affects me?

khrt commented 7 years ago

It should affect everyone who use boolean values. It's how JSON module works so I think there is nothing to do with the framework.

Buy just to clarify the case could you please describe what do you do more detailed?

ak

On 14 Nov 2016 10:23 a.m., "Moritz Grosch (LittleFox)" < notifications@github.com> wrote:

OK I will check - thank you (also for the other bugs) :)

I still think this is something the framework should do (even if just giving a proper Bool type) because this will imho affect other users trying to have bool parameters with JSON serialization.

Or is this something that only affects me?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/khrt/Raisin/issues/22#issuecomment-260285912, or mute the thread https://github.com/notifications/unsubscribe-auth/AAs7Z3aW5MGDqaYgc5VheppbvGFDh-_Pks5q-ChwgaJpZM4Kw24e .

LittleFox94 commented 7 years ago

Being a new user to this Framework, first thing I tried to do was something like this (out of my head):

resource foo => sub {
    params(optional('bar', type => Bool, desc => 'Boolean value', default => 0));
    post sub {
        # do something
    };
};

Writing it like this, the parameter never got to my code because it was flagged as invalid and removed from the params. My client delivered data as application/json.

As far as I understand, this is because of the non-Raisin types which are for plain perl usage and not JSON compatible. I can of course implement the correct type for my self, but I think this would be useful for all users (well, those who are using bool parameters).

I hope I don't confuse something here. :D