From 05d20b11d42cf181a910d1001dfde0e8e597fa0a Mon Sep 17 00:00:00 2001 From: Fabian Mieller Date: Wed, 15 Feb 2023 16:01:44 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20added=20validation=20customAttr?= =?UTF-8?q?ibutes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MetApi.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/MetApi.php b/src/MetApi.php index a9c3994..0ff7482 100644 --- a/src/MetApi.php +++ b/src/MetApi.php @@ -51,7 +51,7 @@ public function metApiInit(Request $request) * @param array $messages * @return Controller */ - public function option(string $name, array|string $rules, array $messages = []): self + public function option(string $name, array|string $rules, array $messages = [], string $customAttribute = null): self { $this->query['options']['rules'][$name] = $rules; @@ -68,6 +68,13 @@ public function option(string $name, array|string $rules, array $messages = []): ); } + if ($customAttribute) { + if (!array_key_exists('customAttributes', $this->query['options'])) { + $this->query['options']['customAttributes'] = []; + } + $this->query['options']['customAttributes'][$name] = $customAttribute; + } + return $this; } @@ -150,7 +157,7 @@ public function paginate($collection, $perpage = 50, $maxPages = 10) public function verify($abort = true) { - $validate = Validator::make($this->request->all(), $this->query['options']['rules'], $this->query['options']['messages'] ?? []); + $validate = Validator::make($this->request->all(), $this->query['options']['rules'], $this->query['options']['messages'] ?? [], $this->query['options']['customAttributes'] ?? []); if ($validate->fails()) { foreach ($validate->errors()->toArray() as $key => $value) { From 38b4043b82553b08e77c76fd1d1a14e0b54c7588 Mon Sep 17 00:00:00 2001 From: Fabian Mieller Date: Wed, 15 Feb 2023 16:11:24 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=92=A1=20customAttribute=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MetApi.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MetApi.php b/src/MetApi.php index 0ff7482..2350dbb 100644 --- a/src/MetApi.php +++ b/src/MetApi.php @@ -49,6 +49,7 @@ public function metApiInit(Request $request) * @param string $name * @param array|string $rules * @param array $messages + * @param string $customAttribute * @return Controller */ public function option(string $name, array|string $rules, array $messages = [], string $customAttribute = null): self From 89adbe88b24526409a53a9e0e8f4a61d706bdced Mon Sep 17 00:00:00 2001 From: Fabian Mieller Date: Wed, 15 Feb 2023 18:59:26 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9D=20added=20example=20for=20vali?= =?UTF-8?q?dation=20custom=20attributes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 3b55e7e..6e32c69 100644 --- a/readme.md +++ b/readme.md @@ -40,10 +40,10 @@ Add the trait use acidjazz\metapi\MetApi; class Controller { - use Metapi; + use Metapi; ``` -## Examples +## Examples ```php option('contact.email', 'required|email', [], 'Email Address') + ->option('contact.name', 'required|string', [], 'Firstname') + ->option('contact.surname', 'required|string', [], 'Lastname') + ->verify(); + ... + $this->render($results); + } +``` + +`POST /send` + +```json +{ + "status": "error", + "errors": [ + { + "status": 400, + "message": "contact.email", + "detail": "Email Address is a required field." + }, + { + "status": 400, + "message": "contact.name", + "detail": "Firstname is a required field." + }, + { + "status": 400, + "message": "contact.surname", + "detail": "Lastname is a required field." + } + ] +} +```