Skip to content

Commit ec99987

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 b3c86e7 commit ec99987

25 files changed

+299
-178
lines changed

src/ValueObject/AttributeDefinition.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ final class AttributeDefinition
3232
*/
3333
public function __construct(array $input)
3434
{
35-
$this->attributeName = $input['AttributeName'] ?? null;
36-
$this->attributeType = $input['AttributeType'] ?? null;
35+
$this->attributeName = $input['AttributeName'] ?? $this->throwException(new InvalidArgument('Missing required field "AttributeName".'));
36+
$this->attributeType = $input['AttributeType'] ?? $this->throwException(new InvalidArgument('Missing required field "AttributeType".'));
3737
}
3838

3939
/**
@@ -66,18 +66,22 @@ public function getAttributeType(): string
6666
public function requestBody(): array
6767
{
6868
$payload = [];
69-
if (null === $v = $this->attributeName) {
70-
throw new InvalidArgument(sprintf('Missing parameter "AttributeName" for "%s". The value cannot be null.', __CLASS__));
71-
}
69+
$v = $this->attributeName;
7270
$payload['AttributeName'] = $v;
73-
if (null === $v = $this->attributeType) {
74-
throw new InvalidArgument(sprintf('Missing parameter "AttributeType" for "%s". The value cannot be null.', __CLASS__));
75-
}
71+
$v = $this->attributeType;
7672
if (!ScalarAttributeType::exists($v)) {
7773
throw new InvalidArgument(sprintf('Invalid parameter "AttributeType" for "%s". The value "%s" is not a valid "ScalarAttributeType".', __CLASS__, $v));
7874
}
7975
$payload['AttributeType'] = $v;
8076

8177
return $payload;
8278
}
79+
80+
/**
81+
* @return never
82+
*/
83+
private function throwException(\Throwable $exception)
84+
{
85+
throw $exception;
86+
}
8387
}

src/ValueObject/Condition.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ final class Condition
146146
public function __construct(array $input)
147147
{
148148
$this->attributeValueList = isset($input['AttributeValueList']) ? array_map([AttributeValue::class, 'create'], $input['AttributeValueList']) : null;
149-
$this->comparisonOperator = $input['ComparisonOperator'] ?? null;
149+
$this->comparisonOperator = $input['ComparisonOperator'] ?? $this->throwException(new InvalidArgument('Missing required field "ComparisonOperator".'));
150150
}
151151

152152
/**
@@ -190,14 +190,20 @@ public function requestBody(): array
190190
$payload['AttributeValueList'][$index] = $listValue->requestBody();
191191
}
192192
}
193-
if (null === $v = $this->comparisonOperator) {
194-
throw new InvalidArgument(sprintf('Missing parameter "ComparisonOperator" for "%s". The value cannot be null.', __CLASS__));
195-
}
193+
$v = $this->comparisonOperator;
196194
if (!ComparisonOperator::exists($v)) {
197195
throw new InvalidArgument(sprintf('Invalid parameter "ComparisonOperator" for "%s". The value "%s" is not a valid "ComparisonOperator".', __CLASS__, $v));
198196
}
199197
$payload['ComparisonOperator'] = $v;
200198

201199
return $payload;
202200
}
201+
202+
/**
203+
* @return never
204+
*/
205+
private function throwException(\Throwable $exception)
206+
{
207+
throw $exception;
208+
}
203209
}

src/ValueObject/ConditionCheck.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ final class ConditionCheck
6363
*/
6464
public function __construct(array $input)
6565
{
66-
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : null;
67-
$this->tableName = $input['TableName'] ?? null;
68-
$this->conditionExpression = $input['ConditionExpression'] ?? null;
66+
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : $this->throwException(new InvalidArgument('Missing required field "Key".'));
67+
$this->tableName = $input['TableName'] ?? $this->throwException(new InvalidArgument('Missing required field "TableName".'));
68+
$this->conditionExpression = $input['ConditionExpression'] ?? $this->throwException(new InvalidArgument('Missing required field "ConditionExpression".'));
6969
$this->expressionAttributeNames = $input['ExpressionAttributeNames'] ?? null;
7070
$this->expressionAttributeValues = isset($input['ExpressionAttributeValues']) ? array_map([AttributeValue::class, 'create'], $input['ExpressionAttributeValues']) : null;
7171
$this->returnValuesOnConditionCheckFailure = $input['ReturnValuesOnConditionCheckFailure'] ?? null;
@@ -112,7 +112,7 @@ public function getExpressionAttributeValues(): array
112112
*/
113113
public function getKey(): array
114114
{
115-
return $this->key ?? [];
115+
return $this->key;
116116
}
117117

118118
/**
@@ -134,9 +134,7 @@ public function getTableName(): string
134134
public function requestBody(): array
135135
{
136136
$payload = [];
137-
if (null === $v = $this->key) {
138-
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
139-
}
137+
$v = $this->key;
140138

141139
if (empty($v)) {
142140
$payload['Key'] = new \stdClass();
@@ -146,13 +144,9 @@ public function requestBody(): array
146144
$payload['Key'][$name] = $mv->requestBody();
147145
}
148146
}
149-
if (null === $v = $this->tableName) {
150-
throw new InvalidArgument(sprintf('Missing parameter "TableName" for "%s". The value cannot be null.', __CLASS__));
151-
}
147+
$v = $this->tableName;
152148
$payload['TableName'] = $v;
153-
if (null === $v = $this->conditionExpression) {
154-
throw new InvalidArgument(sprintf('Missing parameter "ConditionExpression" for "%s". The value cannot be null.', __CLASS__));
155-
}
149+
$v = $this->conditionExpression;
156150
$payload['ConditionExpression'] = $v;
157151
if (null !== $v = $this->expressionAttributeNames) {
158152
if (empty($v)) {
@@ -183,4 +177,12 @@ public function requestBody(): array
183177

184178
return $payload;
185179
}
180+
181+
/**
182+
* @return never
183+
*/
184+
private function throwException(\Throwable $exception)
185+
{
186+
throw $exception;
187+
}
186188
}

