Skip to content

Commit ee99622

Browse files
authored
Merge pull request #32 from swaggest/complex-enum
deep compare for complex enums, resolves #31
2 parents f03e5a6 + 401eccf commit ee99622

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Schema.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,17 @@ private function processEnum($data, $path = '#')
222222
{
223223
$enumOk = false;
224224
foreach ($this->enum as $item) {
225-
if ($item === $data) { // todo support complex structures here
225+
if ($item === $data) {
226226
$enumOk = true;
227227
break;
228+
} else {
229+
if (is_array($item) || is_object($item)) {
230+
$diff = new JsonDiff($item, $data, JsonDiff::STOP_ON_DIFF);
231+
if ($diff->getDiffCnt() === 0) {
232+
$enumOk = true;
233+
break;
234+
}
235+
}
228236
}
229237
}
230238
if (!$enumOk) {

tests/resources/suite/enum.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[
2+
{
3+
"description": "complex enum validation",
4+
"schema": {
5+
"enum": [
6+
6,
7+
"foo",
8+
[],
9+
true,
10+
{
11+
"foo": 12
12+
},
13+
[{"one": 1,"two":2}]
14+
]
15+
},
16+
"tests": [
17+
{
18+
"description": "one of the enum is valid",
19+
"data": [],
20+
"valid": true
21+
},
22+
{
23+
"description": "missing from enum is not valid",
24+
"data": [1,2],
25+
"valid": false
26+
},
27+
{
28+
"description": "something else is invalid",
29+
"data": null,
30+
"valid": false
31+
},
32+
{
33+
"description": "objects are deep compared",
34+
"data": {
35+
"foo": false
36+
},
37+
"valid": false
38+
},
39+
{
40+
"description": "objects are deep compared 2",
41+
"data": {
42+
"foo": 12
43+
},
44+
"valid": true
45+
},
46+
{
47+
"description": "objects are deep compared 3",
48+
"data": [
49+
{
50+
"two": 2,
51+
"one": 1
52+
}
53+
],
54+
"valid": true
55+
},
56+
{
57+
"description": "objects are deep compared 4",
58+
"data": [
59+
{
60+
"one": 1,
61+
"two": 2
62+
}
63+
],
64+
"valid": true
65+
}
66+
]
67+
}
68+
]

0 commit comments

Comments
 (0)