From 971da213a390d06e61322245ca3f8b01220bd137 Mon Sep 17 00:00:00 2001 From: peterkinamo Date: Fri, 3 Aug 2018 12:28:23 +0200 Subject: [PATCH 1/3] Ascii rule added Ascii unit test added --- src/Valitron/Validator.php | 18 ++++++++++++++++++ tests/Valitron/ValidateTest.php | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 313d7e8..eff6d43 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -505,6 +505,24 @@ class Validator return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; } + /** + * Validate that a field contains only ASCII characters + * + * @param $field + * @param $value + * @return bool|false|string + */ + protected function validateAscii($field, $value) + { + // multibyte extension needed + if (function_exists('mb_detect_encoding')) { + return mb_detect_encoding($value, 'ASCII', true); + } + + // fallback with regex + return 0 === preg_match('/[^\x00-\x7F]/', $value); + } + /** * Validate that a field is a valid e-mail address and the domain name is active * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 4cf5b59..d77634b 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -617,6 +617,20 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testAsciiValid() + { + $v = new Validator(array('text' => '12345 abcde')); + $v->rule('ascii', 'text'); + $this->assertTrue($v->validate()); + } + + public function testAsciiInvalid() + { + $v = new Validator(array('text' => '12345 abcdé')); + $v->rule('ascii', 'text'); + $this->assertFalse($v->validate()); + } + public function testIpValid() { $v = new Validator(array('ip' => '127.0.0.1')); From e5ae2c9a5f523752a65541aec6adc6ddbf716556 Mon Sep 17 00:00:00 2001 From: peterkinamo Date: Fri, 3 Aug 2018 13:02:21 +0200 Subject: [PATCH 2/3] ipv4 and ipv6 rule and tests added --- src/Valitron/Validator.php | 24 ++++++++++++++++++++++++ tests/Valitron/ValidateTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index eff6d43..574f185 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -493,6 +493,30 @@ class Validator return filter_var($value, \FILTER_VALIDATE_IP) !== false; } + /** + * Validate that a field is a valid IP v4 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv4($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false; + } + + /** + * Validate that a field is a valid IP v6 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv6($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false; + } + /** * Validate that a field is a valid e-mail address * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d77634b..52aba0d 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -645,6 +645,34 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testIpv4Valid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv4', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv4Invalid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv4', 'ip'); + $this->assertFalse($v->validate()); + } + + public function testIpv6Valid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv6', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv6Invalid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv6', 'ip'); + $this->assertFalse($v->validate()); + } + public function testEmailValid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); From 46ccdf1acecf0dfd362fe6086c3626e4db561627 Mon Sep 17 00:00:00 2001 From: peterkinamo Date: Fri, 3 Aug 2018 13:38:39 +0200 Subject: [PATCH 3/3] ascii, ipv4 and ipv6 rules added to README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4da73cb..ca9d986 100644 --- a/README.md +++ b/README.md @@ -133,12 +133,15 @@ V::lang('ar'); * `in` - Performs in_array check on given array values * `notIn` - Negation of `in` rule (not in array of values) * `ip` - Valid IP address + * `ipv4` - Valid IP v4 address + * `ipv6` - Valid IP v6 address * `email` - Valid email address * `emailDNS` - Valid email address with active DNS record * `url` - Valid URL * `urlActive` - Valid URL with active DNS record * `alpha` - Alphabetic characters only * `alphaNum` - Alphabetic and numeric characters only + * `ascii` - ASCII characters only * `slug` - URL slug characters (a-z, 0-9, -, \_) * `regex` - Field matches given regex pattern * `date` - Field is a valid date