Skip to content

Commit 120030c

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 eb38b8c commit 120030c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/ValueObject/EphemeralStorage.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AsyncAws\Lambda\ValueObject;
44

5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
57
/**
68
* The size of the function's `/tmp` directory in MB. The default value is 512, but it can be any whole number between
79
* 512 and 10,240 MB.
@@ -20,7 +22,7 @@ final class EphemeralStorage
2022
*/
2123
public function __construct(array $input)
2224
{
23-
$this->size = $input['Size'] ?? null;
25+
$this->size = $input['Size'] ?? $this->throwException(new InvalidArgument('Missing required field "Size".'));
2426
}
2527

2628
/**
@@ -37,4 +39,12 @@ public function getSize(): int
3739
{
3840
return $this->size;
3941
}
42+
43+
/**
44+
* @return never
45+
*/
46+
private function throwException(\Throwable $exception)
47+
{
48+
throw $exception;
49+
}
4050
}

src/ValueObject/FileSystemConfig.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AsyncAws\Lambda\ValueObject;
44

5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
57
/**
68
* Details about the connection between a Lambda function and an Amazon EFS file system [^1].
79
*
@@ -27,8 +29,8 @@ final class FileSystemConfig
2729
*/
2830
public function __construct(array $input)
2931
{
30-
$this->arn = $input['Arn'] ?? null;
31-
$this->localMountPath = $input['LocalMountPath'] ?? null;
32+
$this->arn = $input['Arn'] ?? $this->throwException(new InvalidArgument('Missing required field "Arn".'));
33+
$this->localMountPath = $input['LocalMountPath'] ?? $this->throwException(new InvalidArgument('Missing required field "LocalMountPath".'));
3234
}
3335

3436
/**
@@ -51,4 +53,12 @@ public function getLocalMountPath(): string
5153
{
5254
return $this->localMountPath;
5355
}
56+
57+
/**
58+
* @return never
59+
*/
60+
private function throwException(\Throwable $exception)
61+
{
62+
throw $exception;
63+
}
5464
}

0 commit comments

Comments
 (0)