diff --git a/src/Validation/Rules/Library/RuleString.php b/src/Validation/Rules/Library/RuleString.php index 376b9a8..2505808 100644 --- a/src/Validation/Rules/Library/RuleString.php +++ b/src/Validation/Rules/Library/RuleString.php @@ -98,9 +98,13 @@ public function min(int $len): void public function regexp(string $expr): void { - if (!preg_match(sprintf("#%s#", $expr), $this->value)) { - throw new RuleFailed(sprintf("string does not match regular expression %s", $expr)); - } + if(@preg_match($expr, '') === FALSE) { + throw new RuleFailed(sprintf("regexp is not a valid 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 @@ -113,4 +117,4 @@ public function between(int $min, int $max): void $this->min($min); $this->max($max); } -} \ No newline at end of file +} diff --git a/tests/Parsing/Simple/SimpleRuleParserTest.php b/tests/Parsing/Simple/SimpleRuleParserTest.php index 0059b1a..202e01e 100644 --- a/tests/Parsing/Simple/SimpleRuleParserTest.php +++ b/tests/Parsing/Simple/SimpleRuleParserTest.php @@ -54,8 +54,8 @@ public function goodTestSet(): array [":string ?", [TokenRule::make("string", []), TokenQuantifier::make(0, 1)]], [":string('\'')", [TokenRule::make("string", ["'"])]], [ - ":string :regexp('\w{1,4}') {1}", - [TokenRule::make("string", []), TokenSubRule::make("regexp", ['\w{1,4}']), TokenQuantifier::make(1, 1)], + ":string :regexp('/\w{1,4}/') {1}", + [TokenRule::make("string", []), TokenSubRule::make("regexp", ['/\w{1,4}/']), TokenQuantifier::make(1, 1)], ], [":string :min(501)", [TokenRule::make("string", []), TokenSubRule::make("min", [501])]], [":string? :min(1)", [TokenNullableRule::make("string", []), TokenSubRule::make("min", [1])]], diff --git a/tests/Validation/ArrayValidatorTest.php b/tests/Validation/ArrayValidatorTest.php index b3e0ef8..21b65ed 100644 --- a/tests/Validation/ArrayValidatorTest.php +++ b/tests/Validation/ArrayValidatorTest.php @@ -20,7 +20,7 @@ public function data(): array [[], [':exact("name")?' => ':string'], self::EXPECT_PASS], [['name' => 'John Dutton'], ['name' => ':string'], self::EXPECT_PASS], [['name' => 'Kayce Dutton'], ['name' => ':string :min(20)'], self::EXPECT_FAIL], - [['name' => 'Kayce Dutton'], ['name' => ':string :regexp("Jamie")'], self::EXPECT_FAIL], + [['name' => 'Kayce Dutton'], ['name' => ':string :regexp("/Jamie/")'], self::EXPECT_FAIL], [['name' => 'Jamie', 'lastname' => 'Dutton'], [':string {2}' => ':string'], self::EXPECT_PASS], [['name' => 'Beth Dutton'], ['*' => ':string or :number'], self::EXPECT_PASS], [['12' => ''], [':string' => ''], self::EXPECT_FAIL], @@ -52,14 +52,14 @@ public function data(): array "lastname" => "", ], [ - ":string :regexp('\w{0,4}') {1}" => ":any", - ":string :regexp('\w{0,}') {1}" => ":any", + ":string :regexp('/\w{0,4}/') {1}" => ":any", + ":string :regexp('/\w{0,}/') {1}" => ":any", ], self::EXPECT_PASS, ], [ ["name" => "John"], - [':string :regexp("\w{1,4}") {1}' => ":any"], + [':string :regexp("/\w{1,4}/") {1}' => ":any"], self::EXPECT_PASS, ], [ @@ -156,7 +156,7 @@ public function data(): array "last_name" => "Walberg", ], [ - ":string :regexp('(first|last)_name') *" => ":string", + ":string :regexp('/(first|last)_name/') *" => ":string", ], self::EXPECT_PASS, ], diff --git a/tests/Validation/Rules/Library/RuleStringTest.php b/tests/Validation/Rules/Library/RuleStringTest.php index 7c1b8c9..5dfb08b 100644 --- a/tests/Validation/Rules/Library/RuleStringTest.php +++ b/tests/Validation/Rules/Library/RuleStringTest.php @@ -32,8 +32,12 @@ public function data(): array ['max', [3], 'abcd', self::SHOULD_FAIL], ['between', [1, 3], 'abc', self::SHOULD_SUCCEED], ['between', [1, 3], 'abcd', self::SHOULD_FAIL], - ['regexp', ['^[a]+$'], 'aa', self::SHOULD_SUCCEED], - ['regexp', ['^[a]+$'], 'ab', self::SHOULD_FAIL], + ['regexp', ['/^[a]+$/'], 'aa', self::SHOULD_SUCCEED], + ['regexp', ['/^[a]+$/'], 'ab', self::SHOULD_FAIL], + ['regexp', ['^[a]+$'], 'aa', self::SHOULD_FAIL], + ['regexp', ['#^[a]+$#'], 'aa', self::SHOULD_SUCCEED], + ['regexp', ['@^[a]+$@'], 'aa', self::SHOULD_SUCCEED], + ['regexp', ['/^(https?:\/\/)/i'], 'http://github.com', self::SHOULD_SUCCEED], ['contains', ['bb'], 'abba', self::SHOULD_SUCCEED], ['contains', ['bb'], 'ab', self::SHOULD_FAIL], ['starts', ['na'], 'name', self::SHOULD_SUCCEED],