Skip to content

Commit

Permalink
Merge pull request #23 from lohanidamodar/feat-numeric-validators
Browse files Browse the repository at this point in the history
update integer and float validator tests to test loose condition
  • Loading branch information
eldadfux authored Aug 2, 2021
2 parents 3b94251 + b5cf284 commit 3cd5fa2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Validator/FloatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
*/
class FloatValidator extends Validator
{
/**
* @var bool
*/
protected $loose = false;

/**
* Pass true to accept float strings as valid float values
* This option is good for validating query string params.
*
* @param bool $loose
*/
public function __construct(bool $loose = false)
{
$this->loose = $loose;
}

/**
* Get Description
*
Expand Down Expand Up @@ -69,6 +85,12 @@ public function getType(): string
*/
public function isValid($value)
{
if($this->loose) {
if(!\is_numeric($value)) {
return false;
}
$value = $value+0;
}
if (!\is_float($value)) {
return false;
}
Expand Down
23 changes: 22 additions & 1 deletion src/Validator/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Utopia\Validator;

use Utopia\Validator;

/**
* Integer
*
Expand All @@ -23,6 +22,22 @@
*/
class Integer extends Validator
{
/**
* @var bool
*/
protected $loose = false;

/**
* Pass true to accept integer strings as valid integer values
* This option is good for validating query string params.
*
* @param bool $loose
*/
public function __construct(bool $loose = false)
{
$this->loose = $loose;
}

/**
* Get Description
*
Expand Down Expand Up @@ -69,6 +84,12 @@ public function getType(): string
*/
public function isValid($value)
{
if($this->loose) {
if(!\is_numeric($value)) {
return false;
}
$value = $value+0;
}
if (!\is_int($value)) {
return false;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Validator/FloatValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ class FloatValidatorTest extends TestCase
*/
protected $validator = null;

/**
* @var FloatValidator
*/
protected $looseValidator = null;

public function setUp():void
{
$this->validator = new FloatValidator();
$this->looseValidator = new FloatValidator(true);
}

public function tearDown():void
{
$this->validator = null;
$this->looseValidator = null;
}

public function testIsValid()
Expand All @@ -45,5 +52,17 @@ public function testIsValid()
$this->assertEquals($this->validator->isValid('23'), false);
$this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_FLOAT);
$this->assertEquals($this->validator->isArray(), false);

// Assertions Loose
$this->assertEquals($this->looseValidator->isValid(27.25), true);
$this->assertEquals($this->looseValidator->isValid('abc'), false);
$this->assertEquals($this->looseValidator->isValid(23), false);
$this->assertEquals($this->looseValidator->isValid(23.5), true);
$this->assertEquals($this->looseValidator->isValid(1e7), true);
$this->assertEquals($this->looseValidator->isValid(true), false);
$this->assertEquals($this->looseValidator->isValid('23.5'), true);
$this->assertEquals($this->looseValidator->isValid('23'), false);
$this->assertEquals($this->looseValidator->getType(), \Utopia\Validator::TYPE_FLOAT);
$this->assertEquals($this->looseValidator->isArray(), false);
}
}
18 changes: 18 additions & 0 deletions tests/Validator/IntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ class IntegerTest extends TestCase
* @var \Utopia\Validator\Integer
*/
protected $validator = null;

/**
* @var \Utopia\Validator\Integer
*/
protected $looseValidator = null;

public function setUp():void
{
$this->validator = new Integer();
$this->looseValidator = new Integer(true);
}

public function tearDown():void
{
$this->validator = null;
$this->looseValidator = null;
}

public function testIsValid()
Expand All @@ -44,5 +51,16 @@ public function testIsValid()
$this->assertEquals($this->validator->isValid(false), false);
$this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_INTEGER);
$this->assertEquals($this->validator->isArray(), false);

// Assertions loose
$this->assertEquals($this->looseValidator->isValid(23), true);
$this->assertEquals($this->looseValidator->isValid('23'), true);
$this->assertEquals($this->looseValidator->isValid(23.5), false);
$this->assertEquals($this->looseValidator->isValid('23.5'), false);
$this->assertEquals($this->looseValidator->isValid(null), false);
$this->assertEquals($this->looseValidator->isValid(true), false);
$this->assertEquals($this->looseValidator->isValid(false), false);
$this->assertEquals($this->looseValidator->getType(), \Utopia\Validator::TYPE_INTEGER);
$this->assertEquals($this->looseValidator->isArray(), false);
}
}

0 comments on commit 3cd5fa2

Please sign in to comment.