auraphp/Aura.Filter

can't use validate rule 'blank' at "2.0.0"

Closed this issue · 35 comments

Hi, my name is Satomi. I'm using aurafilter, but upon doing composer update, I updated from "2.0.0-beta2" to "2.0.0", and it seems like the "blank" validation rule went away. I'm getting this error: Aura\Filter\Exception\RuleNotMapped: blank. Did the name of this validation rule change? Thank very much!

I think some of docs (validate.md) may need updating.
This section appears correct:
https://github.com/auraphp/Aura.Filter/blob/2.x/docs/subject-filter.md#creating-a-subject-filter

The release notes for 2.0.0-beta3 mention new isBlank function.
The tests might make it more clear.

yes @jakejohns is right.

You can use

$filter->validate('field')->isBlank();

It changed in beta3 actually : 3ded916 . Similar question : https://twitter.com/mbrevda/status/698853633291657217 and answers .

This is fixed with #108 .

I am closing for now, we can open if this is still a problem.

Thank you for reporting.

@jakejohns , @harikt
Thank you for your response!
Since isBlank throws an error when there's a fair present, how should I validate when something isn't blank?
Before I would have used $filter->validate('field')->isNot('blank');.
How should I write a validation to ensure an attribute is present?
I think I might be able to mess with the reverse property and do something, but is there a better way?

@satomif good question. In that case doesn't any of the validators can be applied ?

I will re-open the issue for I don't know a correct answer for I see this is removed from test : 3ded916#diff-6966a4109c3e93b304f2bc7b4c86c3d4L122

@satomif for the current time you may want to write a custom filter https://github.com/auraphp/Aura.Filter/blob/2.x/docs/custom.md#a-validate-class that checks if it is empty. I don't know other means. It seems we missed the functionality. Lets wait to hear from @pmjones if there is something else or we miss it completely.

Thank you.

@pmjones the PR #108 didn't completely fixed the issue. The question is how do we do

$filter->validate('field')->isNot('blank');

now ?

In theory, you should just validate against the rule you want the field to use. E.g., instead of this ...

$filter->validate('field')->isNot('blank');
$filter->validate('field')->is('whatever');

... you would only do this:

$filter->validate('field')->is('whatever');

The only issue would be if you want to validate that the field is blank, and not apply any other validations. That is, you want the field to be blank, only blank, and nothing but blank. Is that the case @satomif ?

Also, let me know if I'm missing something here.

@pmjones Thanks for the reply!
What I want to validate is that something is present—that it's not blank. Which validation should I use if I don't have anything else to validate about the field, other than its presence?

@satomif That's a great question. I do not have an answer. It seems like adding another validation called "exists", that is pretty much an empty piece of validation code, would be the right thing. Would that help?

@pmjones
I think that would replace the functionality I was getting with isNot('blank'). Please reference this issue if you add that validation. In the mean time, I can just use a custom validation that does nothing to achieve the same effect—is that right?

In the mean time, I can just use a custom validation that does nothing to achieve the same effect—is that right?

Yes, that sounds right. If/when you create it, send a PR here, and you'll get credit. Meanwhile, if you don't get around to a PR, I'll put one together myself later.

Sorry for the hassle, and thanks for the report!

@satomif I have just added the validation rule extant, which checks to see if the field exists in the subject, even if its value is null. Let me know if that solves your problem!

@satomif No, it doesn't work as expected; the ValidateSpec::applyRule() code gets in the way. Still working on it!

All right @satomif that should do it. Try the 'extant' rule and let me know how it goes!

@pmjones why did you named it extant and not exists ?

Because ->is('exists') and ->isNot('exists') doesn't sound right when you read it. ;-)

I considered "present" and "existent" but they didn't strike me the right way (though if others think one of them is preferable to "extant" I'm happy to make the change).

:-) , different thought... How about ->is('available') and ->isNot('available') ;-) .

present is also good. I was basically looking at define extant to understand the meaning. Probably people search for keywords in docs :) .

@harikt Fair point. I forget that my vocabulary tends to the obscure. ;-)

@mbrevda you might be interested in this too -- what would be a good name here?

Fwiw, I think extant is basically fine, although I think it has more of a "remains" or "survives" connotation.

Maybe is('set')? Or is the slight distinction from the built-in too much?

@jakejohns Good thought -- I had not considered merely 'set'. On consideration, I think it's too close to isset(), which returns false on a null, even if the variable itself exists, whereas 'extant' is more like array_key_exists(), which returns true even when the array key is null.

Your point about the remains/survives connotation is well-taken, and makes the word less appropriate in this context.

I guess we're down to "present" or "available." Further thoughts, folks?

Skimming through, I missed what happened to isNotBlank() - seems that would be the most straightforward. Otherwise is('set') might be more in line with typical php verbiage.

... is('inSubject') is ('memberOfSubject')?

"present" seems the clearest to me—Rails uses "present" to mean not nil, false, or empty (string or collection), and I find it pretty readable.

Re-reading from @satomif I see I was slightly off with "extant":

What I want to validate is that something is present—that it's not blank. Which validation should I use if I don't have anything else to validate about the field, other than its presence?

So I need to change the logic a little bit in "extant" so that it has to be non-blank, and I think the right name might be "filled" or "entered", as is validate('field')->is('filled'). Thoughts?

All right, I hope this is the last time. @mbrevda had the right idea after all; since we have isBlank(), we should have isNotBlank(). That's been added, and Extant has been removed entirely. Sorry for misreading your earlier statements @satomif. You should be able to do $filter->validate('field')->isNotBlank() now; let me know how it works for you.

Thanks, everyone, for your patience while I worked this out.

@pmjones Thank you. It is intuitive and easier for non English speaker. I'm sure @satomif wii be agreed. :)

Thanks @koriym that's just what I needed to hear. :-)

cool isNotBlank looks nice :) .

Sorry for the delayed response; thank you for adding that method!
I tried it out, but I’m having a problem. I have an attribute called “code”, but both when it’s blank and when there’s something in the attribute, I get the message "code should not have been blank”. Am I misusing the method?

        $filter_factory = new FilterFactory();
        $filter = $filter_factory->newSubjectFilter();
        $filter->validate('code')->isNotBlank();
        $data    = [
            'code'       => 'ああああ',
        ];
        $success = $filter->apply($data);
        if (!$success) {
            $failures = $filter->getFailures();
            print_r($failures->getMessages());
        }
    [code] => Array
        (
            [0] => code should not have been blank
        )

Hey @satomif ,

can you incorporate the above code as a failing test. You can create another method similar to

public function testUseFieldMessage()
{
$this->filter->validate('foo')->isNotBlank()->asSoftRule();
$this->filter->validate('foo')->is('alnum')->asSoftRule();
$this->filter->validate('foo')->is('strlenMin', 6)->asSoftRule();
$subject = (object) array('foo' => '');
$result = $this->filter->apply($subject);
$this->assertFalse($result);
$expect = array(
'foo' => array(
'foo should not have been blank',
'foo should have validated as alnum',
'foo should have validated as strlenMin(6)',
),
);
$actual = $this->filter->getFailures()->getMessages();
$this->assertSame($expect, $actual);

Thank you.

Related #113

Insufficient testing on my part. Working on it now. Thanks for your patience.

All right, let's try this again. Newest push to 2.x branch has modified logic in ValidateSpec and SanitizeSpec, and better testing around isNotBlank(). Let me know if it works for you @satomif .

@pmjones Thanks! The above example works for me now.

@satofmif Excellent! Thanks, again, for your patience and good will. :-)