src/ValueObject/CreateGlobalSecondaryIndexAction.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ final class CreateGlobalSecondaryIndexAction
4545
*/
4646
public function __construct(array $input)
4747
{
48-
$this->indexName = $input['IndexName'] ?? null;
49-
$this->keySchema = isset($input['KeySchema']) ? array_map([KeySchemaElement::class, 'create'], $input['KeySchema']) : null;
50-
$this->projection = isset($input['Projection']) ? Projection::create($input['Projection']) : null;
48+
$this->indexName = $input['IndexName'] ?? $this->throwException(new InvalidArgument('Missing required field "IndexName".'));
49+
$this->keySchema = isset($input['KeySchema']) ? array_map([KeySchemaElement::class, 'create'], $input['KeySchema']) : $this->throwException(new InvalidArgument('Missing required field "KeySchema".'));
50+
$this->projection = isset($input['Projection']) ? Projection::create($input['Projection']) : $this->throwException(new InvalidArgument('Missing required field "Projection".'));
5151
$this->provisionedThroughput = isset($input['ProvisionedThroughput']) ? ProvisionedThroughput::create($input['ProvisionedThroughput']) : null;
5252
}
5353

@@ -74,7 +74,7 @@ public function getIndexName(): string
7474
*/
7575
public function getKeySchema(): array
7676
{
77-
return $this->keySchema ?? [];
77+
return $this->keySchema;
7878
}
7979

8080
public function getProjection(): Projection
@@ -93,13 +93,9 @@ public function getProvisionedThroughput(): ?ProvisionedThroughput
9393
public function requestBody(): array
9494
{
9595
$payload = [];
96-
if (null === $v = $this->indexName) {
97-
throw new InvalidArgument(sprintf('Missing parameter "IndexName" for "%s". The value cannot be null.', __CLASS__));
98-
}
96+
$v = $this->indexName;
9997
$payload['IndexName'] = $v;
100-
if (null === $v = $this->keySchema) {
101-
throw new InvalidArgument(sprintf('Missing parameter "KeySchema" for "%s". The value cannot be null.', __CLASS__));
102-
}
98+
$v = $this->keySchema;
10399

104100
$index = -1;
105101
$payload['KeySchema'] = [];
@@ -108,14 +104,20 @@ public function requestBody(): array
108104
$payload['KeySchema'][$index] = $listValue->requestBody();
109105
}
110106

111-
if (null === $v = $this->projection) {
112-
throw new InvalidArgument(sprintf('Missing parameter "Projection" for "%s". The value cannot be null.', __CLASS__));
113-
}
107+
$v = $this->projection;
114108
$payload['Projection'] = $v->requestBody();
115109
if (null !== $v = $this->provisionedThroughput) {
116110
$payload['ProvisionedThroughput'] = $v->requestBody();
117111
}
118112

119113
return $payload;
120114
}
115+
116+
/**
117+
* @return never
118+
*/
119+
private function throwException(\Throwable $exception)
120+
{
121+
throw $exception;
122+
}
121123
}

