Skip to content

Commit b339f5b

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 58f37e9 commit b339f5b

11 files changed

+118
-55
lines changed

src/ValueObject/CloudWatchLogsConfig.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class CloudWatchLogsConfig
4343
*/
4444
public function __construct(array $input)
4545
{
46-
$this->status = $input['status'] ?? null;
46+
$this->status = $input['status'] ?? $this->throwException(new InvalidArgument('Missing required field "status".'));
4747
$this->groupName = $input['groupName'] ?? null;
4848
$this->streamName = $input['streamName'] ?? null;
4949
}
@@ -84,9 +84,7 @@ public function getStreamName(): ?string
8484
public function requestBody(): array
8585
{
8686
$payload = [];
87-
if (null === $v = $this->status) {
88-
throw new InvalidArgument(sprintf('Missing parameter "status" for "%s". The value cannot be null.', __CLASS__));
89-
}
87+
$v = $this->status;
9088
if (!LogsConfigStatusType::exists($v)) {
9189
throw new InvalidArgument(sprintf('Invalid parameter "status" for "%s". The value "%s" is not a valid "LogsConfigStatusType".', __CLASS__, $v));
9290
}
@@ -100,4 +98,12 @@ public function requestBody(): array
10098

10199
return $payload;
102100
}
101+
102+
/**
103+
* @return never
104+
*/
105+
private function throwException(\Throwable $exception)
106+
{
107+
throw $exception;
108+
}
103109
}

src/ValueObject/EnvironmentVariable.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ final class EnvironmentVariable
4848
*/
4949
public function __construct(array $input)
5050
{
51-
$this->name = $input['name'] ?? null;
52-
$this->value = $input['value'] ?? null;
51+
$this->name = $input['name'] ?? $this->throwException(new InvalidArgument('Missing required field "name".'));
52+
$this->value = $input['value'] ?? $this->throwException(new InvalidArgument('Missing required field "value".'));
5353
$this->type = $input['type'] ?? null;
5454
}
5555

@@ -89,13 +89,9 @@ public function getValue(): string
8989
public function requestBody(): array
9090
{
9191
$payload = [];
92-
if (null === $v = $this->name) {
93-
throw new InvalidArgument(sprintf('Missing parameter "name" for "%s". The value cannot be null.', __CLASS__));
94-
}
92+
$v = $this->name;
9593
$payload['name'] = $v;
96-
if (null === $v = $this->value) {
97-
throw new InvalidArgument(sprintf('Missing parameter "value" for "%s". The value cannot be null.', __CLASS__));
98-
}
94+
$v = $this->value;
9995
$payload['value'] = $v;
10096
if (null !== $v = $this->type) {
10197
if (!EnvironmentVariableType::exists($v)) {
@@ -106,4 +102,12 @@ public function requestBody(): array
106102

107103
return $payload;
108104
}
105+
106+
/**
107+
* @return never
108+
*/
109+
private function throwException(\Throwable $exception)
110+
{
111+
throw $exception;
112+
}
109113
}

src/ValueObject/GitSubmodulesConfig.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class GitSubmodulesConfig
2121
*/
2222
public function __construct(array $input)
2323
{
24-
$this->fetchSubmodules = $input['fetchSubmodules'] ?? null;
24+
$this->fetchSubmodules = $input['fetchSubmodules'] ?? $this->throwException(new InvalidArgument('Missing required field "fetchSubmodules".'));
2525
}
2626

2727
/**
@@ -45,11 +45,17 @@ public function getFetchSubmodules(): bool
4545
public function requestBody(): array
4646
{
4747
$payload = [];
48-
if (null === $v = $this->fetchSubmodules) {
49-
throw new InvalidArgument(sprintf('Missing parameter "fetchSubmodules" for "%s". The value cannot be null.', __CLASS__));
50-
}
48+
$v = $this->fetchSubmodules;
5149
$payload['fetchSubmodules'] = (bool) $v;
5250

5351
return $payload;
5452
}
53+
54+
/**
55+
* @return never
56+
*/
57+
private function throwException(\Throwable $exception)
58+
{
59+
throw $exception;
60+
}
5561
}

src/ValueObject/ProjectArtifacts.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ final class ProjectArtifacts
136136
*/
137137
public function __construct(array $input)
138138
{
139-
$this->type = $input['type'] ?? null;
139+
$this->type = $input['type'] ?? $this->throwException(new InvalidArgument('Missing required field "type".'));
140140
$this->location = $input['location'] ?? null;
141141
$this->path = $input['path'] ?? null;
142142
$this->namespaceType = $input['namespaceType'] ?? null;
@@ -235,9 +235,7 @@ public function getType(): string
235235
public function requestBody(): array
236236
{
237237
$payload = [];
238-
if (null === $v = $this->type) {
239-
throw new InvalidArgument(sprintf('Missing parameter "type" for "%s". The value cannot be null.', __CLASS__));
240-
}
238+
$v = $this->type;
241239
if (!ArtifactsType::exists($v)) {
242240
throw new InvalidArgument(sprintf('Invalid parameter "type" for "%s". The value "%s" is not a valid "ArtifactsType".', __CLASS__, $v));
243241
}
@@ -281,4 +279,12 @@ public function requestBody(): array
281279

282280
return $payload;
283281
}
282+
283+
/**
284+
* @return never
285+
*/
286+
private function throwException(\Throwable $exception)
287+
{
288+
throw $exception;
289+
}
284290
}

