Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #59 with new build-in validator NotMatch , NotEqual #61

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/validation_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ title: Built-in validation rules
2. `NotRegex`: value must NOT match a regular expression pattern. Rule options: `pattern`
3. `Callback`: checks if a value is valid using a custom callback (a function, an object's method, a class' static method). Rule options: `callback` and `arguments` (additional paramters for the callback)
4. `Match`: the value must match the value of another item in the context. Rule options: `item` (eg: if `auth[password_confirm]` must match `auth[password]` the `item` is `auth[password]`
5. `Equal`: the value must be the same as predefined value. Rule options: `value`
5. `NotMatch': the value must not match the value of another item in the context. Rule options: `item` (eg: if `auth[password]` must not match `auth[username]` the `item` is `auth[username]`
6. `Equal`: the value must be the same as predefined value. Rule options: `value`
7. `NotEqual`: the value must not be the same as predefined value. Rule options: `value`

### File validators
File validators work only with local files and they fail if the file does not exist
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sirius Validation
# Sirius Validation

[![Source Code](http://img.shields.io/badge/source-siriusphp/validation-blue.svg?style=flat-square)](https://github.com/siriusphp/validation)
[![Latest Version](https://img.shields.io/packagist/v/siriusphp/validation.svg?style=flat-square)](https://github.com/siriusphp/validation/releases)
Expand All @@ -18,7 +18,7 @@ Sirius Validation is a library for data validation. It offers:
Out-of-the-box, the library can handle `array`s, `ArrayObject`s and objects that have implemented the `toArray` method.
In order to validate other data containers you must create a [`DataWrapper`](https://github.com/siriusphp/validation/blob/master/src/Validation/DataWrapper/WrapperInterface.php) so that the validator be able to extract data from your object.

##Elevator pitch
## Elevator pitch

```php
$validator = new \Sirius\Validation\Validator;
Expand Down Expand Up @@ -60,12 +60,12 @@ $validator->add('shipping_address[city]:City', 'MyApp\Validator\City'); // uses

```

##Links
## Links

- [documentation](http://sirius.ro/php/sirius/validation/)
- [changelog](CHANGELOG.md)

##Known issues
## Known issues

In PHP 5.3 there is some problem with the SplObject storage that prevents the library to remove validation rules.
This means that in PHP 5.3, you cannot remove a validation rule from a `Validator` or `ValueValidator` object
Expand Down
9 changes: 9 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ public static function equalTo($value, $otherElementOrValue, $context = null)
return $value == Arr::getByPath($context, $otherElementOrValue);
}

public static function notEqualTo($value, $otherElementOrValue, $context = null)
{
if (func_num_args() == 2) {
return $value != $otherElementOrValue;
}

return $value != Arr::getByPath($context, $otherElementOrValue);
}

public static function date($value, $format = 'Y-m-d')
{
$validator = new Rule\Date(
Expand Down
16 changes: 16 additions & 0 deletions src/Rule/NotEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Sirius\Validation\Rule;

class NotEqual extends Equal
{
const MESSAGE = 'This input is equal to {value}';
const LABELED_MESSAGE = '{label} is equal to {value}';

public function validate($value, $valueIdentifier = null)
{
parent::validate($value, $valueIdentifier);
$this->success = ! $this->success;

return $this->success;
}
}
16 changes: 16 additions & 0 deletions src/Rule/NotMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Sirius\Validation\Rule;

class NotMatch extends Match
{
const MESSAGE = 'This input does match {item}';
const LABELED_MESSAGE = '{label} does match {item}';

public function validate($value, $valueIdentifier = null)
{
parent::validate($value, $valueIdentifier);
$this->success = ! $this->success;

return $this->success;
}
}
2 changes: 2 additions & 0 deletions src/RuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ protected function registerDefaultRules()
'Match',
'MaxLength',
'MinLength',
'NotEqual',
'NotInList',
'NotMatch',
'NotRegex',
'Number',
'Regex',
Expand Down
4 changes: 4 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ class Validator implements ValidatorInterface

const RULE_MATCH = 'match';

const RULE_NOT_MATCH = 'notmatch';

const RULE_EQUAL = 'equal';

const RULE_NOT_EQUAL = 'notequal';

const RULE_CALLBACK = 'callback';

// files rules
Expand Down
10 changes: 5 additions & 5 deletions tests/src/ComplexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function setUp()
$this->validator
->add('email', 'email | required')// does the order matter?
->add('email_confirm', 'required | email | match(item=email)')
->add('password', 'required')
->add('password', 'required | notmatch(item=email)')
->add('password_confirm', 'required | match(item=password)')
->add('feedback', 'requiredwith(item=agree_to_provide_feedback)')
->add('birthday', 'requiredwhen', array( 'item' => 'email_confirm', 'rule' => 'Email' ))
Expand Down Expand Up @@ -41,8 +41,8 @@ function notestWithCorrectData()
function testWithInvalidData()
{
$data = array(
'email_confirm' => 'me@domain.com',
'password' => '1234',
'email' => 'me@domain.com',
'password' => 'me@domain.com',
'password_confirm' => '123456',
'agree_to_provide_feedback' => true,
'lines' => array(
Expand All @@ -52,10 +52,10 @@ function testWithInvalidData()
$this->validator->validate($data);
$messages = $this->validator->getMessages();

$this->assertEquals('This field is required', (string) $messages['email'][0]);
$this->assertEquals('This field is required', (string) $messages['email_confirm'][0]);
$this->assertEquals('This input does match email', (string) $messages['password'][0]);
$this->assertEquals('This input does not match password', (string) $messages['password_confirm'][0]);
$this->assertEquals('This field is required', (string) $messages['feedback'][0]);
$this->assertEquals('This field is required', (string) $messages['lines[0][price]'][0]);
}

}
43 changes: 43 additions & 0 deletions tests/src/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,55 @@ function testOfEqualToWithContext()
}
}

function testOfNotEqualToWithContext()
{
$pool = array(
array(
'value',
'element_1',
false
),
array(
'value',
'element_2',
true
),
array(
'new value',
'element_3[sub_element_1][sub_element_2]',
false
)
);
$context = array(
'element_1' => 'value',
'element_2' => 'another_value',
'element_3' => array(
'sub_element_1' => array(
'sub_element_2' => 'new value'
)
)
);
foreach ($pool as $sample) {
$this->assertSame(
Helper::notEqualTo($sample[0], $sample[1], $context),
$sample[2],
sprintf("%s %s", $sample[0], $sample[1])
);
}
}

function testOfEqualToWithoutContext()
{
$this->assertTrue(Helper::equalTo(5, '5'));
$this->assertFalse(Helper::equalTo(5, 'a'));
}

function testOfNotEqualToWithoutContext()
{
$this->assertTrue(Helper::NotEqualTo(5, 'a'));
$this->assertFalse(Helper::NotEqualTo(5, '5'));
}

function testOfWebsite()
{
$pool = array(
Expand Down
28 changes: 28 additions & 0 deletions tests/src/Rule/NotEqualTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Sirius\Validation\Rule;

use Sirius\Validation\Rule\NotEqual as Rule;

class NotEqualTest extends \PHPUnit_Framework_TestCase
{

function setUp()
{
$this->rule = new Rule();
}

function testValidationWithOptionSet()
{
$this->rule->setOption(Rule::OPTION_VALUE, '123');
$this->assertFalse($this->rule->validate('123'));
$this->assertTrue($this->rule->validate('abc'));
}

function testValidationWithoutOptionSet()
{
$this->assertFalse($this->rule->validate('abc'));
$this->assertFalse($this->rule->validate(null));
}

}
36 changes: 36 additions & 0 deletions tests/src/Rule/NotMatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sirius\Validation\Rule;

use Sirius\Validation\DataWrapper\ArrayWrapper;
use Sirius\Validation\Rule\NotMatch as Rule;

class NotMatchTest extends \PHPUnit_Framework_TestCase
{

function setUp()
{
$this->rule = new Rule();
$this->rule->setContext(
new ArrayWrapper(
array(
'password' => 'secret'
)
)
);
}

function testValidationWithItemPresent()
{
$this->rule->setOption(Rule::OPTION_ITEM, 'password');
$this->assertFalse($this->rule->validate('secret'));
$this->assertTrue($this->rule->validate('abc'));
}

function testValidationWithoutItemPresent()
{
$this->assertFalse($this->rule->validate('abc'));
$this->assertFalse($this->rule->validate(null));
}

}