Comments (13)
Hey @JuGid ! Yeah, with PHP it is not always clear how to deal with type coercion. String can be coerced to int or float and back again. Can we say that "5"
is equivalent to 5
? In PHP... probably, in general those are data of different types.
Let's review your example:
- the pattern
:number :int
won't match"5"
, because it only matches integers (hence the sub-rule:int
). - however, simple
:number
would match"5"
or5
or5.0
- the pattern
:string :regexp("/(\d)*/")
matches"5"
only (hence:string
rule)
As a reference, this test passes (see the image).
Is it relevant to propose a PR that also convert '5' to 5 with intval() when ":number :int" is used with a string like '5' or '5678' ?
Since this library is purely for PHP (with its dynamic nature), I think the PR makes sense.
from pasvl.
There is another way for you, use a compound rule: :string :regexp("/(\d)*/") or :number :int
which matches 5
and "5"
.
from pasvl.
Very interesting, my output is:
Test pass
Data value [5] matched no pattern at level [loop]
Test pass
Test pass
Test pass
The second failure is expected as it applied rule :number :int
on '5'
.
I run PHP 7.4.15
, what is your version?
from pasvl.
are you positive you are using the latest 1.0.0
release?
from pasvl.
Oh ok... I updated PASVL from 0.6.4 to 1.0.0 and the tests pass now, I did not see the new release... My bad.
Going to change my patterns now to new valuable patterns that matches the reality...
from pasvl.
I have investigate a bit more, and it's caused by the subrule ":int" that does not detect 5 as an int or '5' also (btw it's a string). Most of parser parse [count: 5] as ["count" => "5"] so you don't have to use ":number :int" as it uses is_int() from php to validate but ":number" or ":string" only.
Is it relevant to propose a PR that also convert '5' to 5 with intval() when ":number :int" is used with a string like '5' or '5678' ?
from pasvl.
Let's review your example:
- the pattern
:number :int
won't match"5"
, because it only matches integers (hence the sub-rule:int
).- however, simple
:number
would match"5"
or5
or5.0
- the pattern
:string :regexp("/(\d)*/")
matches"5"
only (hence:string
rule)
This is what I found when I made my tests. PHP can presents some weird cases since 'a' == 0 returns true, et 'f' == 0 returns true too.
What I can propose is juste make a verification with is_numeric. If false, throw an Exception, else, continue with intval.
from pasvl.
Yes, that's a good solution, but :string :regexp("/(\d)*/")
does not match "5" weirdly... As I did in my first test.
from pasvl.
ok, let's review your failing test. Can you push it?
from pasvl.
I can post a simple test that glob each one I did :
<?php
require __DIR__.'/vendor/autoload.php';
use PASVL\Validation\ValidatorBuilder;
$patterns = [
[
'loop'=> [
'count' => ':string :regexp("/(\d)*/")'
]
],
[
'loop'=> [
'count'=>':number :int'
]
],
[
'loop'=> [
'count'=>':string :regexp("/(\d)*/") or :number :int'
]
],
[
'loop'=> [
'count'=>':string'
]
],
[
'loop'=> [
'count'=>':number'
]
]
];
$array = ['loop'=> [
'count'=> '5'
]
];
foreach($patterns as $pattern) {
try{
$builder = ValidatorBuilder::forArray($pattern);
$validator = $builder->build();
$validator->validate($array);
echo 'Test pass' . "\n";
}catch(\Exception $e) {
echo $e->getMessage() . "\n";
}
}
Console output :
Data value [5] matched no pattern at level [loop]
Data value [5] matched no pattern at level [loop]
Data value [5] matched no pattern at level [loop]
Test pass
Test pass
from pasvl.
Ok, I need data as well to understand what has happened, or you can actually push a failing test to the ArrayValidatorTest
.
from pasvl.
Ah I see there is a small $array
in the code sample, let me review it.
from pasvl.
On the MacOS, I have a PHP 7.3.10
(Wow, I have to update that... I'm so late)
from pasvl.
Related Issues (9)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pasvl.