src/ValueObject/ProjectCache.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ final class ProjectCache
7171
*/
7272
public function __construct(array $input)
7373
{
74-
$this->type = $input['type'] ?? null;
74+
$this->type = $input['type'] ?? $this->throwException(new InvalidArgument('Missing required field "type".'));
7575
$this->location = $input['location'] ?? null;
7676
$this->modes = $input['modes'] ?? null;
7777
}
@@ -115,9 +115,7 @@ public function getType(): string
115115
public function requestBody(): array
116116
{
117117
$payload = [];
118-
if (null === $v = $this->type) {
119-
throw new InvalidArgument(sprintf('Missing parameter "type" for "%s". The value cannot be null.', __CLASS__));
120-
}
118+
$v = $this->type;
121119
if (!CacheType::exists($v)) {
122120
throw new InvalidArgument(sprintf('Invalid parameter "type" for "%s". The value "%s" is not a valid "CacheType".', __CLASS__, $v));
123121
}
@@ -139,4 +137,12 @@ public function requestBody(): array
139137

140138
return $payload;
141139
}
140+
141+
/**
142+
* @return never
143+
*/
144+
private function throwException(\Throwable $exception)
145+
{
146+
throw $exception;
147+
}
142148
}

src/ValueObject/ProjectEnvironment.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AsyncAws\CodeBuild\Enum\ComputeType;
66
use AsyncAws\CodeBuild\Enum\EnvironmentType;
77
use AsyncAws\CodeBuild\Enum\ImagePullCredentialsType;
8+
use AsyncAws\Core\Exception\InvalidArgument;
89

910
/**
1011
* Information about the build environment of the build project.
@@ -141,9 +142,9 @@ final class ProjectEnvironment
141142
*/
142143
public function __construct(array $input)
143144
{
144-
$this->type = $input['type'] ?? null;
145-
$this->image = $input['image'] ?? null;
146-
$this->computeType = $input['computeType'] ?? null;
145+
$this->type = $input['type'] ?? $this->throwException(new InvalidArgument('Missing required field "type".'));
146+
$this->image = $input['image'] ?? $this->throwException(new InvalidArgument('Missing required field "image".'));
147+
$this->computeType = $input['computeType'] ?? $this->throwException(new InvalidArgument('Missing required field "computeType".'));
147148
$this->environmentVariables = isset($input['environmentVariables']) ? array_map([EnvironmentVariable::class, 'create'], $input['environmentVariables']) : null;
148149
$this->privilegedMode = $input['privilegedMode'] ?? null;
149150
$this->certificate = $input['certificate'] ?? null;
@@ -219,4 +220,12 @@ public function getType(): string
219220
{
220221
return $this->type;
221222
}
223+
224+
/**
225+
* @return never
226+
*/
227+
private function throwException(\Throwable $exception)
228+
{
229+
throw $exception;
230+
}
222231
}

src/ValueObject/ProjectSource.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ final class ProjectSource
140140
*/
141141
public function __construct(array $input)
142142
{
143-
$this->type = $input['type'] ?? null;
143+
$this->type = $input['type'] ?? $this->throwException(new InvalidArgument('Missing required field "type".'));
144144
$this->location = $input['location'] ?? null;
145145
$this->gitCloneDepth = $input['gitCloneDepth'] ?? null;
146146
$this->gitSubmodulesConfig = isset($input['gitSubmodulesConfig']) ? GitSubmodulesConfig::create($input['gitSubmodulesConfig']) : null;
@@ -230,9 +230,7 @@ public function getType(): string
230230
public function requestBody(): array
231231
{
232232
$payload = [];
233-
if (null === $v = $this->type) {
234-
throw new InvalidArgument(sprintf('Missing parameter "type" for "%s". The value cannot be null.', __CLASS__));
235-
}
233+
$v = $this->type;
236234
if (!SourceType::exists($v)) {
237235
throw new InvalidArgument(sprintf('Invalid parameter "type" for "%s". The value "%s" is not a valid "SourceType".', __CLASS__, $v));
238236
}
@@ -267,4 +265,12 @@ public function requestBody(): array
267265

268266
return $payload;
269267
}
268+
269+
/**
270+
* @return never
271+
*/
272+
private function throwException(\Throwable $exception)
273+
{
274+
throw $exception;
275+
}
270276
}