src/ValueObject/CreateReplicationGroupMemberAction.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class CreateReplicationGroupMemberAction
4848
*/
4949
public function __construct(array $input)
5050
{
51-
$this->regionName = $input['RegionName'] ?? null;
51+
$this->regionName = $input['RegionName'] ?? $this->throwException(new InvalidArgument('Missing required field "RegionName".'));
5252
$this->kmsMasterKeyId = $input['KMSMasterKeyId'] ?? null;
5353
$this->provisionedThroughputOverride = isset($input['ProvisionedThroughputOverride']) ? ProvisionedThroughputOverride::create($input['ProvisionedThroughputOverride']) : null;
5454
$this->globalSecondaryIndexes = isset($input['GlobalSecondaryIndexes']) ? array_map([ReplicaGlobalSecondaryIndex::class, 'create'], $input['GlobalSecondaryIndexes']) : null;
@@ -106,9 +106,7 @@ public function getTableClassOverride(): ?string
106106
public function requestBody(): array
107107
{
108108
$payload = [];
109-
if (null === $v = $this->regionName) {
110-
throw new InvalidArgument(sprintf('Missing parameter "RegionName" for "%s". The value cannot be null.', __CLASS__));
111-
}
109+
$v = $this->regionName;
112110
$payload['RegionName'] = $v;
113111
if (null !== $v = $this->kmsMasterKeyId) {
114112
$payload['KMSMasterKeyId'] = $v;
@@ -133,4 +131,12 @@ public function requestBody(): array
133131

134132
return $payload;
135133
}
134+
135+
/**
136+
* @return never
137+
*/
138+
private function throwException(\Throwable $exception)
139+
{
140+
throw $exception;
141+
}
136142
}

src/ValueObject/Delete.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ final class Delete
5353
*/
5454
public function __construct(array $input)
5555
{
56-
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : null;
57-
$this->tableName = $input['TableName'] ?? null;
56+
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : $this->throwException(new InvalidArgument('Missing required field "Key".'));
57+
$this->tableName = $input['TableName'] ?? $this->throwException(new InvalidArgument('Missing required field "TableName".'));
5858
$this->conditionExpression = $input['ConditionExpression'] ?? null;
5959
$this->expressionAttributeNames = $input['ExpressionAttributeNames'] ?? null;
6060
$this->expressionAttributeValues = isset($input['ExpressionAttributeValues']) ? array_map([AttributeValue::class, 'create'], $input['ExpressionAttributeValues']) : null;
@@ -102,7 +102,7 @@ public function getExpressionAttributeValues(): array
102102
*/
103103
public function getKey(): array
104104
{
105-
return $this->key ?? [];
105+
return $this->key;
106106
}
107107

108108
/**
@@ -124,9 +124,7 @@ public function getTableName(): string
124124
public function requestBody(): array
125125
{
126126
$payload = [];
127-
if (null === $v = $this->key) {
128-
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
129-
}
127+
$v = $this->key;
130128

131129
if (empty($v)) {
132130
$payload['Key'] = new \stdClass();
@@ -136,9 +134,7 @@ public function requestBody(): array
136134
$payload['Key'][$name] = $mv->requestBody();
137135
}
138136
}
139-
if (null === $v = $this->tableName) {
140-
throw new InvalidArgument(sprintf('Missing parameter "TableName" for "%s". The value cannot be null.', __CLASS__));
141-
}
137+
$v = $this->tableName;
142138
$payload['TableName'] = $v;
143139
if (null !== $v = $this->conditionExpression) {
144140
$payload['ConditionExpression'] = $v;
@@ -172,4 +168,12 @@ public function requestBody(): array
172168

173169
return $payload;
174170
}
171+
172+
/**
173+
* @return never
174+
*/
175+
private function throwException(\Throwable $exception)
176+
{
177+
throw $exception;
178+
}
175179
}

src/ValueObject/DeleteGlobalSecondaryIndexAction.php

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

2727
/**
@@ -45,11 +45,17 @@ public function getIndexName(): string
4545
public function requestBody(): array
4646
{
4747
$payload = [];
48-
if (null === $v = $this->indexName) {
49-
throw new InvalidArgument(sprintf('Missing parameter "IndexName" for "%s". The value cannot be null.', __CLASS__));
50-
}
48+
$v = $this->indexName;
5149
$payload['IndexName'] = $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/DeleteReplicationGroupMemberAction.php

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

2727
/**
@@ -45,11 +45,17 @@ public function getRegionName(): string
4545
public function requestBody(): array
4646
{
4747
$payload = [];
48-
if (null === $v = $this->regionName) {
49-
throw new InvalidArgument(sprintf('Missing parameter "RegionName" for "%s". The value cannot be null.', __CLASS__));
50-
}
48+
$v = $this->regionName;
5149
$payload['RegionName'] = $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/DeleteRequest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class DeleteRequest
2222
*/
2323
public function __construct(array $input)
2424
{
25-
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : null;
25+
$this->key = isset($input['Key']) ? array_map([AttributeValue::class, 'create'], $input['Key']) : $this->throwException(new InvalidArgument('Missing required field "Key".'));
2626
}
2727

2828
/**
@@ -40,7 +40,7 @@ public static function create($input): self
4040
*/
4141
public function getKey(): array
4242
{
43-
return $this->key ?? [];
43+
return $this->key;
4444
}
4545

4646
/**
@@ -49,9 +49,7 @@ public function getKey(): array
4949
public function requestBody(): array
5050
{
5151
$payload = [];
52-
if (null === $v = $this->key) {
53-
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
54-
}
52+
$v = $this->key;
5553

5654
if (empty($v)) {
5755
$payload['Key'] = new \stdClass();
@@ -64,4 +62,12 @@ public function requestBody(): array
6462

6563
return $payload;
6664
}
65+
66+
/**
67+
* @return never
68+
*/
69+
private function throwException(\Throwable $exception)
70+
{
71+
throw $exception;
72+
}
6773
}

0 commit comments

Comments
 (0)