From 63dee4e79b367733269c0ff791c4910a90a7b581 Mon Sep 17 00:00:00 2001 From: Dmitriy Lezhnev Date: Wed, 10 Feb 2021 11:58:04 +0500 Subject: [PATCH] - updated phpunit xml - updated readme --- README.md | 9 +++++---- phpunit.xml | 17 ++++++++++------- src/Validation/Rules/Library/RuleString.php | 18 +++++++++++------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ec6833e..0aad50b 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ try { ### Optional String Validation ```php -$pattern = ":string :regexp('^[ab]+$')"; +$pattern = ":string :regexp('#^[ab]+$#')"; $builder = \PASVL\Validation\ValidatorBuilder::forString($pattern); $validator = $builder->build(); $validator->validate("abab"); // the string is valid @@ -121,7 +121,7 @@ It looks like this: ``` - as rule with subrules ```php - $pattern = ["name" => ":string :regexp('\d*')"]; // the value must be a string which contains only digits + $pattern = ["name" => ":string :regexp('#\d*#')"]; // the value must be a string which contains only digits ``` - as rule with quantifiers ```php @@ -153,7 +153,7 @@ To add new custom rules, follow these steps: ## Built-in Rules This package comes with a few built-in rules and their corresponding sub-rules (see in folder `src/Validation/Rules/Library`): - `:string` - the value must be string - - `:regexp()` - provide a regular expression without trailing delimiters + - `:regexp()` - provide a regular expression(the same as for `preg_match()`) - `:url` - `:email` - `:uuid` @@ -200,7 +200,8 @@ This package comes with a few built-in rules and their corresponding sub-rules ( ## 🏆 Contributors - **[Greg Corrigan](https://github.com/corrigang)**. Greg spotted a problem with nullable values reported as invalid. - **Henry Combrinck**. Henry tested the library extensively on real data and found tricky bugs and edge cases. Awesome contribution to make the package valuable to the community. -- **[@Averor](https://github.com/Averor)**. Found a bug in parenthesis parsing. +- **[@Averor](https://github.com/Averor)**. Found a bug in parentheses parsing. +- **[Julien Gidel](https://github.com/JuGid)**. Improved `regexp` sub-rule. ## License This project is licensed under the terms of the MIT license. diff --git a/phpunit.xml b/phpunit.xml index 9a4c224..61e9f95 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,6 @@ - + stopOnFailure="true" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" +> + + + ./src + + ./tests - - - ./src - - diff --git a/src/Validation/Rules/Library/RuleString.php b/src/Validation/Rules/Library/RuleString.php index 2505808..125aa09 100644 --- a/src/Validation/Rules/Library/RuleString.php +++ b/src/Validation/Rules/Library/RuleString.php @@ -98,18 +98,22 @@ public function min(int $len): void public function regexp(string $expr): void { - if(@preg_match($expr, '') === FALSE) { - throw new RuleFailed(sprintf("regexp is not a valid regular expression %s", $expr)); - } + error_clear_last(); + if (@preg_match($expr, '') === false) { + $lastError = error_get_last() ? error_get_last()['message'] : ''; + throw new RuleFailed(sprintf("regexp is not a valid regular expression %s: %s", $expr, $lastError)); + } - if(!preg_match($expr, $this->value)) { - throw new RuleFailed(sprintf("string does not match regular expression %s", $expr)); - } + if (!preg_match($expr, $this->value)) { + throw new RuleFailed(sprintf("string does not match regular expression %s", $expr)); + } } public function url(): void { - if (filter_var($this->value, FILTER_VALIDATE_URL) === false) throw new RuleFailed("string must be url"); + if (filter_var($this->value, FILTER_VALIDATE_URL) === false) { + throw new RuleFailed("string must be url"); + } } public function between(int $min, int $max): void