Git Product home page Git Product logo

Comments (13)

lezhnev74 avatar lezhnev74 commented on September 24, 2024 1

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:

  1. the pattern :number :int won't match "5", because it only matches integers (hence the sub-rule :int).
  2. however, simple :number would match "5" or 5 or 5.0
  3. the pattern :string :regexp("/(\d)*/") matches "5" only (hence :string rule)

As a reference, this test passes (see the image).
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.

lezhnev74 avatar lezhnev74 commented on September 24, 2024 1

There is another way for you, use a compound rule: :string :regexp("/(\d)*/") or :number :int which matches 5 and "5".

from pasvl.

lezhnev74 avatar lezhnev74 commented on September 24, 2024 1

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.

lezhnev74 avatar lezhnev74 commented on September 24, 2024 1

are you positive you are using the latest 1.0.0 release?

from pasvl.

JuGid avatar JuGid commented on September 24, 2024 1

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.

JuGid avatar JuGid commented on September 24, 2024

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.

JuGid avatar JuGid commented on September 24, 2024

Let's review your example:

  1. the pattern :number :int won't match "5", because it only matches integers (hence the sub-rule :int).
  2. however, simple :number would match "5" or 5 or 5.0
  3. 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.

JuGid avatar JuGid commented on September 24, 2024

Yes, that's a good solution, but :string :regexp("/(\d)*/") does not match "5" weirdly... As I did in my first test.

from pasvl.

lezhnev74 avatar lezhnev74 commented on September 24, 2024

ok, let's review your failing test. Can you push it?

from pasvl.

JuGid avatar JuGid commented on September 24, 2024

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.

lezhnev74 avatar lezhnev74 commented on September 24, 2024

Ok, I need data as well to understand what has happened, or you can actually push a failing test to the ArrayValidatorTest.

from pasvl.

lezhnev74 avatar lezhnev74 commented on September 24, 2024

Ah I see there is a small $array in the code sample, let me review it.

from pasvl.

JuGid avatar JuGid commented on September 24, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.