Skip to content

Commit

Permalink
Add numeric whitelist validator
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Jun 11, 2024
1 parent 8fe57da commit 373be4c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/Validator/NumericWhiteList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Utopia\Validator;

use Utopia\Validator;

/**
* Numeric WhiteList
*
* Checks if a variable is inside predefined numerical white list.
*/
class NumericWhiteList extends Validator
{
/**
* @var int[]
*/
protected array $list;

/**
* @var bool
*/
protected bool $strict;

/**
* Constructor
*
* Sets a white list array.
*
* @param int[] $list
* @param bool $strict disable type check
*/
public function __construct(array $list, bool $strict = flase)
{
$this->list = $list;
$this->strict = $strict;
}

/**
* Get List of All Allowed Values
*
* @return array
*/
public function getList(): array
{
return $this->list;
}

/**
* Get Description
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
return 'Value must be one of ('.\implode(', ', $this->list).')';
}

/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}

/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_INTEGER;
}

/**
* Is valid
*
* Validation will pass if $value is in the white list array.
*
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool
{
if (\is_array($value)) {
return false;
}

if (!\in_array($value, $this->list, $this->strict)) {
return false;
}

return true;
}
}
38 changes: 38 additions & 0 deletions tests/Validator/NumericWhitelistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Utopia\Validator;

use PHPUnit\Framework\TestCase;

class NumericWhiteListTest extends TestCase
{
public function testCanValidateStrictly()
{
$whiteList = new NumericWhiteList([1, 2, 3, 4], true);

$this->assertTrue($whiteList->isValid(3));
$this->assertTrue($whiteList->isValid(4));

$this->assertFalse($whiteList->isValid('STRING1'));
$this->assertFalse($whiteList->isValid('strIng1'));
$this->assertFalse($whiteList->isValid('3'));
$this->assertFalse($whiteList->isValid(5));
$this->assertFalse($whiteList->isArray());
$this->assertEquals($whiteList->getList(), [1 ,2, 3, 4]);
$this->assertEquals(\Utopia\Validator::TYPE_INTEGER, $whiteList->getType());
}

public function testCanValidateLoosely(): void
{
$whiteList = new NumericWhiteList([1, 2, 3, 4]);

$this->assertTrue($whiteList->isValid(3));
$this->assertTrue($whiteList->isValid(4));
$this->assertTrue($whiteList->isValid('3'));
$this->assertTrue($whiteList->isValid('4'));
$this->assertFalse($whiteList->isValid('STRING1'));
$this->assertFalse($whiteList->isValid('strIng1'));
$this->assertFalse($whiteList->isValid('5'));
$this->assertEquals($whiteList->getList(), [1, 2, 3, 4]);
}
}

0 comments on commit 373be4c

Please sign in to comment.