Skip to content

Commit

Permalink
Merge pull request #4609 from caswell-wc/api_validation_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner authored May 9, 2021
2 parents d1fce53 + dfc39ca commit 2c9c9b2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
16 changes: 16 additions & 0 deletions system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,28 @@ public function failNotFound(string $description = 'Not Found', string $code = n
* @param string $message
*
* @return mixed
*
* @deprecated Use failValidationErrors instead
*/
public function failValidationError(string $description = 'Bad Request', string $code = null, string $message = '')
{
return $this->fail($description, $this->codes['invalid_data'], $code, $message);
}

/**
* Used when the data provided by the client cannot be validated on one or more fields.
*
* @param string|string[] $errors
* @param string|null $code
* @param string $message
*
* @return mixed
*/
public function failValidationErrors($errors, string $code = null, string $message = '')
{
return $this->fail($errors, $this->codes['invalid_data'], $code, $message);
}

//--------------------------------------------------------------------

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,25 @@ public function testValidationError()
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
}

public function testValidationErrors()
{
$controller = $this->makeController();
$controller->failValidationErrors(['foo' => 'Nope', 'bar' => 'No way'], 'FAT CHANCE', 'A Custom Reason');

$expected = [
'status' => 400,
'error' => 'FAT CHANCE',
'messages' => [
'foo' => 'Nope',
'bar' => 'No way',
],
];

$this->assertEquals('A Custom Reason', $this->response->getReason());
$this->assertEquals(400, $this->response->getStatusCode());
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
}

public function testResourceExists()
{
$controller = $this->makeController();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Deprecations:
- Consolidated and deprecated ``ControllerResponse`` and ``FeatureResponse`` in favor of ``TestResponse``.
- Deprecated ``Time::instance()``, use ``Time::createFromInstance()`` instead (now accepts ``DateTimeInterface``).
- Deprecated ``IncomingRequest::removeRelativeDirectory()``, use ``URI::removeDotSegments()`` instead
- Deprecated ``\API\ResponseTrait::failValidationError`` to use ``\API\ResponseTrait::failValidationErrors`` instead

Bugs Fixed:

Expand Down
10 changes: 5 additions & 5 deletions user_guide_src/source/outgoing/api_responses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,18 @@ Class Reference

return $this->failNotFound('User 13 cannot be found.');

.. php:method:: failValidationError(string $description = 'Bad Request'[, string $code=null[, string $message = '']])
.. php:method:: failValidationErrors($errors[, string $code=null[, string $message = '']])
:param string $description: The error message to show the user.
:param mixed $errors: The error message or array of messages to show the user.
:param string $code: A custom, API-specific, error code.
:param string $message: A custom "reason" message to return.
:returns: The value of the Response object's send() method.

Sets the appropriate status code to use when data the client sent did not pass validation rules.
Status code is typically 400.
Sets the appropriate status code to use when data the client sent did not pass validation rules. Status code is typically 400.

::

return $this->failValidationError($validation->getError('api_field'));
return $this->failValidationErrors($validation->getErrors());

.. php:method:: failResourceExists(string $description = 'Conflict'[, string $code=null[, string $message = '']])
Expand Down Expand Up @@ -318,3 +317,4 @@ Class Reference
::

return $this->failServerError('Server error.');

0 comments on commit 2c9c9b2

Please sign in to comment.