Skip to content

Commit

Permalink
Validate value object required fields in the constructor (#1488)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
stof committed Jul 1, 2023
1 parent 8f7b57c commit 8f986a5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
14 changes: 10 additions & 4 deletions src/ValueObject/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Content
*/
public function __construct(array $input)
{
$this->data = $input['Data'] ?? null;
$this->data = $input['Data'] ?? $this->throwException(new InvalidArgument('Missing required field "Data".'));
$this->charset = $input['Charset'] ?? null;
}

Expand Down Expand Up @@ -60,14 +60,20 @@ public function getData(): string
public function requestBody(): array
{
$payload = [];
if (null === $v = $this->data) {
throw new InvalidArgument(sprintf('Missing parameter "Data" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->data;
$payload['Data'] = $v;
if (null !== $v = $this->charset) {
$payload['Charset'] = $v;
}

return $payload;
}

/**
* @return never
*/
private function throwException(\Throwable $exception)
{
throw $exception;
}
}
14 changes: 10 additions & 4 deletions src/ValueObject/ListManagementOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ListManagementOptions
*/
public function __construct(array $input)
{
$this->contactListName = $input['ContactListName'] ?? null;
$this->contactListName = $input['ContactListName'] ?? $this->throwException(new InvalidArgument('Missing required field "ContactListName".'));
$this->topicName = $input['TopicName'] ?? null;
}

Expand Down Expand Up @@ -59,14 +59,20 @@ public function getTopicName(): ?string
public function requestBody(): array
{
$payload = [];
if (null === $v = $this->contactListName) {
throw new InvalidArgument(sprintf('Missing parameter "ContactListName" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->contactListName;
$payload['ContactListName'] = $v;
if (null !== $v = $this->topicName) {
$payload['TopicName'] = $v;
}

return $payload;
}

/**
* @return never
*/
private function throwException(\Throwable $exception)
{
throw $exception;
}
}
20 changes: 12 additions & 8 deletions src/ValueObject/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ final class Message
*/
public function __construct(array $input)
{
$this->subject = isset($input['Subject']) ? Content::create($input['Subject']) : null;
$this->body = isset($input['Body']) ? Body::create($input['Body']) : null;
$this->subject = isset($input['Subject']) ? Content::create($input['Subject']) : $this->throwException(new InvalidArgument('Missing required field "Subject".'));
$this->body = isset($input['Body']) ? Body::create($input['Body']) : $this->throwException(new InvalidArgument('Missing required field "Body".'));
}

/**
Expand Down Expand Up @@ -61,15 +61,19 @@ public function getSubject(): Content
public function requestBody(): array
{
$payload = [];
if (null === $v = $this->subject) {
throw new InvalidArgument(sprintf('Missing parameter "Subject" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->subject;
$payload['Subject'] = $v->requestBody();
if (null === $v = $this->body) {
throw new InvalidArgument(sprintf('Missing parameter "Body" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->body;
$payload['Body'] = $v->requestBody();

return $payload;
}

/**
* @return never
*/
private function throwException(\Throwable $exception)
{
throw $exception;
}
}
20 changes: 12 additions & 8 deletions src/ValueObject/MessageTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ final class MessageTag
*/
public function __construct(array $input)
{
$this->name = $input['Name'] ?? null;
$this->value = $input['Value'] ?? null;
$this->name = $input['Name'] ?? $this->throwException(new InvalidArgument('Missing required field "Name".'));
$this->value = $input['Value'] ?? $this->throwException(new InvalidArgument('Missing required field "Value".'));
}

/**
Expand Down Expand Up @@ -65,15 +65,19 @@ public function getValue(): string
public function requestBody(): array
{
$payload = [];
if (null === $v = $this->name) {
throw new InvalidArgument(sprintf('Missing parameter "Name" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->name;
$payload['Name'] = $v;
if (null === $v = $this->value) {
throw new InvalidArgument(sprintf('Missing parameter "Value" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->value;
$payload['Value'] = $v;

return $payload;
}

/**
* @return never
*/
private function throwException(\Throwable $exception)
{
throw $exception;
}
}
14 changes: 10 additions & 4 deletions src/ValueObject/RawMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class RawMessage
*/
public function __construct(array $input)
{
$this->data = $input['Data'] ?? null;
$this->data = $input['Data'] ?? $this->throwException(new InvalidArgument('Missing required field "Data".'));
}

/**
Expand All @@ -57,11 +57,17 @@ public function getData(): string
public function requestBody(): array
{
$payload = [];
if (null === $v = $this->data) {
throw new InvalidArgument(sprintf('Missing parameter "Data" for "%s". The value cannot be null.', __CLASS__));
}
$v = $this->data;
$payload['Data'] = base64_encode($v);

return $payload;
}

/**
* @return never
*/
private function throwException(\Throwable $exception)
{
throw $exception;
}
}

0 comments on commit 8f986a5

Please sign in to comment.