Compare commits

..

3 commits

Author SHA1 Message Date
vlw
df150f0d86 feat: add method to validate and return Reflect\Response if Ruleset is invalid to the Ruleset class (#16)
Closes #13

Reviewed-on: https://codeberg.org/reflect/reflect-rules-plugin/pulls/16
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2024-11-20 10:39:33 +00:00
vlw
4133b25e93 docs(chore): change link to Reflect in README from GitHub to Codeberg (#15)
Reviewed-on: https://codeberg.org/reflect/reflect-rules-plugin/pulls/15
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2024-11-20 10:39:03 +00:00
vlw
24352ae45b doc(fix): use a POST request as the example in the README instread of GET (#14)
The example sends a request body to a GET request, and [GET requests can not have a request body](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET). So this PR changes the example to a POST request instead.

Reviewed-on: https://codeberg.org/reflect/reflect-rules-plugin/pulls/14
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2024-11-20 10:38:06 +00:00
2 changed files with 13 additions and 4 deletions

View file

@ -1,11 +1,11 @@
# Request validation plugin for the [Reflect API Framework](https://github.com/victorwesterlund/reflect) # Request validation plugin for the [Reflect API Framework](https://codeberg.org/reflect/reflect)
This request pre-processor adds request validation for an API written in the Reflect API Framework. This request pre-processor adds request validation for an API written in the Reflect API Framework.
Write safer Reflect endpoints by enforcing request data structure validation before the request reaches your endpoint's logic. This plugin validates GET and POST data (even JSON) and returns an array with scoped `Error`s that can be further acted upon if desired. Write safer Reflect endpoints by enforcing request data structure validation before the request reaches your endpoint's logic. This plugin validates GET and POST data (even JSON) and returns an array with scoped `Error`s that can be further acted upon if desired.
*Example:* ## Example
``` ```
GET Request: /my-endpoint?key1=lorem-ipsum&key2=dolor POST Request: /my-endpoint?key1=lorem-ipsum&key2=dolor
POST Body: {"key3":15, "key4":["hello", "world"]} POST Body: {"key3":15, "key4":["hello", "world"]}
``` ```
```php ```php
@ -16,7 +16,7 @@ use \ReflectRules\Type;
use \ReflectRules\Rules; use \ReflectRules\Rules;
use \ReflectRules\Ruleset; use \ReflectRules\Ruleset;
class GET_MyEndpoint implements Endpoint { class POST_MyEndpoint implements Endpoint {
private Ruleset $rules; private Ruleset $rules;
public function __construct() { public function __construct() {
@ -71,6 +71,8 @@ Ruleset->get_errors();
Use `Ruleset->is_valid(): bool` to quickly check if any errors are set. Use `Ruleset->is_valid(): bool` to quickly check if any errors are set.
You can also use `Ruleset->validate_or_exit(): true|Response` to automatically return a `Reflect\Response` with all errors to current STDOUT if validation fails.
# Installation # Installation
Install with composer Install with composer

View file

@ -4,6 +4,8 @@
use \ReflectRules\Rules; use \ReflectRules\Rules;
use \Reflect\Response;
require_once "Rules.php"; require_once "Rules.php";
// Available superglobal scopes // Available superglobal scopes
@ -169,4 +171,9 @@
return $this->is_valid; return $this->is_valid;
} }
// Return Reflect\Response with errors and code 422 Unprocessable Content if validation failed
public function validate_or_exit(): true|Response {
return $this->is_valid() ? true : new Response($this->errors, 422);
}
} }