Skip to content

Commit 3b982ef

Browse files
authored
Merge pull request #32 from rollun-com/iliya
fix bug enum. allow type array value
2 parents 933cf92 + d72b23b commit 3b982ef

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ services:
2828
volumes:
2929
- ./:/var/www/app
3030

31-
rollun-openapi-logstash:
32-
image: docker.elastic.co/logstash/logstash:7.7.0
33-
volumes:
34-
- ./docker/logstash/config/:/usr/share/logstash/config/
31+
# rollun-openapi-logstash:
32+
# image: docker.elastic.co/logstash/logstash:7.7.0
33+
# volumes:
34+
# - ./docker/logstash/config/:/usr/share/logstash/config/

src/OpenAPI/Server/Validator/Enum.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,29 @@ protected function castToType($item)
6868
}
6969

7070
public function isValid($value): bool
71+
{
72+
if (is_array($value)) {
73+
return $this->validateArray($value);
74+
}
75+
76+
return $this->isInArray($value);
77+
}
78+
79+
protected function isInArray($value): bool
7180
{
7281
if (!$result = in_array($value, $this->allowed, true)) {
7382
$this->error(self::INVALID, $value);
7483
}
75-
7684
return $result;
7785
}
86+
87+
protected function validateArray(array $value): bool
88+
{
89+
foreach ($value as $item) {
90+
if (!$this->isInArray($item)) {
91+
return false;
92+
}
93+
}
94+
return true;
95+
}
7896
}

test/unit/Server/Validator/EnumTest.php

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testStringBoolNotValid(): void
2222
// false converts to empty string since we use implode
2323
self::assertEquals([
2424
Enum::INVALID => "The value 'false' not in enum list."
25-
],$messages);
25+
], $messages);
2626
}
2727

2828
public function testStringCastsToInt()
@@ -46,7 +46,7 @@ public function testStringWithQuotes()
4646
self::assertNotEmpty($messages = $validator->getMessages());
4747
self::assertEquals([
4848
Enum::INVALID => "The value 'another' not in enum list."
49-
],$messages);
49+
], $messages);
5050
}
5151

5252
public function testString()
@@ -59,6 +59,78 @@ public function testString()
5959
self::assertNotEmpty($messages = $validator->getMessages());
6060
self::assertEquals([
6161
Enum::INVALID => "The value 'another' not in enum list."
62-
],$messages);
62+
], $messages);
6363
}
64-
}
64+
65+
public function testArrayInt()
66+
{
67+
$validator = new Enum(['allowed' => [
68+
"random",
69+
"anotherRandom",
70+
]]);
71+
72+
self::assertFalse($validator->isValid([1]));
73+
self::assertNotEmpty($messages = $validator->getMessages());
74+
self::assertEquals([
75+
Enum::INVALID => "The value '1' not in enum list."
76+
], $messages);
77+
}
78+
79+
public function testArrayEmpty()
80+
{
81+
$validator = new Enum(['allowed' => [
82+
"random",
83+
"anotherRandom",
84+
]]);
85+
86+
self::assertTrue($validator->isValid([]));
87+
self::assertEmpty($validator->getMessages());
88+
}
89+
90+
public function testValidArrayOneItem()
91+
{
92+
$validator = new Enum(['allowed' => [
93+
"random",
94+
"anotherRandom",
95+
]]);
96+
self::assertTrue($validator->isValid(["random"]));
97+
self::assertEmpty($validator->getMessages());
98+
}
99+
100+
public function testValidArraySeveralItems()
101+
{
102+
$validator = new Enum(['allowed' => [
103+
"random",
104+
"anotherRandom",
105+
"anotherRandomWord",
106+
]]);
107+
self::assertTrue($validator->isValid(["random", "anotherRandom"]));
108+
self::assertEmpty($validator->getMessages());
109+
}
110+
111+
public function testInvalidArrayOneItem()
112+
{
113+
$validator = new Enum(['allowed' => [
114+
"random",
115+
"anotherRandom",
116+
]]);
117+
self::assertFalse($validator->isValid(["anotherRandomWord"]));
118+
self::assertNotEmpty($messages = $validator->getMessages());
119+
self::assertEquals([
120+
Enum::INVALID => "The value 'anotherRandomWord' not in enum list."
121+
], $messages);
122+
}
123+
124+
public function testInvalidArraySeveralItems()
125+
{
126+
$validator = new Enum(['allowed' => [
127+
"random",
128+
"anotherRandom",
129+
]]);
130+
self::assertFalse($validator->isValid(["anotherRandomWord", "sameRandomWord"]));
131+
self::assertNotEmpty($messages = $validator->getMessages());
132+
self::assertEquals([
133+
Enum::INVALID => "The value 'anotherRandomWord' not in enum list."
134+
], $messages);
135+
}
136+
}

0 commit comments

Comments
 (0)