Skip to content

Commit

Permalink
Merge pull request #8 from JuGid/master
Browse files Browse the repository at this point in the history
Suppress # in regexp validator function
  • Loading branch information
lezhnev74 authored Feb 10, 2021
2 parents 91d2c11 + 56f87af commit 31fda37
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/Validation/Rules/Library/RuleString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -113,4 +117,4 @@ public function between(int $min, int $max): void
$this->min($min);
$this->max($max);
}
}
}
4 changes: 2 additions & 2 deletions tests/Parsing/Simple/SimpleRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])]],
Expand Down
10 changes: 5 additions & 5 deletions tests/Validation/ArrayValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
],
[
Expand Down Expand Up @@ -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,
],
Expand Down
8 changes: 6 additions & 2 deletions tests/Validation/Rules/Library/RuleStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 31fda37

Please sign in to comment.