Skip to content

Commit

Permalink
Merge pull request aws#742 from jeskew/feature/validate-min-and-max
Browse files Browse the repository at this point in the history
Check model-defined min and max values during validation
  • Loading branch information
jeskew committed Sep 3, 2015
2 parents 0dac064 + b78612c commit 81cdc53
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
50 changes: 46 additions & 4 deletions src/Api/Validator.php
Expand Up @@ -34,6 +34,7 @@ public function validate($name, Shape $shape, array $input)
implode("\n", $this->errors)
);
$this->errors = [];

throw new \InvalidArgumentException($message);
}
}
Expand Down Expand Up @@ -91,10 +92,23 @@ private function check_structure(StructureShape $shape, $value)
private function check_list(ListShape $shape, $value)
{
if (!is_array($value)) {
$this->addError('must be an array. Found ' . Aws\describe_type($value));
$this->addError('must be an array. Found '
. Aws\describe_type($value));
return;
}

list($min, $max, $count) = [$shape['min'], $shape['max'], count($value)];

if ($min && $count < $min) {
$this->addError("must have at least $min members."
. " Value provided has $count.");
}

if ($max && $count > $max) {
$this->addError("must have no more than $max members."
. " Value provided has $count.");
}

$items = $shape->getMember();
foreach ($value as $index => $v) {
$this->path[] = $index;
Expand Down Expand Up @@ -131,22 +145,37 @@ private function check_blob(Shape $shape, $value)
if ($type != 'object' || !method_exists($value, '__toString')) {
$this->addError('must be an fopen resource, a '
. 'GuzzleHttp\Stream\StreamInterface object, or something '
. 'that can be cast to a string. Found ' . Aws\describe_type($value));
. 'that can be cast to a string. Found '
. Aws\describe_type($value));
}
}
}

private function check_numeric(Shape $shape, $value)
{
if (!is_numeric($value)) {
$this->addError('must be numeric. Found ' . Aws\describe_type($value));
$this->addError('must be numeric. Found '
. Aws\describe_type($value));
return;
}

list($min, $max) = [$shape['min'], $shape['max']];

if ($min && $value < $min) {
$this->addError("must be at least $min. Value provided is $value.");
}

if ($max && $value > $max) {
$this->addError("must be no more than $max."
. " Value provided is $value.");
}
}

private function check_boolean(Shape $shape, $value)
{
if (!is_bool($value)) {
$this->addError('must be a boolean. Found ' . Aws\describe_type($value));
$this->addError('must be a boolean. Found '
. Aws\describe_type($value));
}
}

Expand All @@ -155,6 +184,19 @@ private function check_string(Shape $shape, $value)
if (!$this->checkCanString($value)) {
$this->addError('must be a string or an object that implements '
. '__toString(). Found ' . Aws\describe_type($value));
return;
}

list($min, $max, $len) = [$shape['min'], $shape['max'], strlen($value)];

if ($min && $len < $min) {
$this->addError("must be at least $min characters long."
. " Value provided is $len characters long.");
}

if ($max && $len > $max) {
$this->addError("must be no more than $max characters long."
. " Value provided is $len characters long.");
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/data/s3/2006-03-01/api-2.json
Expand Up @@ -2945,7 +2945,10 @@
"member":{"shape":"ObjectIdentifier"},
"flattened":true
},
"ObjectKey":{"type":"string"},
"ObjectKey":{
"type":"string",
"min":1
},
"ObjectList":{
"type":"list",
"member":{"shape":"Object"},
Expand Down
2 changes: 1 addition & 1 deletion src/data/s3/2006-03-01/api-2.json.php

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions tests/Api/ValidatorTest.php
Expand Up @@ -298,6 +298,49 @@ public function validationProvider()
['foo' => Psr7\stream_for('test')],
true
],
[
[
'type' => 'structure',
'members' => ['foo' => [
'type' => 'string',
'min' => 10,
'max' => 1,
]]
],
['foo' => 'bar'],
"Found 2 errors while validating the input provided for the Foo operation:\n"
. "[foo] must be at least 10 characters long. Value provided is 3 characters long.\n"
. "[foo] must be no more than 1 characters long. Value provided is 3 characters long."
],
[
[
'type' => 'structure',
'members' => ['foo' => [
'type' => 'integer',
'min' => 10,
'max' => 1,
]]
],
['foo' => 3],
"Found 2 errors while validating the input provided for the Foo operation:\n"
. "[foo] must be at least 10. Value provided is 3.\n"
. "[foo] must be no more than 1. Value provided is 3."
],
[
[
'type' => 'structure',
'members' => ['foo' => [
'type' => 'list',
'member' => ['type' => 'string'],
'min' => 10,
'max' => 1,
]]
],
['foo' => ['bar', 'baz']],
"Found 2 errors while validating the input provided for the Foo operation:\n"
. "[foo] must have at least 10 members. Value provided has 2.\n"
. "[foo] must have no more than 1 members. Value provided has 2."
],
];
}

Expand Down

0 comments on commit 81cdc53

Please sign in to comment.