Skip to content

Commit bb99e84

Browse files
authored
Merge pull request #11 from undabot/develop
Develop
2 parents 127c54e + 500d4ff commit bb99e84

File tree

2 files changed

+85
-55
lines changed

2 files changed

+85
-55
lines changed

src/Util/ValidResourceAssertion.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function assert(array $resource): void
2020
JsonApiAssertion::keyIsset($resource, 'type', 'Resource must have key `type`');
2121

2222
// According to the JSON:API specification, these are the only allowed keys
23-
$allowedKeys = ['type', 'id', 'links', 'meta', 'attributes', 'relationships'];
23+
$allowedKeys = ['type', 'id', 'lid', 'links', 'meta', 'attributes', 'relationships'];
2424

2525
// Assert that there are only whitelisted keys present
2626
$disallowedKeys = array_diff(array_keys($resource), $allowedKeys);
@@ -32,10 +32,21 @@ public static function assert(array $resource): void
3232

3333
JsonApiAssertion::string($resource['type'], 'Resource `type` must be string');
3434

35-
// Id is optional for resources sent in POST requests
36-
if (true === \array_key_exists('id', $resource)) {
35+
// id or lid are optional for resources sent in POST requests
36+
$isIdIncluded = true === \array_key_exists('id', $resource);
37+
$isLidIncluded = true === \array_key_exists('lid', $resource);
38+
if ($isIdIncluded) {
3739
JsonApiAssertion::string($resource['id'], 'Resource `id` must be string');
40+
// If id is included, lid can't be included
41+
if ($isLidIncluded) {
42+
throw new ValidationException('Resource can not have both `id` and `lid`', 0);
43+
}
3844
}
45+
// if there is no id and lid is included, it must be a string
46+
if ($isLidIncluded) {
47+
JsonApiAssertion::string($resource['lid'], 'Resource `lid` must be string');
48+
}
49+
3950

4051
// @todo validate attributes
4152
// @todo validate relationships

tests/Unit/Util/Assert/ValidJsonResourceAssertionTest.php

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,78 +35,97 @@ public function testValidateInvalidResourceArray(array $resource): void
3535
ValidResourceAssertion::assert($resource);
3636
}
3737

38-
public function validResourceData()
38+
public function validResourceData(): \Generator
3939
{
40-
return [
40+
yield 'Only valid id and type exists' => [
4141
[
42-
[
43-
'id' => '1',
44-
'type' => 'x',
45-
],
42+
'id' => '1',
43+
'type' => 'x',
4644
],
45+
];
46+
47+
yield 'Valid id and type exists with empty attributes and relationships' => [
48+
[
49+
'id' => '1',
50+
'type' => 'x',
51+
'attributes' => [],
52+
'relationships' => [],
53+
],
54+
];
55+
56+
yield 'Valid id and type exists with empty meta and links' => [
4757
[
48-
[
49-
'id' => '1',
50-
'type' => 'x',
51-
'attributes' => [],
52-
'relationships' => [],
53-
],
58+
'id' => '1',
59+
'type' => 'x',
60+
'meta' => [],
61+
'links' => [],
5462
],
63+
];
64+
65+
yield 'Valid id and type exists with empty attributes, relationships, meta and links' => [
5566
[
56-
[
57-
'id' => '1',
58-
'type' => 'x',
59-
'meta' => [],
60-
'links' => [],
61-
],
67+
'id' => '1',
68+
'type' => 'x',
69+
'attributes' => [],
70+
'relationships' => [],
71+
'meta' => [],
72+
'links' => [],
6273
],
74+
];
75+
76+
yield 'Only valid lid and type exists' => [
6377
[
64-
[
65-
'id' => '1',
66-
'type' => 'x',
67-
'attributes' => [],
68-
'relationships' => [],
69-
'meta' => [],
70-
'links' => [],
71-
],
78+
'lid' => '1',
79+
'type' => 'x',
7280
],
7381
];
7482
}
7583

76-
public function invalidResourceData()
84+
public function invalidResourceData(): \Generator
7785
{
78-
return [
86+
yield 'Missing type' => [
7987
[
80-
[
81-
'id' => '1',
82-
],
88+
'id' => '1',
8389
],
90+
];
91+
92+
yield 'Typo in type' => [
93+
[
94+
'id' => '1',
95+
'typex' => 'x',
96+
'attributes' => [],
97+
'relationships' => [],
98+
],
99+
];
100+
101+
yield 'Typo in id' => [
84102
[
85-
[
86-
'id' => '1',
87-
'typex' => 'x',
88-
'attributes' => [],
89-
'relationships' => [],
90-
],
103+
'idx' => '1',
104+
'type' => 'x',
105+
'meta' => [],
106+
'links' => [],
91107
],
108+
];
109+
110+
yield 'Forbidden extra sent' => [
92111
[
93-
[
94-
'idx' => '1',
95-
'type' => 'x',
96-
'meta' => [],
97-
'links' => [],
98-
],
112+
'id' => '1',
113+
'type' => 'x',
114+
'attributes' => [],
115+
'relationships' => [],
116+
'meta' => [],
117+
'links' => [],
118+
'extra' => [],
99119
],
120+
];
121+
122+
yield 'Both lid and id sent' => [
100123
[
101-
[
102-
'id' => '1',
103-
'type' => 'x',
104-
'attributes' => [],
105-
'relationships' => [],
106-
'meta' => [],
107-
'links' => [],
108-
'extra' => [],
109-
],
124+
'id' => '1',
125+
'lid' => '2',
126+
'type' => 'x',
127+
'meta' => [],
128+
'links' => [],
110129
],
111130
];
112131
}

0 commit comments

Comments
 (0)