Skip to content

Commit c28e2a1

Browse files
committed
Add check for required properties in error objects
1 parent 0d952d6 commit c28e2a1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111

1212
- Dropped support for PHP 7.4 and PHP 8.0
1313

14+
### Fixed
15+
16+
- Add check that an error object contains at least one of `id`, `links`, `status`, `code`, `title`, `detail`, `source` or `meta`.
17+
1418
### Deprecated
1519

1620
- `\Art4\Accessable::get()` will add `mixed` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors.

src/V1/Error.php

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected function parse(mixed $object): void
3333
);
3434
}
3535

36+
if (!property_exists($object, 'id') and !property_exists($object, 'links') and !property_exists($object, 'status') and !property_exists($object, 'code') and !property_exists($object, 'title') and !property_exists($object, 'detail') and !property_exists($object, 'source') and !property_exists($object, 'meta')) {
37+
throw new ValidationException('An error object MUST contain at least one of: `id`, `links`, `status`, `code`, `title`, `detail`, `source` or `meta`.');
38+
}
39+
3640
if (property_exists($object, 'id')) {
3741
if (!is_string($object->id)) {
3842
throw new ValidationException(

tests/Unit/V1/ErrorTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,28 @@ public function testCreateDetailWithoutStringThrowsException(mixed $input): void
161161

162162
$error = new Error($object, $this->manager, $this->parent);
163163
}
164+
165+
/**
166+
* An error object MAY have the following members, and MUST contain at least one of:
167+
* - id
168+
* - links
169+
* - status
170+
* - code
171+
* - title
172+
* - details
173+
* - source
174+
* - meta
175+
*/
176+
public function testCreateWithoutRequiredPropertiesThrowsException(): void
177+
{
178+
$object = new \stdClass();
179+
$object->description = 'error description';
180+
181+
$this->expectException(ValidationException::class);
182+
$this->expectExceptionMessage(
183+
'An error object MUST contain at least one of: `id`, `links`, `status`, `code`, `title`, `detail`, `source` or `meta`.'
184+
);
185+
186+
$error = new Error($object, $this->manager, $this->parent);
187+
}
164188
}

0 commit comments

Comments
 (0)