mirror of
https://codeberg.org/reflect/reflect-rules-plugin.git
synced 2025-09-14 17:03:42 +02:00
Compare commits
3 commits
9c837fd194
...
df150f0d86
Author | SHA1 | Date | |
---|---|---|---|
df150f0d86 | |||
4133b25e93 | |||
24352ae45b |
2 changed files with 13 additions and 4 deletions
10
README.md
10
README.md
|
@ -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.
|
||||
|
||||
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"]}
|
||||
```
|
||||
```php
|
||||
|
@ -16,7 +16,7 @@ use \ReflectRules\Type;
|
|||
use \ReflectRules\Rules;
|
||||
use \ReflectRules\Ruleset;
|
||||
|
||||
class GET_MyEndpoint implements Endpoint {
|
||||
class POST_MyEndpoint implements Endpoint {
|
||||
private Ruleset $rules;
|
||||
|
||||
public function __construct() {
|
||||
|
@ -71,6 +71,8 @@ Ruleset->get_errors();
|
|||
|
||||
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
|
||||
|
||||
Install with composer
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
use \ReflectRules\Rules;
|
||||
|
||||
use \Reflect\Response;
|
||||
|
||||
require_once "Rules.php";
|
||||
|
||||
// Available superglobal scopes
|
||||
|
@ -169,4 +171,9 @@
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue