Skip to content

Commit

Permalink
required validator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Aug 24, 2020
1 parent cd38950 commit da5f3a7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Validators/RequiredValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Packaged\Validate\Validators;

use Generator;
use Packaged\Validate\AbstractSerializableValidator;
use Packaged\Validate\SerializableValidator;

class RequiredValidator extends AbstractSerializableValidator
{
public static function deserialize($configuration): SerializableValidator
{
return new static();
}

public function serialize(): array
{
return [];
}

protected function _doValidate($value): Generator
{
if($value === null || $value === '')
{
yield $this->_makeError('required');
}
}
}
19 changes: 19 additions & 0 deletions tests/RequiredValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Packaged\Validate\Tests;

use Packaged\Validate\Validators\RequiredValidator;
use PHPUnit\Framework\TestCase;

class RequiredValidatorTest extends TestCase
{
public function testRequired()
{
$validator = new RequiredValidator();
$this->assertFalse($validator->isValid(null));
$this->assertFalse($validator->isValid(''));
$this->assertTrue($validator->isValid('test'));
$this->assertTrue($validator->isValid(0));
$this->assertTrue($validator->isValid(false));
$this->assertTrue($validator->isValid(true));
}
}

0 comments on commit da5f3a7

Please sign in to comment.