geraintluff / jsv4-php

A (coercive) JSON Schema v4 Validator for PHP
Other
114 stars 31 forks source link

Coerce options #5

Open jdesrosiers opened 10 years ago

jdesrosiers commented 10 years ago

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?

geraintluff commented 10 years ago

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.)

jdesrosiers commented 10 years ago

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.

passthefist commented 10 years ago

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.

stoiev commented 10 years ago

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);
geraintluff commented 10 years ago

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.)

jdesrosiers commented 10 years ago

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