Skip to content

Commit 6c2dd09

Browse files
authored
Validate value object required fields in the constructor (#1488)
* Refactor the code generating object properties * Refactor the generation of array getters Optional array fields still need to consider the property as nullable even though the getter will return an empty array as AWS does not treat absent lists the same than empty lists in input. * Add validation of required parameters in value object constructors * Remove checks for missing properties for value object request bodies This is already validated in the constructor now.
1 parent 3451300 commit 6c2dd09

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/ValueObject/RepositoryTrigger.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ final class RepositoryTrigger
5252
*/
5353
public function __construct(array $input)
5454
{
55-
$this->name = $input['name'] ?? null;
56-
$this->destinationArn = $input['destinationArn'] ?? null;
55+
$this->name = $input['name'] ?? $this->throwException(new InvalidArgument('Missing required field "name".'));
56+
$this->destinationArn = $input['destinationArn'] ?? $this->throwException(new InvalidArgument('Missing required field "destinationArn".'));
5757
$this->customData = $input['customData'] ?? null;
5858
$this->branches = $input['branches'] ?? null;
59-
$this->events = $input['events'] ?? null;
59+
$this->events = $input['events'] ?? $this->throwException(new InvalidArgument('Missing required field "events".'));
6060
}
6161

6262
/**
@@ -96,7 +96,7 @@ public function getDestinationArn(): string
9696
*/
9797
public function getEvents(): array
9898
{
99-
return $this->events ?? [];
99+
return $this->events;
100100
}
101101

102102
public function getName(): string
@@ -110,13 +110,9 @@ public function getName(): string
110110
public function requestBody(): array
111111
{
112112
$payload = [];
113-
if (null === $v = $this->name) {
114-
throw new InvalidArgument(sprintf('Missing parameter "name" for "%s". The value cannot be null.', __CLASS__));
115-
}
113+
$v = $this->name;
116114
$payload['name'] = $v;
117-
if (null === $v = $this->destinationArn) {
118-
throw new InvalidArgument(sprintf('Missing parameter "destinationArn" for "%s". The value cannot be null.', __CLASS__));
119-
}
115+
$v = $this->destinationArn;
120116
$payload['destinationArn'] = $v;
121117
if (null !== $v = $this->customData) {
122118
$payload['customData'] = $v;
@@ -129,9 +125,7 @@ public function requestBody(): array
129125
$payload['branches'][$index] = $listValue;
130126
}
131127
}
132-
if (null === $v = $this->events) {
133-
throw new InvalidArgument(sprintf('Missing parameter "events" for "%s". The value cannot be null.', __CLASS__));
134-
}
128+
$v = $this->events;
135129

136130
$index = -1;
137131
$payload['events'] = [];
@@ -145,4 +139,12 @@ public function requestBody(): array
145139

146140
return $payload;
147141
}
142+
143+
/**
144+
* @return never
145+
*/
146+
private function throwException(\Throwable $exception)
147+
{
148+
throw $exception;
149+
}
148150
}

0 commit comments

Comments
 (0)