Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce options #5

Open
jdesrosiers opened this issue Jan 26, 2014 · 6 comments
Open

Coerce options #5

jdesrosiers opened this issue Jan 26, 2014 · 6 comments

Comments

@jdesrosiers
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link

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
Copy link

stoiev commented May 7, 2014

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
Copy link
Owner

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants