jsv4-php icon indicating copy to clipboard operation
jsv4-php copied to clipboard

Coerce options

Open jdesrosiers opened this issue 11 years ago • 6 comments

I'm having an issue with the coerce functionality. The way I see it, this function does three things: type conversion, filling in defaults, and filling in required properties. When I use the coerce function, I sometimes want type conversion, I always want defaults, and I never want filling in required properties. One way to deal with this would be to add an options parameter the coerce function. This would allow the user to fine tune the coerce features that are applied. Another approach would be to factor each of the coercion features out in separate public functions. Developers could then use those functions to coerce their data how they want before passing it to the validate function. The coerce function would use these functions internally. What do you think?

jdesrosiers avatar Jan 26 '14 06:01 jdesrosiers

I think an options parameter seems best to me. :)

Out of curiosity, why do you sometimes not want type-conversion? (I can understand not filling in required properties.)

geraintluff avatar Jan 26 '14 15:01 geraintluff

The only time I want to use type conversion is when I am validating $_GET input because there is no way for a user to explicitly declare their types. However, if the input is JSON and there is a public schema defining what the types should be, I think I should be enforcing those types. It comes down to preference really, but type conversion isn't a really a big concern for me. The important part of this issue is that I only want properties added if there is a default value is specified in the schema. I would be happy if the only change was to remove the filling in required properties functionality.

jdesrosiers avatar Jan 26 '14 17:01 jdesrosiers

Was going to open a similar issue, we noticed the same thing. If a value is marked as required, but no default is provided, then that seems like some invalid JSON. Otherwise there's not much of a point to a required item.

The library used to actually function this way, I think a lucky bug from the lack of

private $coerce;

We added a similar parameter as jdesrosiers suggested to our fork. An options array sounds like a better idea though.

passthefist avatar Jan 29 '14 06:01 passthefist

First, thanks for this piece of software =)

A possible workaround before an improvement related to this issue:

$get_obj = (object) $_GET;
$get_coerced = Jsv4::coerce($get_obj, $schema);
foreach($get_obj as $key => $value){
    if(isset($get_coerced->value->$key)){
        $get_obj->$key = $get_coerced->value->$key;
    }
}
$result = Jsv4::validate($get_obj, $schema);

stoiev avatar May 07 '14 17:05 stoiev

Sorry for being silent for so long. I don't actively do any PHP development any more, so I wasn't really set up for it.

Is there still demand for this? (Asking before I take a swing at it.)

geraintluff avatar Oct 07 '14 13:10 geraintluff

I'm not actively doing PHP development at the moment either. So, don't go out of your way just for me.

jdesrosiers avatar Oct 08 '14 04:10 jdesrosiers