mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge remote master branch
This commit is contained in:
commit
f70e27af56
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
||||
composer.phar
|
||||
composer.lock
|
||||
vendor
|
||||
.idea/
|
||||
|
||||
12
.travis.yml
12
.travis.yml
@ -1,21 +1,27 @@
|
||||
# see http://about.travis-ci.org/docs/user/languages/php/ for more hints
|
||||
language: php
|
||||
dist: precise
|
||||
|
||||
# list any PHP version you want to test against
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- nightly
|
||||
- hhvm
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- php: hhvm
|
||||
- php: nightly
|
||||
include:
|
||||
- php: 5.3
|
||||
dist: precise
|
||||
|
||||
before_script:
|
||||
- composer install
|
||||
|
||||
# Script to run tests
|
||||
script: ./vendor/bin/phpunit
|
||||
script: composer test
|
||||
|
||||
@ -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
|
||||
|
||||
@ -16,11 +16,19 @@
|
||||
"php": ">=5.3.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.8.35"
|
||||
"phpunit/phpunit": "^4.8.35 || ^5.5 || ^6.5"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-mbstring": "It can support the multiple bytes string length."
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Valitron": "src/"
|
||||
"psr-4": {
|
||||
"Valitron\\": "src/Valitron"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Valitron\\": "tests/Valitron"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
34
lang/ko.php
Normal file
34
lang/ko.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'required' => "을(를) 입력해야 합니다.",
|
||||
'equals' => "은(는) '%s'와(과) 같아야 합니다.",
|
||||
'different' => "은(는) '%s'와(과) 달라야 합니다.",
|
||||
'accepted' => "을(를) 동의해야 합니다.",
|
||||
'numeric' => "은(는) 숫자여야 합니다.",
|
||||
'integer' => "은(는) 정수여야 합니다.",
|
||||
'length' => "의 길이는 %d 이어야 합니다.",
|
||||
'min' => "은(는) %s 이상이어야 합니다.",
|
||||
'max' => "은(는) %s 이하여야 합니다.",
|
||||
'in' => "은(는) 올바르지 않은 값을 포함하고 있습니다.",
|
||||
'notIn' => "은(는) 올바르지 않은 값을 포함하고 있습니다.",
|
||||
'ip' => "은(는) 올바르지 않은 IP입니다.",
|
||||
'email' => "은(는) 올바르지 않은 email입니다.",
|
||||
'url' => "은(는) 올바르지 않은 URL입니다.",
|
||||
'urlActive' => "은(는) 접속 가능한 도메인이어야 합니다.",
|
||||
'alpha' => "은(는) 영문자(a-z)로만 이루어져야 합니다.",
|
||||
'alphaNum' => "은(는) 영문자(a-z)와 숫자(0-9)로만 이루어져야 합니다.",
|
||||
'slug' => "은(는) 영문자(a-z)와 숫자(0-9), 특수문자 -와 _로만 이루어져야 합니다.",
|
||||
'regex' => "은(는) 올바르지 않은 문자를 포함하고 있습니다.",
|
||||
'date' => "은(는) 올바르지 않은 날짜입니다.",
|
||||
'dateFormat' => "은(는) '%s' 형태의 날짜여야 합니다.",
|
||||
'dateBefore' => "은(는) '%s' 보다 이전이어야 합니다.",
|
||||
'dateAfter' => "은(는) '%s' 보다 이후여야 합니다.",
|
||||
'contains' => "은(는) '%s'을(를) 포함해야 합니다.",
|
||||
'boolean' => "은(는) boolean이어야 합니다.",
|
||||
'lengthBetween' => "의 길이는 %d에서 %d 사이여야 합니다.",
|
||||
'creditCard' => "은(는) 올바른 신용카드 번호가 아닙니다.",
|
||||
'lengthMin' => "의 길이는 %d 이상이어야 합니다.",
|
||||
'lengthMax' => "의 길이는 %d을(를) 넘을 수 없습니다.",
|
||||
'instanceOf' => "는 '%s'의 객체여야 합니다."
|
||||
);
|
||||
@ -15,4 +15,12 @@
|
||||
<directory suffix="Test.php">tests/Valitron</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</whitelist>
|
||||
<blacklist>
|
||||
<directory suffix=".php">vendor/</directory>
|
||||
</blacklist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Valitron;
|
||||
|
||||
/**
|
||||
@ -77,13 +78,18 @@ class Validator
|
||||
*/
|
||||
protected $validUrlPrefixes = array('http://', 'https://', 'ftp://');
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $stop_on_first_fail = false;
|
||||
|
||||
/**
|
||||
* Setup validation
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @param string $lang
|
||||
* @param string $langDir
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @param string $lang
|
||||
* @param string $langDir
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null)
|
||||
@ -142,8 +148,8 @@ class Validator
|
||||
* Required field validator
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateRequired($field, $value, $params = array())
|
||||
@ -198,7 +204,7 @@ class Validator
|
||||
* This validation rule implies the field is "required"
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAccepted($field, $value)
|
||||
@ -212,7 +218,7 @@ class Validator
|
||||
* Validate that a field is an array
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateArray($field, $value)
|
||||
@ -224,7 +230,7 @@ class Validator
|
||||
* Validate that a field is numeric
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateNumeric($field, $value)
|
||||
@ -236,15 +242,15 @@ class Validator
|
||||
* Validate that a field is an integer
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateInteger($field, $value, $params)
|
||||
{
|
||||
if (isset($params[0]) && (bool)$params[0]) {
|
||||
//strict mode
|
||||
return preg_match('/^-?([0-9])+$/i', $value);
|
||||
return preg_match('/^([0-9]|-[1-9]|-?[1-9][0-9]*)$/i', $value);
|
||||
}
|
||||
|
||||
return filter_var($value, \FILTER_VALIDATE_INT) !== false;
|
||||
@ -288,8 +294,8 @@ class Validator
|
||||
* Validate the length of a string (min)
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@ -304,8 +310,8 @@ class Validator
|
||||
* Validate the length of a string (max)
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@ -375,9 +381,8 @@ class Validator
|
||||
* Validate the size of a field is between min and max values
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateBetween($field, $value, $params)
|
||||
@ -512,7 +517,7 @@ class Validator
|
||||
* Validate that a field is a valid IP address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIp($field, $value)
|
||||
@ -521,22 +526,64 @@ class Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address
|
||||
* 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
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEmail($field, $value)
|
||||
{
|
||||
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
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEmailDNS($field, $value)
|
||||
@ -556,7 +603,7 @@ class Validator
|
||||
* Validate that a field is a valid URL by syntax
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUrl($field, $value)
|
||||
@ -574,7 +621,7 @@ class Validator
|
||||
* Validate that a field is an active URL by verifying DNS record
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUrlActive($field, $value)
|
||||
@ -594,7 +641,7 @@ class Validator
|
||||
* Validate that a field contains only alphabetic characters
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAlpha($field, $value)
|
||||
@ -606,7 +653,7 @@ class Validator
|
||||
* Validate that a field contains only alpha-numeric characters
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAlphaNum($field, $value)
|
||||
@ -618,7 +665,7 @@ class Validator
|
||||
* Validate that a field contains only alpha-numeric characters, dashes, and underscores
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateSlug($field, $value)
|
||||
@ -633,8 +680,8 @@ class Validator
|
||||
* Validate that a field passes a regular expression check
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateRegex($field, $value, $params)
|
||||
@ -646,7 +693,7 @@ class Validator
|
||||
* Validate that a field is a valid date
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDate($field, $value)
|
||||
@ -712,7 +759,7 @@ class Validator
|
||||
* Validate that a field contains a boolean.
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateBoolean($field, $value)
|
||||
@ -725,8 +772,8 @@ class Validator
|
||||
* optionally filtered by an array
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateCreditCard($field, $value, $params)
|
||||
@ -742,7 +789,7 @@ class Validator
|
||||
if (is_array($params[0])) {
|
||||
$cards = $params[0];
|
||||
} elseif (is_string($params[0])) {
|
||||
$cardType = $params[0];
|
||||
$cardType = $params[0];
|
||||
if (isset($params[1]) && is_array($params[1])) {
|
||||
$cards = $params[1];
|
||||
if (!in_array($cardType, $cards)) {
|
||||
@ -765,7 +812,7 @@ class Validator
|
||||
return false;
|
||||
}
|
||||
for ($i = 0; $i < $strlen; $i++) {
|
||||
$digit = (int) substr($number, $strlen - $i - 1, 1);
|
||||
$digit = (int)substr($number, $strlen - $i - 1, 1);
|
||||
if ($i % 2 == 1) {
|
||||
$sub_total = $digit * 2;
|
||||
if ($sub_total > 9) {
|
||||
@ -777,7 +824,7 @@ class Validator
|
||||
$sum += $sub_total;
|
||||
}
|
||||
if ($sum > 0 && $sum % 10 == 0) {
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -788,11 +835,11 @@ class Validator
|
||||
return true;
|
||||
} else {
|
||||
$cardRegex = array(
|
||||
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
||||
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
||||
'amex' => '#^3[47][0-9]{13}$#',
|
||||
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
||||
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
||||
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
||||
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
||||
'amex' => '#^3[47][0-9]{13}$#',
|
||||
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
||||
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
||||
);
|
||||
|
||||
if (isset($cardType)) {
|
||||
@ -973,16 +1020,14 @@ class Validator
|
||||
return array(null, array_key_exists($identifier, $data));
|
||||
}
|
||||
return array(null, false);
|
||||
}
|
||||
// Match array element
|
||||
} // Match array element
|
||||
elseif (count($identifiers) === 0) {
|
||||
if ($allow_empty){
|
||||
if ($allow_empty) {
|
||||
//when empty values are allowed, we only care if the key exists
|
||||
return array(null, array_key_exists($identifier, $data));
|
||||
}
|
||||
return array($data[$identifier], false);
|
||||
}
|
||||
// We need to go deeper
|
||||
return array($data[$identifier], $allow_empty);
|
||||
} // We need to go deeper
|
||||
else {
|
||||
return $this->getPart($data[$identifier], $identifiers, $allow_empty);
|
||||
}
|
||||
@ -995,9 +1040,10 @@ class Validator
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
$set_to_break = false;
|
||||
foreach ($this->_validations as $v) {
|
||||
foreach ($v['fields'] as $field) {
|
||||
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field));
|
||||
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field), false);
|
||||
|
||||
// Don't validate if the field is not required and the value is empty
|
||||
if ($this->hasRule('optional', $field) && isset($values)) {
|
||||
@ -1005,7 +1051,7 @@ class Validator
|
||||
} elseif (
|
||||
$v['rule'] !== 'required' && !$this->hasRule('required', $field) &&
|
||||
$v['rule'] !== 'accepted' &&
|
||||
(! isset($values) || $values === '' || ($multiple && count($values) == 0))
|
||||
(!isset($values) || $values === '' || ($multiple && count($values) == 0))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
@ -1020,6 +1066,8 @@ class Validator
|
||||
|
||||
if (!$multiple) {
|
||||
$values = array($values);
|
||||
} else if (! $this->hasRule('required', $field)){
|
||||
$values = array_filter($values);
|
||||
}
|
||||
|
||||
$result = true;
|
||||
@ -1029,13 +1077,27 @@ class Validator
|
||||
|
||||
if (!$result) {
|
||||
$this->error($field, $v['message'], $v['params']);
|
||||
if ($this->stop_on_first_fail) {
|
||||
$set_to_break = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($set_to_break) break;
|
||||
}
|
||||
|
||||
return count($this->errors()) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the validation stop a rule is failed
|
||||
* @param bool $stop
|
||||
*/
|
||||
public function stopOnFirstFail($stop = true)
|
||||
{
|
||||
$this->stop_on_first_fail = (bool)$stop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all rule callbacks, the static and instance ones.
|
||||
*
|
||||
@ -1169,7 +1231,8 @@ class Validator
|
||||
// Get any other arguments passed to function
|
||||
$params = array_slice(func_get_args(), 2);
|
||||
|
||||
if (is_callable($rule) && !(is_string($rule) && $this->hasValidator($rule))) {
|
||||
if (is_callable($rule)
|
||||
&& !(is_string($rule) && $this->hasValidator($rule))) {
|
||||
$name = $this->getUniqueRuleName($fields);
|
||||
$message = isset($params[0]) ? $params[0] : null;
|
||||
$this->addInstanceRule($name, $rule, $message);
|
||||
@ -1330,7 +1393,7 @@ class Validator
|
||||
if (!empty($message)) {
|
||||
$added->message($message);
|
||||
}
|
||||
}, (array) $rules);
|
||||
}, (array)$rules);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
28
tests/Valitron/StopOnFirstFailTest.php
Normal file
28
tests/Valitron/StopOnFirstFailTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
use Valitron\Validator;
|
||||
|
||||
|
||||
class StopOnFirstFail extends BaseTestCase {
|
||||
|
||||
public function testStopOnFirstFail() {
|
||||
$rules = array(
|
||||
'myField1' => array(
|
||||
array('lengthMin', 5, 'message'=>'myField1 must be 5 characters minimum'),
|
||||
array('url', 'message' => 'myField1 is not a valid url'),
|
||||
array('urlActive', 'message' => 'myField1 is not an active url')
|
||||
)
|
||||
);
|
||||
|
||||
$v = new Validator(array(
|
||||
'myField1' => 'myVal'
|
||||
));
|
||||
|
||||
$v->mapFieldsRules($rules);
|
||||
$v->stopOnFirstFail(true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$errors = $v->errors();
|
||||
$this->assertCount(1, $errors['myField1']);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Valitron\Validator;
|
||||
|
||||
class ValidateTest extends BaseTestCase
|
||||
@ -15,12 +16,13 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertEquals($v->data(), array('foo' => 'bar'));
|
||||
}
|
||||
|
||||
public function testAccurateErrorCount()
|
||||
public function testAccurateErrorShouldReturnFalse()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester'));
|
||||
$v->rule('required', 'name');
|
||||
$this->assertSame(1, count($v->errors('name')));
|
||||
$this->assertFalse($v->errors('name'));
|
||||
}
|
||||
|
||||
public function testArrayOfFieldsToValidate()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||
@ -158,7 +160,8 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testAcceptedNotSet(){
|
||||
public function testAcceptedNotSet()
|
||||
{
|
||||
$v = new Validator();
|
||||
$v->rule('accepted', 'agree');
|
||||
$this->assertFalse($v->validate());
|
||||
@ -189,25 +192,52 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testIntegerStrict(){
|
||||
public function testIntegerStrict()
|
||||
{
|
||||
|
||||
$v = new Validator(array('num' => ' 41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => ' 41243'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '+41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
|
||||
$v = new Validator(array('num' => '+41243'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '-1'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '-0'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '0'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '+0'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '+1'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '0123'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '-0123'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testIntegerInvalid()
|
||||
@ -216,7 +246,6 @@ class ValidateTest extends BaseTestCase
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
|
||||
$v = new Validator(array('num' => '--1231'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
@ -592,6 +621,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'));
|
||||
@ -606,6 +649,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'));
|
||||
@ -620,13 +691,15 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testEmailDnsValid(){
|
||||
public function testEmailDnsValid()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||
$v->rule('emailDNS', 'email');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testEmailDnsInvalid(){
|
||||
public function testEmailDnsInvalid()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx'));
|
||||
$v->rule('emailDNS', 'email');
|
||||
$this->assertFalse($v->validate());
|
||||
@ -793,7 +866,7 @@ class ValidateTest extends BaseTestCase
|
||||
$v->rule(
|
||||
'dateBefore',
|
||||
'endDate',
|
||||
new DateTime('2013-04-08')
|
||||
new DateTime('2013-04-08')
|
||||
)->label('End date')->message('{field} must be before the end of the fiscal year, %s.');
|
||||
|
||||
$v->rule(
|
||||
@ -955,7 +1028,7 @@ class ValidateTest extends BaseTestCase
|
||||
$rules = array(
|
||||
'required' => 'nonexistent_field',
|
||||
'accepted' => 'foo',
|
||||
'integer' => 'foo'
|
||||
'integer' => 'foo'
|
||||
);
|
||||
|
||||
$v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz'));
|
||||
@ -1001,7 +1074,7 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAcceptBulkRulesWithNestedRules()
|
||||
{
|
||||
$rules = array(
|
||||
'length' => array(
|
||||
'length' => array(
|
||||
array('foo', 5),
|
||||
array('bar', 5)
|
||||
)
|
||||
@ -1022,7 +1095,7 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAcceptBulkRulesWithNestedRulesAndMultipleFields()
|
||||
{
|
||||
$rules = array(
|
||||
'length' => array(
|
||||
'length' => array(
|
||||
array(array('foo', 'bar'), 5),
|
||||
array('baz', 5)
|
||||
)
|
||||
@ -1043,7 +1116,7 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAcceptBulkRulesWithMultipleArrayParams()
|
||||
{
|
||||
$rules = array(
|
||||
'in' => array(
|
||||
'in' => array(
|
||||
array(array('foo', 'bar'), array('x', 'y'))
|
||||
)
|
||||
);
|
||||
@ -1059,15 +1132,16 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertEquals($v1->errors(), $v2->errors());
|
||||
}
|
||||
|
||||
public function testMalformedBulkRules(){
|
||||
$v = new Validator();
|
||||
$v->rules(
|
||||
array(
|
||||
'required'=>array('foo', 'bar')
|
||||
)
|
||||
);
|
||||
public function testMalformedBulkRules()
|
||||
{
|
||||
$v = new Validator();
|
||||
$v->rules(
|
||||
array(
|
||||
'required' => array('foo', 'bar')
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse($v->validate());
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testCustomLabelInMessage()
|
||||
@ -1083,30 +1157,30 @@ class ValidateTest extends BaseTestCase
|
||||
$v = new Valitron\Validator(array());
|
||||
$v->rule('required', array('name', 'email'))->message('{field} is required');
|
||||
$v->labels(array(
|
||||
'name' => 'Name',
|
||||
'email' => 'Email address'
|
||||
'name' => 'Name',
|
||||
'email' => 'Email address'
|
||||
));
|
||||
$v->validate();
|
||||
$this->assertEquals(array(
|
||||
'name' => array('Name is required'),
|
||||
'email' => array('Email address is required')
|
||||
'name' => array('Name is required'),
|
||||
'email' => array('Email address is required')
|
||||
), $v->errors());
|
||||
}
|
||||
|
||||
public function testCustomLabelArrayWithoutMessage()
|
||||
{
|
||||
$v = new Valitron\Validator(array(
|
||||
'password' => 'foo',
|
||||
'passwordConfirm' => 'bar'
|
||||
'password' => 'foo',
|
||||
'passwordConfirm' => 'bar'
|
||||
));
|
||||
$v->rule('equals', 'password', 'passwordConfirm');
|
||||
$v->labels(array(
|
||||
'password' => 'Password',
|
||||
'passwordConfirm' => 'Password Confirm'
|
||||
'password' => 'Password',
|
||||
'passwordConfirm' => 'Password Confirm'
|
||||
));
|
||||
$v->validate();
|
||||
$this->assertEquals(array(
|
||||
'password' => array("Password must be the same as 'Password Confirm'"),
|
||||
'password' => array("Password must be the same as 'Password Confirm'"),
|
||||
), $v->errors());
|
||||
}
|
||||
|
||||
@ -1117,7 +1191,9 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAddRuleClosure()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester'));
|
||||
$v->addRule('testRule', function() { return true; });
|
||||
$v->addRule('testRule', function () {
|
||||
return true;
|
||||
});
|
||||
$v->rule('testRule', 'name');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
@ -1125,7 +1201,9 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAddRuleClosureReturnsFalse()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester'));
|
||||
$v->addRule('testRule', function() { return false; });
|
||||
$v->addRule('testRule', function () {
|
||||
return false;
|
||||
});
|
||||
$v->rule('testRule', 'name');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
@ -1133,7 +1211,9 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAddRuleClosureWithFieldArray()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'foo@example.com'));
|
||||
$v->addRule('testRule', function() { return true; });
|
||||
$v->addRule('testRule', function () {
|
||||
return true;
|
||||
});
|
||||
$v->rule('testRule', array('name', 'email'));
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
@ -1141,7 +1221,9 @@ class ValidateTest extends BaseTestCase
|
||||
public function testAddRuleClosureWithArrayAsExtraParameter()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester'));
|
||||
$v->addRule('testRule', function() { return true; });
|
||||
$v->addRule('testRule', function () {
|
||||
return true;
|
||||
});
|
||||
$v->rule('testRule', 'name', array('foo', 'bar'));
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
@ -1154,8 +1236,16 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function sampleObjectCallback() { return true; }
|
||||
public function sampleObjectCallbackFalse() { return false; }
|
||||
public function sampleObjectCallback()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sampleObjectCallbackFalse()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function testAddRuleCallbackArray()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester'));
|
||||
@ -1219,14 +1309,14 @@ class ValidateTest extends BaseTestCase
|
||||
|
||||
public function testCreditCardValid()
|
||||
{
|
||||
$visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);
|
||||
$mastercard = array(5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965, 2223000048400011, 2223520043560014);
|
||||
$amex = array(371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316);
|
||||
$dinersclub = array(30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454);
|
||||
$discover = array(6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251);
|
||||
$visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);
|
||||
$mastercard = array(5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965, 2223000048400011, 2223520043560014);
|
||||
$amex = array(371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316);
|
||||
$dinersclub = array(30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454);
|
||||
$discover = array(6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251);
|
||||
|
||||
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
||||
foreach($numbers as $number) {
|
||||
foreach ($numbers as $number) {
|
||||
$v = new Validator(array('test' => $number));
|
||||
$v->rule('creditCard', 'test');
|
||||
$this->assertTrue($v->validate());
|
||||
@ -1243,14 +1333,14 @@ class ValidateTest extends BaseTestCase
|
||||
|
||||
public function testCreditCardInvalid()
|
||||
{
|
||||
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
|
||||
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);
|
||||
$amex = array(271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316);
|
||||
$dinersclub = array(20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454);
|
||||
$discover = array(5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251);
|
||||
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
|
||||
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);
|
||||
$amex = array(271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316);
|
||||
$dinersclub = array(20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454);
|
||||
$discover = array(5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251);
|
||||
|
||||
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
||||
foreach($numbers as $number) {
|
||||
foreach ($numbers as $number) {
|
||||
$v = new Validator(array('test' => $number));
|
||||
$v->rule('creditCard', 'test');
|
||||
$this->assertFalse($v->validate());
|
||||
@ -1349,29 +1439,29 @@ class ValidateTest extends BaseTestCase
|
||||
return array(
|
||||
array(
|
||||
'expected' => 'Test must be at least 140 long',
|
||||
'input' => 'tweeet',
|
||||
'test' => array(140),
|
||||
'message' => '{field} must be at least %d long',
|
||||
'input' => 'tweeet',
|
||||
'test' => array(140),
|
||||
'message' => '{field} must be at least %d long',
|
||||
),
|
||||
array(
|
||||
'expected' => 'Test must be between 1 and 140 characters',
|
||||
'input' => array(1, 2, 3),
|
||||
'test' => array(1, 140),
|
||||
'message' => 'Test must be between %d and %d characters',
|
||||
'input' => array(1, 2, 3),
|
||||
'test' => array(1, 140),
|
||||
'message' => 'Test must be between %d and %d characters',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOptionalProvidedValid()
|
||||
{
|
||||
$v = new Validator(array('address' => 'user@example.com'));
|
||||
$v = new Validator(array('address' => 'user@example.com'));
|
||||
$v->rule('optional', 'address')->rule('email', 'address');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testOptionalProvidedInvalid()
|
||||
{
|
||||
$v = new Validator(array('address' => 'userexample.com'));
|
||||
$v = new Validator(array('address' => 'userexample.com'));
|
||||
$v->rule('optional', 'address')->rule('email', 'address');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
@ -1522,6 +1612,38 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/vlucas/valitron/issues/262
|
||||
*/
|
||||
public function testOptionalArrayPartsAreIgnored()
|
||||
{
|
||||
$v = new Validator(array(
|
||||
'data' => array(
|
||||
array('foo' => '2018-01-01'),
|
||||
array('bar' => 1)
|
||||
)
|
||||
)
|
||||
);
|
||||
$v->rule('date', 'data.*.foo');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/vlucas/valitron/issues/262
|
||||
*/
|
||||
public function testRequiredArrayPartsAreNotIgnored()
|
||||
{
|
||||
$v = new Validator(array(
|
||||
'data' => array(
|
||||
array('foo' => '2018-01-01'),
|
||||
array('bar' => 1)
|
||||
)
|
||||
)
|
||||
);
|
||||
$v->rule('required', 'data.*.foo');
|
||||
$v->rule('date', 'data.*.foo');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
}
|
||||
|
||||
function sampleFunctionCallback($field, $value, array $params)
|
||||
|
||||
@ -16,4 +16,4 @@ if($vendorPos !== false) {
|
||||
$loader = require __DIR__.'/../vendor/autoload.php';
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/Valitron/BaseTestCase.php';
|
||||
require_once __DIR__ . '/Valitron/BaseTestCase.php';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user