Git Product home page Git Product logo

Comments (5)

lezhnev74 avatar lezhnev74 commented on June 23, 2024 1

Hey @Enchiridion , yeah good point.
There is a way to say that a value can be nullable with a pattern like this :array?, however it does not let you specify the array pattern. Another thought is a new key modifier (along with ?, * etc) that means key value can be nullable. Hm... Definitely a missing feature but there are more than one way to add it, need to think about it.

from pasvl.

Enchiridion avatar Enchiridion commented on June 23, 2024

I'm working with an new API and just came across another issue. Some pieces of data may be multiple types, for example it can be either not present, null, false, or an array with a specific shape (the API isn't the greatest). Maybe these complex cases could be handled by a specifying a function? Then I can implement the custom logic needed and also utilize the library where possible. I think only a new rule would be needed and none of the modifiers need to be touched.

from pasvl.

lezhnev74 avatar lezhnev74 commented on June 23, 2024

I think you can have a new rule with your custom logic right now. Try this:

  • create a new class (take this one as example RuleArray). Maybe even extend that one.
  • that new class is not known to the package by default, so you need to extend RuleLocator as well
  • when you create a new validator instance, provide that rule locator.

See this example for more: Example/customRule.php

from pasvl.

Enchiridion avatar Enchiridion commented on June 23, 2024

Thanks! I was able to use that to create a custom rule that's working great. Now that I've dug into the code more, I realized that the detailed error messages aren't being passed on, which would of been really helpful earlier when I was building the rules.

For example:

try {
  $pattern = array( 'name' => ':number' );
  $content = array( 'name' => 'foo' );
  $builder = \PASVL\Validation\ValidatorBuilder::forArray( $pattern );
  $builder->build()->validate( $content );
} catch ( \Throwable $e ) {
  echo $e->getMessage();
}

The Exception's message should be the value is not a number but it's actually Data value [foo] matched no pattern at root level. Is this a bug?

from pasvl.

lezhnev74 avatar lezhnev74 commented on June 23, 2024

Good question, I remember that error messages were not that simple. To put it shortly it only can tell you that patterns did not match, but it can't tell you what exact pattern did not match.

That is because keys are specified as patterns in general, exact key is just a special case. So the system does not know what pattern was meant for the value.

So here is an example:

$data = [
    "ab" => "",
    "abc" => "",
];
$pattern = [
    ":string :between(1,3)" => "",
    ":string :between(2,4)" => "",
];

Both data items match both key patterns. The system just applies different combination of patterns to the data and stops whenever there is a match. Otherwise it halts with a message like the one you posted.

p.s. I get your confusion and I had the same question in the past, but I am yet to come to a general solution that allows to specify keys as patterns as well as gives better messages.

p.p.s. Hence the regular expressions function preg_match it returns bool result, it does not tell you what part of the expression failed, hm?

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.