khrt / Raisin

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

Coerce value prior validation (fixes #32) #54

Closed khrt closed 5 years ago

slobo commented 5 years ago

Would coerce => 1 be a good default? Seems to me that when specifying the type, it would be intuitive that Raisin would do it's best to instantiate an object of that type without needing to ask it via coerce => 1

Ex, if I have this:

params(
  requires( period_start => ( type => 'DateTime' ) )
);
get fun($params) { ... }

I would expect $params->{period_start} to be a DateTime object.

Or would it break things in the wild? Sorry if I'm confusing the issue.

Thanks

mschout commented 5 years ago

There needs to be a small patch to make coerce work with Moose types. I'm going to submit a PR that builds on this one.

mschout commented 5 years ago

Related to @slobo's comment, I'm somewhat ambivalent about defaulting coerce to 1. In Moose, you have to explicitly say coerce => 1 if you want your attributes to use coercion. I am guessing there was a good reason this was done, but I don't know what that reason was :).

mschout commented 5 years ago

PR #56 makes coerce work for Moose types (with test). Thanks so much for implementing this!

slobo commented 5 years ago

My reasoning would be as follows:

† A good next step may be for Raisin to introspect Moose types and add the attribute constraints and documentation to swagger. In this case I can see using coerce => 0 as being useful if I'd like documentation to be generated, but still want to do some manual checks on the data before it turns into a real object. But that would still be exception rather than the rule in my code style. YMMV :)

khrt commented 5 years ago

I would go with coerce set to true by default, with ability to disable it.

mschout commented 5 years ago

I've updated the Moose patch to resolve the conflicts (and use coerce by default).