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

Feature Request: Truthy Validation Rule #262

Closed
dac514 opened this issue Jan 22, 2015 · 7 comments
Closed

Feature Request: Truthy Validation Rule #262

dac514 opened this issue Jan 22, 2015 · 7 comments

Comments

@dac514
Copy link

dac514 commented Jan 22, 2015

Hi,

Is it possible to create a "Truthy" validator?

That is, for my purposes, the following are all OK: true, "true", "TRUE", "True", 1, "1"

The code would look like this:

<?php
namespace Respect\Validation\Rules;

class Truthy extends AbstractRule
{
    public function validate($input)
    {
        if (is_bool($input)) {
            return $input == true;
        }

        if (is_numeric($input)) {
            return (int)$input == 1;
        }

        if (is_string($input)) {
            $input = strtolower($input);

            return $input == 'true';
        }

        return false;
    }
}

Thank you for your consideration.

@henriquemoody
Copy link
Member

You can use v::in(array(true, 'true'))->validete(strtolower($input));, since 1 == true, '1' == true, true == true, and 'true' == 'true'.

To be honest I don't see much usage for Truthy validator, can you give us some use cases?

@alganet
Copy link
Member

alganet commented Jan 22, 2015

I can see the reasoning behind this. The FILTER_VALIDATE_BOOLEAN considers even "on" and "yes" as valid booleanish.

Perhaps we could provide a proxy for all FILTER_VALIDATE_ validators. That could grasp a lot of use cases.

@dac514
Copy link
Author

dac514 commented Jan 22, 2015

Hi @henriquemoody

I'm building a REST API using SLIM. I need to validate /path/to/list?is_active=true

If I do: v::bool()->check($app->request->get('is_active', false));

I get: _error: ""true" must be a boolean",_

With /path/to/list?is_active=false

If I do your example: v::in(array(true, 'true'))->check(strtolower($app->request->get('is_active', false)));

I get: TRUE (I expect false)

And so on.

I agree with @alganet

Thank you for your consideration

@henriquemoody
Copy link
Member

We already have yes() and no(), I'm not good with names at all, maybe we can create trueValue() and falseValue() (we already have nullValue(), were the idea came from).

Both in insensitive case:

trueValue falseValue
1 0
true false
'1' '0'
'on' 'off'
'true' 'false'
'yes' 'no'

What you think?

@dac514
Copy link
Author

dac514 commented Jan 22, 2015

I like it.

👍

@henriquemoody
Copy link
Member

I will send a PR tomorrow! 😄

@henriquemoody
Copy link
Member

@alganet, I've created filterVar() rule. Take a look on #267.

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

No branches or pull requests

3 participants