Merge pull request #261 from rauwebieten/master

ipv4, ipv6 and ascii rules
This commit is contained in:
Vance Lucas 2018-09-11 11:59:23 -05:00 committed by GitHub
commit 14702f00d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 0 deletions

View File

@ -133,12 +133,15 @@ V::lang('ar');
* `in` - Performs in_array check on given array values * `in` - Performs in_array check on given array values
* `notIn` - Negation of `in` rule (not in array of values) * `notIn` - Negation of `in` rule (not in array of values)
* `ip` - Valid IP address * `ip` - Valid IP address
* `ipv4` - Valid IP v4 address
* `ipv6` - Valid IP v6 address
* `email` - Valid email address * `email` - Valid email address
* `emailDNS` - Valid email address with active DNS record * `emailDNS` - Valid email address with active DNS record
* `url` - Valid URL * `url` - Valid URL
* `urlActive` - Valid URL with active DNS record * `urlActive` - Valid URL with active DNS record
* `alpha` - Alphabetic characters only * `alpha` - Alphabetic characters only
* `alphaNum` - Alphabetic and numeric characters only * `alphaNum` - Alphabetic and numeric characters only
* `ascii` - ASCII characters only
* `slug` - URL slug characters (a-z, 0-9, -, \_) * `slug` - URL slug characters (a-z, 0-9, -, \_)
* `regex` - Field matches given regex pattern * `regex` - Field matches given regex pattern
* `date` - Field is a valid date * `date` - Field is a valid date

View File

@ -493,6 +493,30 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_IP) !== false; 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 * Validate that a field is a valid e-mail address
* *
@ -505,6 +529,24 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; 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 * Validate that a field is a valid e-mail address and the domain name is active
* *

View File

@ -621,6 +621,20 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $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() public function testIpValid()
{ {
$v = new Validator(array('ip' => '127.0.0.1')); $v = new Validator(array('ip' => '127.0.0.1'));
@ -635,6 +649,34 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $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() public function testEmailValid()
{ {
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));