Respect / Validation

The most awesome validation engine ever created for PHP
https://respect-validation.readthedocs.io
MIT License
5.76k stars 774 forks source link

~~Why does v::containsAny(array('mod'))->validate('mode') return true?~~ Because the expected functionality is confused with In ( https://respect-validation.readthedocs.io/en/latest/rules/In/ ) #1381

Closed jasonkhanlar closed 2 years ago

jasonkhanlar commented 2 years ago

Either I am dumb or something is broken.

echo '<pre>1: ' . var_export(v::containsAny(array('mode'))->validate('mode'), true) . '</pre>';

returns true (good!)

echo '<pre>2: ' . var_export(v::containsAny(array('mod'))->validate('mode'), true) . '</pre>';

returns true (bad!)


https://respect-validation.readthedocs.io/en/latest/rules/ContainsAny/

"A second parameter may be passed for identical comparison instead of equal comparison for arrays."

echo '<pre>3: ' . var_export(v::containsAny(array('mode'), false)->validate('mode'), true) . '</pre>';

returns true (good!)

echo '<pre>4: ' . var_export(v::containsAny(array('mod'), false)->validate('mode'), true) . '</pre>';

returns true (I guess this make sense, but it shouldn't)

echo '<pre>5: ' . var_export(v::containsAny(array('mode'), true)->validate('mode'), true) . '</pre>';

returns true (good!)

echo '<pre>6: ' . var_export(v::containsAny(array('mod'), true)->validate('mode'), true) . '</pre>';

returns true (bad!)


Expected result similar to:

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

I think my understanding of syntax is backwards maybe?

ContainsAny(array $needles)
ContainsAny(array $needles, bool $identical)

Shouldn't that be: v::containsAny(haystack)->validate(needle);?


Well well well, I just found https://respect-validation.readthedocs.io/en/latest/rules/In/ And what do you know!?!?!?

echo '<pre>7: ' . var_export(v::in(array('mode'), false)->validate('mode'), true) . '</pre>';

returns true (good!)

echo '<pre>8: ' . var_export(v::in(array('mod'), false)->validate('mode'), true) . '</pre>';

returns false (good!)

echo '<pre>9: ' . var_export(v::in(array('mode'), true)->validate('mode'), true) . '</pre>';

returns true (good!)

echo '<pre>10: ' . var_export(v::in(array('mod'), true)->validate('mode'), true) . '</pre>';

returns false (good!)

jasonkhanlar commented 2 years ago

I figured out working solution. However, a suggestion to use same naming mechanisms as other languages (e.g. JavaScript/Node, PHP, Python, Perl, etc.) perhaps even creating aliases for additional names mapped to duplicate across other unused names for convenience of familiarity; e.g. 'includes' to match JavaScript