Skip to content

Commit 78d293d

Browse files
Merge pull request #137 from utopia-php/feat-anyof-validator
Added AnyOf Validator
2 parents 8fe57da + bfe39bd commit 78d293d

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/Validator/AnyOf.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Utopia\Validator;
4+
5+
use Utopia\Validator;
6+
7+
/**
8+
* Ensure at least one validator from a list passed the check
9+
*
10+
* @package Utopia\Validator
11+
*/
12+
class AnyOf extends Validator
13+
{
14+
protected ?Validator $failedRule = null;
15+
16+
/**
17+
* @param array<Validator> $validators
18+
*/
19+
public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED)
20+
{
21+
}
22+
23+
/**
24+
* Get Description
25+
*
26+
* Returns validator description
27+
*
28+
* @return string
29+
*/
30+
public function getDescription(): string
31+
{
32+
if (!(\is_null($this->failedRule))) {
33+
$description = $this->failedRule->getDescription();
34+
} else {
35+
$description = $this->validators[0]->getDescription();
36+
}
37+
38+
return $description;
39+
}
40+
41+
/**
42+
* Is valid
43+
*
44+
* Validation will pass when all rules are valid if only one of the rules is invalid validation will fail.
45+
*
46+
* @param mixed $value
47+
* @return bool
48+
*/
49+
public function isValid(mixed $value): bool
50+
{
51+
foreach ($this->validators as $rule) {
52+
$valid = $rule->isValid($value);
53+
54+
$this->failedRule = $rule;
55+
56+
if ($valid) {
57+
return true;
58+
}
59+
}
60+
61+
return false;
62+
}
63+
64+
/**
65+
* Get Type
66+
*
67+
* Returns validator type.
68+
*
69+
* @return string
70+
*/
71+
public function getType(): string
72+
{
73+
return $this->type;
74+
}
75+
76+
/**
77+
* Is array
78+
*
79+
* Function will return true if object is array.
80+
*
81+
* @return bool
82+
*/
83+
public function isArray(): bool
84+
{
85+
return true;
86+
}
87+
}

tests/Validator/MultipleOfTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Utopia\Validator;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Utopia\Validator;
7+
8+
class MultipleOfTest extends TestCase
9+
{
10+
public function setUp(): void
11+
{
12+
}
13+
14+
public function testIsValid()
15+
{
16+
$validTextValidUrl = 'http://example.com';
17+
$validTextInvalidUrl = 'hello world';
18+
$invalidTextValidUrl = 'http://example.com/very-long-url';
19+
$invalidTextInvalidUrl = 'Some very long text that is also not an URL';
20+
21+
$vaidator = new AnyOf([new Text(20), new URL()], Validator::TYPE_STRING);
22+
$this->assertTrue($vaidator->isValid($validTextValidUrl));
23+
$this->assertTrue($vaidator->isValid($validTextInvalidUrl));
24+
$this->assertTrue($vaidator->isValid($invalidTextValidUrl));
25+
$this->assertFalse($vaidator->isValid($invalidTextInvalidUrl));
26+
}
27+
}

0 commit comments

Comments
 (0)