src/ValueObject/ProjectSourceVersion.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ final class ProjectSourceVersion
4242
*/
4343
public function __construct(array $input)
4444
{
45-
$this->sourceIdentifier = $input['sourceIdentifier'] ?? null;
46-
$this->sourceVersion = $input['sourceVersion'] ?? null;
45+
$this->sourceIdentifier = $input['sourceIdentifier'] ?? $this->throwException(new InvalidArgument('Missing required field "sourceIdentifier".'));
46+
$this->sourceVersion = $input['sourceVersion'] ?? $this->throwException(new InvalidArgument('Missing required field "sourceVersion".'));
4747
}
4848

4949
/**
@@ -73,15 +73,19 @@ public function getSourceVersion(): string
7373
public function requestBody(): array
7474
{
7575
$payload = [];
76-
if (null === $v = $this->sourceIdentifier) {
77-
throw new InvalidArgument(sprintf('Missing parameter "sourceIdentifier" for "%s". The value cannot be null.', __CLASS__));
78-
}
76+
$v = $this->sourceIdentifier;
7977
$payload['sourceIdentifier'] = $v;
80-
if (null === $v = $this->sourceVersion) {
81-
throw new InvalidArgument(sprintf('Missing parameter "sourceVersion" for "%s". The value cannot be null.', __CLASS__));
82-
}
78+
$v = $this->sourceVersion;
8379
$payload['sourceVersion'] = $v;
8480

8581
return $payload;
8682
}
83+
84+
/**
85+
* @return never
86+
*/
87+
private function throwException(\Throwable $exception)
88+
{
89+
throw $exception;
90+
}
8791
}

src/ValueObject/RegistryCredential.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ final class RegistryCredential
3838
*/
3939
public function __construct(array $input)
4040
{
41-
$this->credential = $input['credential'] ?? null;
42-
$this->credentialProvider = $input['credentialProvider'] ?? null;
41+
$this->credential = $input['credential'] ?? $this->throwException(new InvalidArgument('Missing required field "credential".'));
42+
$this->credentialProvider = $input['credentialProvider'] ?? $this->throwException(new InvalidArgument('Missing required field "credentialProvider".'));
4343
}
4444

4545
/**
@@ -72,18 +72,22 @@ public function getCredentialProvider(): string
7272
public function requestBody(): array
7373
{
7474
$payload = [];
75-
if (null === $v = $this->credential) {
76-
throw new InvalidArgument(sprintf('Missing parameter "credential" for "%s". The value cannot be null.', __CLASS__));
77-
}
75+
$v = $this->credential;
7876
$payload['credential'] = $v;
79-
if (null === $v = $this->credentialProvider) {
80-
throw new InvalidArgument(sprintf('Missing parameter "credentialProvider" for "%s". The value cannot be null.', __CLASS__));
81-
}
77+
$v = $this->credentialProvider;
8278
if (!CredentialProviderType::exists($v)) {
8379
throw new InvalidArgument(sprintf('Invalid parameter "credentialProvider" for "%s". The value "%s" is not a valid "CredentialProviderType".', __CLASS__, $v));
8480
}
8581
$payload['credentialProvider'] = $v;
8682

8783
return $payload;
8884
}
85+
86+
/**
87+
* @return never
88+
*/
89+
private function throwException(\Throwable $exception)
90+
{
91+
throw $exception;
92+
}
8993
}

src/ValueObject/S3LogsConfig.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class S3LogsConfig
4242
*/
4343
public function __construct(array $input)
4444
{
45-
$this->status = $input['status'] ?? null;
45+
$this->status = $input['status'] ?? $this->throwException(new InvalidArgument('Missing required field "status".'));
4646
$this->location = $input['location'] ?? null;
4747
$this->encryptionDisabled = $input['encryptionDisabled'] ?? null;
4848
$this->bucketOwnerAccess = $input['bucketOwnerAccess'] ?? null;
@@ -93,9 +93,7 @@ public function getStatus(): string
9393
public function requestBody(): array
9494
{
9595
$payload = [];
96-
if (null === $v = $this->status) {
97-
throw new InvalidArgument(sprintf('Missing parameter "status" for "%s". The value cannot be null.', __CLASS__));
98-
}
96+
$v = $this->status;
9997
if (!LogsConfigStatusType::exists($v)) {
10098
throw new InvalidArgument(sprintf('Invalid parameter "status" for "%s". The value "%s" is not a valid "LogsConfigStatusType".', __CLASS__, $v));
10199
}
@@ -115,4 +113,12 @@ public function requestBody(): array
115113

116114
return $payload;
117115
}
116+
117+
/**
118+
* @return never
119+
*/
120+
private function throwException(\Throwable $exception)
121+
{
122+
throw $exception;
123+
}
118124
}

0 commit comments

Comments
 (0)