From 8b72e5445a32601aad81e5d1bfd6705b4f9c9b1f Mon Sep 17 00:00:00 2001 From: netsco <12622955+netsco@users.noreply.github.com> Date: Thu, 8 Oct 2020 15:16:47 +0200 Subject: [PATCH 1/5] Update Same.php Typo should be 'as' not 'with' --- src/Rules/Same.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Same.php b/src/Rules/Same.php index b127b55..eda613c 100644 --- a/src/Rules/Same.php +++ b/src/Rules/Same.php @@ -8,7 +8,7 @@ class Same extends Rule { /** @var string */ - protected $message = "The :attribute must be same with :field"; + protected $message = "The :attribute must be same as :field"; /** @var array */ protected $fillableParams = ['field']; From 9d07540bada4fc461634ecb5ab9aa2fc985ff833 Mon Sep 17 00:00:00 2001 From: Ben Freeman Date: Sun, 27 Nov 2022 16:58:09 +0100 Subject: [PATCH 2/5] adding isset key array to Helper.php --- src/Helper.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Helper.php b/src/Helper.php index 06e0ada..8a7da25 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -84,6 +84,31 @@ public static function arrayGet(array $array, $key, $default = null) return $array; } + /** + * + * @param array $array + * @param string|null $key + * @return mixed + */ + public static function arrayIsset(array $array, $key) + { + if (is_null($key)) { + return false; + } + + if (array_key_exists($key, $array)) { + return true; + } + + foreach (explode('.', $key) as $segment) { + if (is_array($array) && array_key_exists($segment, $array)) { + return true; + } + } + + return false; + } + /** * Flatten a multi-dimensional associative array with dots. * Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L81 From abca9e93627b80a0543a7edab7cc71cc85660fc2 Mon Sep 17 00:00:00 2001 From: Ben Freeman Date: Sun, 27 Nov 2022 17:01:57 +0100 Subject: [PATCH 3/5] Only return data for set inputs if no value returned --- src/Validation.php | 28 ++++++++++++++++++++++++++-- tests/ValidatorTest.php | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Validation.php b/src/Validation.php index d74786a..839f5de 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -138,6 +138,8 @@ protected function validateAttribute(Attribute $attribute) $attributeKey = $attribute->getKey(); $rules = $attribute->getRules(); + $inputIsset = $this->inputIsset($attributeKey); + $value = $this->getValue($attributeKey); $isEmptyValue = $this->isEmptyValue($value); @@ -169,9 +171,9 @@ protected function validateAttribute(Attribute $attribute) } } - if ($isValid) { + if ($isValid && ($value!==null || $inputIsset)) { $this->setValidData($attribute, $value); - } else { + } else if (!$isValid) { $this->setInvalidData($attribute, $value); } } @@ -592,6 +594,17 @@ public function getValue(string $key) return Helper::arrayGet($this->inputs, $key); } + /** + * Given $key and get value + * + * @param string $key + * @return mixed + */ + public function inputIsset(string $key):bool + { + return Helper::arrayIsset($this->inputs, $key); + } + /** * Set input value * @@ -686,6 +699,17 @@ public function getValidData(): array return $this->validData; } + /** + * + * + */ + public function validated(): array + { + $this->inputs; + //check inputs and check if validData is not null + $this->validData; + } + /** * Set invalid data * diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index a17a123..9110c48 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -1282,6 +1282,7 @@ public function testGetValidData() 'thing' => 'exists', ], [ 'thing' => 'required', + 'name' => '', 'items.*.product_id' => 'required|numeric', 'emails.*' => 'required|email', 'items.*.qty' => 'required|numeric', From b165ec5bcd6e14249b729c90b16281c2e53a79c1 Mon Sep 17 00:00:00 2001 From: Ben Freeman Date: Sun, 27 Nov 2022 17:09:13 +0100 Subject: [PATCH 4/5] changing to alpha rule --- tests/ValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 9110c48..9154185 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -1282,7 +1282,7 @@ public function testGetValidData() 'thing' => 'exists', ], [ 'thing' => 'required', - 'name' => '', + 'name' => 'alpha', 'items.*.product_id' => 'required|numeric', 'emails.*' => 'required|email', 'items.*.qty' => 'required|numeric', From 516db487539179d867b9434d6941cddb5aa1c2c5 Mon Sep 17 00:00:00 2001 From: Ben Freeman Date: Sun, 27 Nov 2022 19:03:19 +0100 Subject: [PATCH 5/5] Adding validated and throw exception with errors --- src/Validation.php | 12 ++++-- src/ValidationException.php | 84 +++++++++++++++++++++++++++++++++++++ tests/ValidatorTest.php | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/ValidationException.php diff --git a/src/Validation.php b/src/Validation.php index 839f5de..386737b 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -699,15 +699,19 @@ public function getValidData(): array return $this->validData; } + /** + * Get valid data or throw exepction * - * + * @throws \Rakit\Validation\ValidationException + * @return array */ public function validated(): array { - $this->inputs; - //check inputs and check if validData is not null - $this->validData; + if ($this->fails()) { + throw new ValidationException($this); + } + return $this->getValidData(); } /** diff --git a/src/ValidationException.php b/src/ValidationException.php new file mode 100644 index 0000000..1b7e272 --- /dev/null +++ b/src/ValidationException.php @@ -0,0 +1,84 @@ +validation = $validation; + } + + /** + * Create an error message summary from the validation errors. + * + * @param Validation $validation + * @return string + */ + protected static function summarize($validation) + { + $messages = $validation->errors()->all(); + + if (! count($messages) || ! is_string($messages[0])) { + return 'The given data was invalid.'; + } + + $message = array_shift($messages); + + if ($count = $validation->errors()->count()) { + $pluralized = $count === 1 ? 'error' : 'errors'; + + $message .= ' '."and $count more $pluralized"; + } + + return $message; + } + + /** + * Get all the validation error messages. + * + * @return array + */ + public function getErrors():array + { + return $this->validation->errors()->toArray(); + } + + /** + * Set the HTTP status code to be used for the response. + * + * @param int $status + * @return $this + */ + public function status($status) + { + $this->status = $status; + + return $this; + } + +} diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 9154185..8ac314e 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Rakit\Validation\Rule; use Rakit\Validation\Rules\UploadedFile; +use Rakit\Validation\ValidationException; use Rakit\Validation\Validator; class ValidatorTest extends TestCase @@ -1371,6 +1372,85 @@ public function testGetInvalidData() $this->assertFalse(isset($stuffs['two'])); } + public function testValidated() + { + $validation = $this->validator->validate([ + 'emails' => [ + 'foo@bar.com', + 'foo@blah.com' + ], + 'thing' => 'exists', + ], [ + 'thing' => 'required', + 'emails.*' => 'required|email', + ]); + + $validData = $validation->validated(); + + $this->assertEquals([ + 'emails' => [ + 0 => 'foo@bar.com', + 1 => 'foo@blah.com' + ], + 'thing' => 'exists', + ], $validData); + } + + public function testValidatedException() + { + $this->expectException(ValidationException::class); + $validation = $this->validator->validate([ + 'items' => [ + [ + 'product_id' => 1, + 'qty' => 'invalid' + ] + ], + 'emails' => [ + 'foo@bar.com', + 'something', + 'foo@blah.com' + ], + 'stuffs' => [ + 'one' => '1', + 'two' => '2', + 'three' => 'three', + ], + 'thing' => 'exists', + ], [ + 'thing' => 'required', + 'items.*.product_id' => 'required|numeric', + 'emails.*' => 'required|email', + 'items.*.qty' => 'required|numeric', + 'something' => 'required|in:on,off', + 'stuffs' => 'required|array', + 'stuffs.one' => 'numeric', + 'stuffs.two' => 'numeric', + 'stuffs.three' => 'numeric', + ]); + $validation->validated(); + } + + public function testValidatedExceptionData() + { + try { + $validation = $this->validator->validate([ + ], [ + 'thing' => 'required' + ]); + $validation->validated(); + } catch (ValidationException $e) { + $this->assertSame( + [ + 'thing' => [ + 'required'=> 'The Thing is required' + ] + ], + $e->getErrors() + ); + } + } + public function testRuleInInvalidMessages() { $validation = $this->validator->validate([