Skip to content

Commit a4db5e3

Browse files
committed
Reword validation exception messages
This aligns the messages with the wording in the specification.
1 parent 2e2ab12 commit a4db5e3

16 files changed

+61
-61
lines changed

src/Parsers/CollectionParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(ItemParser $itemParser)
3131
public function parse($data): Collection
3232
{
3333
if (!is_array($data)) {
34-
throw new ValidationException(sprintf('ResourceCollection has to be an array, "%s" given.', gettype($data)));
34+
throw new ValidationException(sprintf('ResourceCollection MUST be an array, "%s" given.', gettype($data)));
3535
}
3636

3737
return Collection::make($data)

src/Parsers/DocumentParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function parse(string $json): DocumentInterface
7979
$data = $this->decodeJson($json);
8080

8181
if (!is_object($data)) {
82-
throw new ValidationException(sprintf('Document has to be an object, "%s" given.', gettype($data)));
82+
throw new ValidationException(sprintf('Document MUST be an object, "%s" given.', gettype($data)));
8383
}
8484
if (!property_exists($data, 'data') && !property_exists($data, 'errors') && !property_exists($data, 'meta')) {
8585
throw new ValidationException('Document MUST contain at least one of the following properties: `data`, `errors`, `meta`.');
@@ -91,10 +91,10 @@ public function parse(string $json): DocumentInterface
9191
throw new ValidationException('If Document does not contain a `data` property, the `included` property MUST NOT be present either.');
9292
}
9393
if (property_exists($data, 'data') && !is_object($data->data) && !is_array($data->data) && $data->data !== null) {
94-
throw new ValidationException(sprintf('Document property "data" has to be null, an array or an object, "%s" given.', gettype($data->data)));
94+
throw new ValidationException(sprintf('Document property "data" MUST be null, an array or an object, "%s" given.', gettype($data->data)));
9595
}
9696
if (property_exists($data, 'included') && !is_array($data->included)) {
97-
throw new ValidationException(sprintf('Document property "included" has to be an array, "%s" given.', gettype($data->included)));
97+
throw new ValidationException(sprintf('Document property "included" MUST be an array, "%s" given.', gettype($data->included)));
9898
}
9999

100100
$document = $this->getDocument($data);

src/Parsers/ErrorCollectionParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(ErrorParser $errorParser)
3131
public function parse($data): ErrorCollection
3232
{
3333
if (!is_array($data)) {
34-
throw new ValidationException(sprintf('ErrorCollection has to be in an array, "%s" given.', gettype($data)));
34+
throw new ValidationException(sprintf('ErrorCollection MUST be an array, "%s" given.', gettype($data)));
3535
}
3636
if (count($data) === 0) {
3737
throw new ValidationException('ErrorCollection cannot be empty and MUST have at least one Error object.');

src/Parsers/ErrorParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public function __construct(LinksParser $linksParser, MetaParser $metaParser)
3939
public function parse($data): Error
4040
{
4141
if (!is_object($data)) {
42-
throw new ValidationException(sprintf('Error has to be an object, "%s" given.', gettype($data)));
42+
throw new ValidationException(sprintf('Error MUST be an object, "%s" given.', gettype($data)));
4343
}
4444
if (property_exists($data, 'id') && !is_string($data->id)) {
45-
throw new ValidationException(sprintf('Error property "id" has to be a string, "%s" given.', gettype($data->id)));
45+
throw new ValidationException(sprintf('Error property "id" MUST be a string, "%s" given.', gettype($data->id)));
4646
}
4747
if (property_exists($data, 'status') && !is_string($data->status)) {
48-
throw new ValidationException(sprintf('Error property "status" has to be a string, "%s" given.', gettype($data->status)));
48+
throw new ValidationException(sprintf('Error property "status" MUST be a string, "%s" given.', gettype($data->status)));
4949
}
5050
if (property_exists($data, 'code') && !is_string($data->code)) {
51-
throw new ValidationException(sprintf('Error property "code" has to be a string, "%s" given.', gettype($data->code)));
51+
throw new ValidationException(sprintf('Error property "code" MUST be a string, "%s" given.', gettype($data->code)));
5252
}
5353
if (property_exists($data, 'title') && !is_string($data->title)) {
54-
throw new ValidationException(sprintf('Error property "title" has to be a string, "%s" given.', gettype($data->title)));
54+
throw new ValidationException(sprintf('Error property "title" MUST be a string, "%s" given.', gettype($data->title)));
5555
}
5656
if (property_exists($data, 'detail') && !is_string($data->detail)) {
57-
throw new ValidationException(sprintf('Error property "detail" has to be a string, "%s" given.', gettype($data->detail)));
57+
throw new ValidationException(sprintf('Error property "detail" MUST be a string, "%s" given.', gettype($data->detail)));
5858
}
5959

6060
return new Error(
@@ -77,13 +77,13 @@ public function parse($data): Error
7777
private function buildErrorSource($data): ErrorSource
7878
{
7979
if (!is_object($data)) {
80-
throw new ValidationException(sprintf('ErrorSource has to be an object, "%s" given.', gettype($data)));
80+
throw new ValidationException(sprintf('ErrorSource MUST be an object, "%s" given.', gettype($data)));
8181
}
8282
if (property_exists($data, 'pointer') && !is_string($data->pointer)) {
83-
throw new ValidationException(sprintf('ErrorSource property "pointer" has to be a string, "%s" given.', gettype($data->pointer)));
83+
throw new ValidationException(sprintf('ErrorSource property "pointer" MUST be a string, "%s" given.', gettype($data->pointer)));
8484
}
8585
if (property_exists($data, 'parameter') && !is_string($data->parameter)) {
86-
throw new ValidationException(sprintf('ErrorSource property "parameter" has to be a string, "%s" given.', gettype($data->parameter)));
86+
throw new ValidationException(sprintf('ErrorSource property "parameter" MUST be a string, "%s" given.', gettype($data->parameter)));
8787
}
8888

8989
return new ErrorSource(

src/Parsers/ItemParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(TypeMapperInterface $typeMapper, LinksParser $linksP
4949
public function parse($data): ItemInterface
5050
{
5151
if (!is_object($data)) {
52-
throw new ValidationException(sprintf('Resource has to be an object, "%s" given.', gettype($data)));
52+
throw new ValidationException(sprintf('Resource MUST be an object, "%s" given.', gettype($data)));
5353
}
5454
if (!property_exists($data, 'type')) {
5555
throw new ValidationException('Resource object MUST contain a type.');
@@ -58,14 +58,14 @@ public function parse($data): ItemInterface
5858
throw new ValidationException('Resource object MUST contain an id.');
5959
}
6060
if (!is_string($data->type)) {
61-
throw new ValidationException(sprintf('Resource property "type" has to be a string, "%s" given.', gettype($data->type)));
61+
throw new ValidationException(sprintf('Resource property "type" MUST be a string, "%s" given.', gettype($data->type)));
6262
}
6363
if (!is_string($data->id) && !is_numeric($data->id)) {
64-
throw new ValidationException(sprintf('Resource property "id" has to be a string, "%s" given.', gettype($data->id)));
64+
throw new ValidationException(sprintf('Resource property "id" MUST be a string, "%s" given.', gettype($data->id)));
6565
}
6666
if (property_exists($data, 'attributes')) {
6767
if (!is_object($data->attributes)) {
68-
throw new ValidationException(sprintf('Resource property "attributes" has to be an object, "%s" given.', gettype($data->attributes)));
68+
throw new ValidationException(sprintf('Resource property "attributes" MUST be an object, "%s" given.', gettype($data->attributes)));
6969
}
7070
if (property_exists($data->attributes, 'type') || property_exists($data->attributes, 'id') || property_exists($data->attributes, 'relationships') || property_exists($data->attributes, 'links')) {
7171
throw new ValidationException('These properties are not allowed in attributes: `type`, `id`, `relationships`, `links`.');
@@ -118,7 +118,7 @@ private function getItemInstance(string $type): ItemInterface
118118
private function setRelations(ItemInterface $item, $data): void
119119
{
120120
if (!is_object($data)) {
121-
throw new ValidationException(sprintf('Relationships has to be an object, "%s" given.', gettype($data)));
121+
throw new ValidationException(sprintf('Resource property "relationships" MUST be an object, "%s" given.', gettype($data)));
122122
}
123123
if (property_exists($data, 'type') || property_exists($data, 'id')) {
124124
throw new ValidationException('These properties are not allowed in relationships: `type`, `id`.');
@@ -129,7 +129,7 @@ private function setRelations(ItemInterface $item, $data): void
129129
throw new ValidationException(sprintf('Relationship "%s" cannot be set because it already exists in Resource object.', $name));
130130
}
131131
if (!is_object($relationship)) {
132-
throw new ValidationException(sprintf('Relationship has to be an object, "%s" given.', gettype($relationship)));
132+
throw new ValidationException(sprintf('Relationship MUST be an object, "%s" given.', gettype($relationship)));
133133
}
134134
if (!property_exists($relationship, 'links') && !property_exists($relationship, 'data') && !property_exists($relationship, 'meta')) {
135135
throw new ValidationException('Relationship object MUST contain at least one of the following properties: `links`, `data`, `meta`.');
@@ -173,7 +173,7 @@ function ($identifier) {
173173
}
174174

175175
if (!is_object($data)) {
176-
throw new ValidationException(sprintf('ResourceIdentifier has to be an object, "%s" given.', gettype($data)));
176+
throw new ValidationException(sprintf('ResourceIdentifier MUST be an object, "%s" given.', gettype($data)));
177177
}
178178
if (!property_exists($data, 'type')) {
179179
throw new ValidationException('ResourceIdentifier object MUST contain a type.');
@@ -182,10 +182,10 @@ function ($identifier) {
182182
throw new ValidationException('ResourceIdentifier object MUST contain an id.');
183183
}
184184
if (!is_string($data->type)) {
185-
throw new ValidationException(sprintf('ResourceIdentifier property "type" has to be a string, "%s" given.', gettype($data->type)));
185+
throw new ValidationException(sprintf('ResourceIdentifier property "type" MUST be a string, "%s" given.', gettype($data->type)));
186186
}
187187
if (!is_string($data->id) && !is_numeric($data->id)) {
188-
throw new ValidationException(sprintf('ResourceIdentifier property "id" has to be a string, "%s" given.', gettype($data->id)));
188+
throw new ValidationException(sprintf('ResourceIdentifier property "id" MUST be a string, "%s" given.', gettype($data->id)));
189189
}
190190

191191
return $this->getItemInstance($data->type)->setId($data->id);

src/Parsers/JsonapiParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function __construct(MetaParser $metaParser)
3131
public function parse($data): Jsonapi
3232
{
3333
if (!is_object($data)) {
34-
throw new ValidationException(sprintf('Jsonapi has to be an object, "%s" given.', gettype($data)));
34+
throw new ValidationException(sprintf('Jsonapi MUST be an object, "%s" given.', gettype($data)));
3535
}
3636
if (property_exists($data, 'version') && !is_string($data->version)) {
37-
throw new ValidationException(sprintf('Jsonapi property "version" has to be a string, "%s" given.', gettype($data->version)));
37+
throw new ValidationException(sprintf('Jsonapi property "version" MUST be a string, "%s" given.', gettype($data->version)));
3838
}
3939

4040
return new Jsonapi(

src/Parsers/LinksParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(MetaParser $metaParser)
4747
public function parse($data, string $source): Links
4848
{
4949
if (!is_object($data)) {
50-
throw new ValidationException(sprintf('Links has to be an object, "%s" given.', gettype($data)));
50+
throw new ValidationException(sprintf('Links MUST be an object, "%s" given.', gettype($data)));
5151
}
5252
if ($source === self::SOURCE_ERROR && !property_exists($data, 'about')) {
5353
throw new ValidationException('Error links object MUST contain at least one of the following properties: `about`.');
@@ -76,7 +76,7 @@ function ($link, $name) {
7676
private function buildLink($data, string $name): ? Link
7777
{
7878
if (in_array($name, self::LINKS_THAT_MAY_NOT_BE_NULL_WHEN_PRESENT, true) && !is_string($data) && !is_object($data)) {
79-
throw new ValidationException(sprintf('Link "%s" has to be an object or string, "%s" given.', $name, gettype($data)));
79+
throw new ValidationException(sprintf('Link "%s" MUST be an object or string, "%s" given.', $name, gettype($data)));
8080
}
8181

8282
if ($data === null) {
@@ -88,10 +88,10 @@ private function buildLink($data, string $name): ? Link
8888
}
8989

9090
if (!is_object($data)) {
91-
throw new ValidationException(sprintf('Link "%s" has to be an object, string or null, "%s" given.', $name, gettype($data)));
91+
throw new ValidationException(sprintf('Link "%s" MUST be an object, string or null, "%s" given.', $name, gettype($data)));
9292
}
9393
if (!property_exists($data, 'href')) {
94-
throw new ValidationException(sprintf('Link "%s" must have a "href" attribute.', $name));
94+
throw new ValidationException(sprintf('Link "%s" MUST have a "href" attribute.', $name));
9595
}
9696

9797
return new Link($data->href, property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null);

src/Parsers/MetaParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MetaParser
1818
public function parse($data): Meta
1919
{
2020
if (!is_object($data)) {
21-
throw new ValidationException(sprintf('Meta has to be an object, "%s" given.', gettype($data)));
21+
throw new ValidationException(sprintf('Meta MUST be an object, "%s" given.', gettype($data)));
2222
}
2323

2424
return new Meta((array) $data);

tests/Parsers/CollectionParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function it_throws_when_data_is_not_an_array($invalidData)
4242
$parser = new CollectionParser($this->createMock(ItemParser::class));
4343

4444
$this->expectException(ValidationException::class);
45-
$this->expectExceptionMessage(sprintf('ResourceCollection has to be an array, "%s" given.', gettype($invalidData)));
45+
$this->expectExceptionMessage(sprintf('ResourceCollection MUST be an array, "%s" given.', gettype($invalidData)));
4646

4747
$parser->parse($invalidData);
4848
}

tests/Parsers/DocumentParserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function it_throws_when_json_is_not_a_jsonapi_document(string $invalidJso
7070
$parser = $this->getDocumentParser();
7171

7272
$this->expectException(ValidationException::class);
73-
$this->expectExceptionMessage(sprintf('Document has to be an object, "%s" given.', gettype(json_decode($invalidJson, false))));
73+
$this->expectExceptionMessage(sprintf('Document MUST be an object, "%s" given.', gettype(json_decode($invalidJson, false))));
7474

7575
$parser->parse($invalidJson);
7676
}
@@ -137,7 +137,7 @@ public function it_throws_when_data_is_not_an_array_object_or_null($invalidData)
137137
$parser = $this->getDocumentParser();
138138

139139
$this->expectException(ValidationException::class);
140-
$this->expectExceptionMessage(sprintf('Document property "data" has to be null, an array or an object, "%s" given.', gettype(json_decode($invalidData, false)->data)));
140+
$this->expectExceptionMessage(sprintf('Document property "data" MUST be null, an array or an object, "%s" given.', gettype(json_decode($invalidData, false)->data)));
141141

142142
$parser->parse($invalidData);
143143
}
@@ -163,7 +163,7 @@ public function it_throws_when_included_is_not_an_array($invalidIncluded)
163163
$parser = $this->getDocumentParser();
164164

165165
$this->expectException(ValidationException::class);
166-
$this->expectExceptionMessage(sprintf('Document property "included" has to be an array, "%s" given.', gettype(json_decode($invalidIncluded, false)->included)));
166+
$this->expectExceptionMessage(sprintf('Document property "included" MUST be an array, "%s" given.', gettype(json_decode($invalidIncluded, false)->included)));
167167

168168
$parser->parse($invalidIncluded);
169169
}

tests/Parsers/ErrorCollectionParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function it_throws_when_data_is_not_an_array($invalidData)
4242
$parser = new ErrorCollectionParser($this->createMock(ErrorParser::class));
4343

4444
$this->expectException(ValidationException::class);
45-
$this->expectExceptionMessage(sprintf('ErrorCollection has to be in an array, "%s" given.', gettype($invalidData)));
45+
$this->expectExceptionMessage(sprintf('ErrorCollection MUST be an array, "%s" given.', gettype($invalidData)));
4646

4747
$parser->parse($invalidData);
4848
}

0 commit comments

Comments
 (0)