lezhnev74 / pasvl Goto Github PK
View Code? Open in Web Editor NEWArray Validator (regular expressions for arrays, sort of)
License: MIT License
Array Validator (regular expressions for arrays, sort of)
License: MIT License
That would be convenient to set expectations as multiple options like:
(:string :len(0)) OR (:int :min(0) :max(100))
What's the correct/best way to only match a subset of the array's keys and ignore the rest?
For example in the arrray [ 'foo' => 'zoo', 'bar' => 42, 'baz' => true ]
, I only care about validating the values of foo
and bar
and don't care about baz
or any other entries that may appear later.
Love the library; just started using hit.
However, encountered this issue - have a validator pattern like:
$ref = ":object(nullable) :instance(\My\Class\Name)"
Which will always fail if the object actually is null (it still checks for the instance type, even though object is set to Nullable.
I subclassed ObjectValidator, but looking at the validators, this may be an issue throughout the library?
First, great library!
Is it possible to specify that a value can be NULL or an array with a particular shape?
In this example, roadInfo
can be either NULL or the shape shown below.
'roadInfo' => array(
'id' => ':string',
'type' => ':string',
'title' => ':string',
'data?' => array(
'*' => array(
'language' => ':string',
'data' => ':string',
),
),
),
In case data was marked as invalid by a specific Validator, it should be reported. So client can get a validator name which failed.
Example:
$data = ["name"=>"Alan"];
$pattern = ["name" => ":string :len(10)"];
The report should contain a message that "Length 4 did not match expected 10" or similar.
This also should support multi-language environment.
Hi,
I'm facing an issue I don't really understand. I have this pattern :
array(1) {
'loop' =>
array(2) {
'count' =>
string(26) ":string :regexp("/(\d)*/")" also tried ":number :int"
'sub' =>
string(7) ":string"
}
}
For this array :
array(1) {
'loop' =>
array(2) {
'count' =>
string(1) "5"
'sub' =>
string(13) "create-cookie"
}
}
PASVL always returns an error and I don't really know why. It's an exception that says : Data value [5] matched no pattern at level [loop]
. This is the whole stack trace (only PASVL concern) :
[Error details] Data value [5] matched no pattern at level [loop]
PASVL\Validation\Problems\DataValueMatchedNoPattern: Data value [5] matched no pattern at level [loop] in /AutoMate/vendor/lezhnev74/pasvl/src/Validation/Problems/ArrayFailedValidation.php:14
Stack trace:
#0 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/Problems/DataValueMatchedNoPattern.php(22): PASVL\Validation\Problems\ArrayFailedValidation::make(Array, 'Data value [5] ...')
#1 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(97): PASVL\Validation\Problems\DataValueMatchedNoPattern::fromData('5', Array)
#2 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(55): PASVL\Validation\ArrayValidator->validateArrayLevel(Array, Array)
#3 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(89): PASVL\Validation\ArrayValidator->matchValue(Array, Array)
#4 [internal function]: PASVL\Validation\ArrayValidator->PASVL\Validation\{closure}('loop')
#5 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(95): array_filter(Array, Object(Closure))
#6 AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(55): PASVL\Validation\ArrayValidator->validateArrayLevel(Array, Array)
#7 /AutoMate/vendor/lezhnev74/pasvl/src/Validation/ArrayValidator.php(41): PASVL\Validation\ArrayValidator->matchValue(Array, Array)
#8 /AutoMate/src/Scenario/Transformer/AbstractTransformer.php(80): PASVL\Validation\ArrayValidator->validate(Array)
It seems that the pattern matches the array right... But it doesn't.
Make sure code supports unicode characters in patterns.
Hi,
I'm trying to use your library that's really cool and interesting for the project I'm doing. First time using it, I'm facing this issue.
I have this $pattern (comes from a var_dump($pattern)) :
array(1) {
["go"]=>
string(36) ":string :regexp("%^(https?:\/\/)%i")"
}
And this array (as $this->step) :
array(1) {
["go"]=>
string(25) "https://www.wikipedia.org"
}
when I use this code :
private function validate() {
$pattern = $this->getPattern();
$builder = ValidatorBuilder::forArray($pattern);
$validator = $builder->build();
$value = true;
try {
$validator->validate($this->step);
} catch (ArrayFailedValidation $e) {
echo "Step [". key($this->step) ."] is not a valid step. Watch out the syntax pattern.\n";
echo "[Error details] " . $e->getMessage() . "\n";
$value = false;
}
return $value;
}
But it returns this lines :
Step [go] is not a valid step. Watch out the syntax pattern.
[Error details] Data value [https://www.wikipedia.org] matched no pattern at root level
I tried using preg_match("%^(https?://)%i", 'https://wikipedia.fr'); and it returns 1 (so, true).
I don't know what's wrong with that. Can you help me ?
Regards,
"A pattern can have a single main validator name and any number of sub-validators."
But, in below example
$pattern = [
'test' => ':string :min(1) :max(255)'
];
$test = [
'test' => 'test value'
];
two validators are created, string and min, the last having argument of
1) :max(255
that means erroneous behavior.
Looking into
\PASVL\Pattern\Pattern::parse
method, and regexp used to match validator, mistake can be seen - no matching for closing parenthesis.
A little fix can be changing
$validator_arguments_pattern = "(\((?'arguments'.+,?)+\))?";
to
$validator_arguments_pattern = "(\((?'arguments'[^)]+,?)+\))?";
Still, however, closing parenthesis used as part of argument (ex. for regexp validator) will cause some problems...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.