Skip to content

Commit

Permalink
Implement better validation error with fields (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal authored Oct 17, 2019
1 parent dcf3478 commit 0a9eb7a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
54 changes: 54 additions & 0 deletions src/Errors/ValidationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Bakery\Errors;

use GraphQL\Error\Error;
use GraphQL\Error\ClientAware;

class ValidationError extends Error implements ClientAware
{
/**
* The validator instance.
*
* @var \Illuminate\Contracts\Validation\Validator
*/
public $validator;

/**
* Create a new exception instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*/
public function __construct($validator)
{
parent::__construct($validator->errors()->first());

$this->validator = $validator;
$this->extensions['validation'] = $validator->errors();
}

/**
* Returns true when exception message is safe to be displayed to a client.
*
* @return bool
* @api
*/
public function isClientSafe()
{
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @return string
* @api
*/
public function getCategory()
{
return 'user';
}
}
14 changes: 0 additions & 14 deletions src/Exceptions/ValidationException.php

This file was deleted.

8 changes: 5 additions & 3 deletions src/Support/RootField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Bakery\Support;

use Bakery\Utils\Utils;
use Bakery\Errors\ValidationError;
use Illuminate\Auth\Access\Response;
use Bakery\Types\Definitions\RootType;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Facades\Validator;
use Bakery\Exceptions\ValidationException;
use Bakery\Exceptions\UnauthorizedException;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down Expand Up @@ -233,7 +233,9 @@ protected function guard(Arguments $args): void

/**
* Validate the arguments of the query.
* @param Arguments $args
*
* @param Arguments $args
* @throws ValidationError
*/
protected function validate(Arguments $args): void
{
Expand All @@ -244,7 +246,7 @@ protected function validate(Arguments $args): void
$validator = Validator::make($args->toArray(), $rules, $messages, $attributes);

if ($validator->fails()) {
throw new ValidationException($validator->getMessageBag());
throw new ValidationError($validator);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Feature/CustomMutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public function it_validates_a_custom_mutation()
'input' => [
'email' => 'invalid-email',
],
])->assertJsonFragment(['message' => 'The email must be a valid email address.']);
])->assertJsonFragment([
'message' => 'The email must be a valid email address.',
'validation' => [
'input.email' => ['The email must be a valid email address.'],
],
]);

$this->assertDatabaseMissing('users', [
'email' => 'invalid-email',
Expand Down

0 comments on commit 0a9eb7a

Please sign in to comment.