-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve dataset validation, add ConfirmationValidator (#4)
- Loading branch information
Showing
10 changed files
with
201 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {DataSetValidator, ValidationResponse} from '../validator.js'; | ||
|
||
export class ConfirmationValidator extends DataSetValidator | ||
{ | ||
constructor(field) | ||
{ | ||
super(); | ||
this._field = field; | ||
} | ||
|
||
static deserialize(config) | ||
{ | ||
return new this(config.field); | ||
} | ||
|
||
validate(value) | ||
{ | ||
const data = this.getData(); | ||
const compare = data[this._field] ? data[this._field] : null; | ||
if(compare !== value) | ||
{ | ||
return ValidationResponse.error(['value does not match']); | ||
} | ||
return ValidationResponse.success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Packaged\Validate; | ||
|
||
trait DatasetValidatorTrait | ||
{ | ||
protected $_data = []; | ||
|
||
public function setData(array $data): self | ||
{ | ||
$this->_data = $data; | ||
return $this; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return $this->_data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ | |
|
||
interface IDataSetValidator | ||
{ | ||
public function setData(array $data): self; | ||
|
||
public function getData(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace Packaged\Validate\Validators; | ||
|
||
use Generator; | ||
use Packaged\Validate\AbstractSerializableValidator; | ||
use Packaged\Validate\DatasetValidatorTrait; | ||
use Packaged\Validate\IDataSetValidator; | ||
use Packaged\Validate\SerializableValidator; | ||
|
||
class ConfirmationValidator extends AbstractSerializableValidator implements IDataSetValidator | ||
{ | ||
use DatasetValidatorTrait; | ||
|
||
protected $_field; | ||
|
||
public function __construct($field) | ||
{ | ||
$this->_field = $field; | ||
} | ||
|
||
public static function deserialize($configuration): SerializableValidator | ||
{ | ||
return new static($configuration->field); | ||
} | ||
|
||
public function serialize(): array | ||
{ | ||
return [ | ||
'field' => $this->_field, | ||
]; | ||
} | ||
|
||
protected function _doValidate($value): Generator | ||
{ | ||
$compare = $this->_data[$this->_field] ?? null; | ||
if($compare !== $value) | ||
{ | ||
yield $this->_makeError('value does not match'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Packaged\Validate\Tests; | ||
|
||
use Packaged\Validate\Validators\ConfirmationValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ConfirmationValidatorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provider | ||
*/ | ||
public function testConfirmationValidator(array $data, bool $expect) | ||
{ | ||
$validator = new ConfirmationValidator('field2'); | ||
$validator->setData($data); | ||
|
||
$this->assertEquals($expect, $validator->isValid($data['field1'] ?? null)); | ||
} | ||
|
||
public function provider() | ||
{ | ||
return [ | ||
[['field1' => 10], false], | ||
[['field2' => ''], false], | ||
[['field1' => 'yes', 'field2' => 'no'], false], | ||
[['field1' => 'no', 'field2' => 'yes'], false], | ||
[['field1' => 'test', 'field2' => 'test'], true], | ||
[['field1' => 123, 'field2' => 123], true], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
<?php | ||
namespace Packaged\Validate\Tests; | ||
|
||
use Packaged\Validate\ValidationException; | ||
use Packaged\Validate\Validators\EqualValidator; | ||
use Packaged\Validate\Validators\NumberValidator; | ||
use Packaged\Validate\Validators\RemoteValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RemoteValidatorTest extends TestCase | ||
{ | ||
public function testRemoteValidator() | ||
/** | ||
* @dataProvider provider | ||
*/ | ||
public function testRemoteValidator(array $data, bool $expect) | ||
{ | ||
$validator = new RemoteValidator('testField'); | ||
$validator->addValidator(new NumberValidator(0, 5), 'prereqField', new EqualValidator('abc')); | ||
$this->assertTrue($validator->isValid([])); | ||
$this->assertTrue($validator->isValid(['testField' => 10])); | ||
$this->assertTrue($validator->isValid(['prereqField' => ''])); | ||
$this->assertTrue($validator->isValid(['prereqField' => ''])); | ||
$this->assertFalse($validator->isValid(['prereqField' => 'abc'])); | ||
$this->assertFalse($validator->isValid(['prereqField' => 'abc', 'testField' => 10])); | ||
$this->assertTrue($validator->isValid(['prereqField' => 'abc', 'testField' => 3])); | ||
$validator->setData($data); | ||
|
||
$this->expectException(ValidationException::class); | ||
$this->expectExceptionMessage('not a valid value for a dataset validator'); | ||
$validator->assert('invalid'); | ||
$this->assertEquals($expect, $validator->isValid($data['testField'] ?? null)); | ||
} | ||
|
||
public function provider() | ||
{ | ||
return [ | ||
[['testField' => 10], true], | ||
[['prereqField' => ''], true], | ||
[['prereqField' => ''], true], | ||
[['prereqField' => 'abc'], false], | ||
[['prereqField' => 'abc', 'testField' => 10], false], | ||
[['prereqField' => 'abc', 'testField' => 3], true], | ||
]; | ||
} | ||
} |