Skip to content

Commit

Permalink
Check model-defined min and max values during validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Aug 26, 2015
1 parent ea2c4eb commit db984b9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/Api/Validator.php
Original file line number Diff line number Diff line change
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,21 @@ 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;
}

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

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

$items = $shape->getMember();
foreach ($value as $index => $v) {
$this->path[] = $index;
Expand Down Expand Up @@ -131,22 +143,36 @@ 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;
}

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

if ($shape['max'] && $value > $shape['max']) {
$this->addError('must be no more than ' . $shape['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 +181,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;
}

if ($shape['min'] && strlen($value) < $shape['min']) {
$this->addError('must be at least ' . $shape['min'] . ' characters'
. ' long. Value provided is '. strlen($value) . ' characters'
. ' long.');
}

if ($shape['max'] && strlen($value) > $shape['max']) {
$this->addError('must be no more than ' . $shape['max']
. ' characters long. Value provided is '. strlen($value)
. ' characters long.');
}
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Api/ValidatorTest.php
Original file line number Diff line number Diff line change
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 db984b9

Please sign in to comment.