mirror of
https://codeberg.org/reflect/reflect-rules-plugin.git
synced 2025-09-14 08:53:42 +02:00
Compare commits
No commits in common. "master" and "1.5.0" have entirely different histories.
2 changed files with 6 additions and 22 deletions
13
README.md
13
README.md
|
@ -1,14 +1,11 @@
|
||||||
> [!IMPORTANT]
|
# Request validation plugin for the [Reflect API Framework](https://github.com/victorwesterlund/reflect)
|
||||||
> This plugin has since [Reflect version 3.8.5](https://codeberg.org/reflect/reflect/releases/tag/2.8.5) been deprecated. Reflect now has built-in request validation which is enabled by default. The built-in validator is based on this plugin.
|
|
||||||
|
|
||||||
# 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:*
|
||||||
```
|
```
|
||||||
POST Request: /my-endpoint?key1=lorem-ipsum&key2=dolor
|
GET Request: /my-endpoint?key1=lorem-ipsum&key2=dolor
|
||||||
POST Body: {"key3":15, "key4":["hello", "world"]}
|
POST Body: {"key3":15, "key4":["hello", "world"]}
|
||||||
```
|
```
|
||||||
```php
|
```php
|
||||||
|
@ -19,7 +16,7 @@ use \ReflectRules\Type;
|
||||||
use \ReflectRules\Rules;
|
use \ReflectRules\Rules;
|
||||||
use \ReflectRules\Ruleset;
|
use \ReflectRules\Ruleset;
|
||||||
|
|
||||||
class POST_MyEndpoint implements Endpoint {
|
class GET_MyEndpoint implements Endpoint {
|
||||||
private Ruleset $rules;
|
private Ruleset $rules;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
@ -74,8 +71,6 @@ 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
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
use \ReflectRules\Rules;
|
use \ReflectRules\Rules;
|
||||||
|
|
||||||
use \Reflect\Response;
|
|
||||||
|
|
||||||
require_once "Rules.php";
|
require_once "Rules.php";
|
||||||
|
|
||||||
// Available superglobal scopes
|
// Available superglobal scopes
|
||||||
|
@ -122,7 +120,7 @@
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
// Perform request processing on GET properties (search parameters)
|
// Perform request processing on GET properties (search parameters)
|
||||||
public function GET(array $rules): self {
|
public function GET(array $rules): void {
|
||||||
// (Re)enable strict mode if property is null
|
// (Re)enable strict mode if property is null
|
||||||
if ($this->strict === null) {
|
if ($this->strict === null) {
|
||||||
$this->strict = true;
|
$this->strict = true;
|
||||||
|
@ -133,12 +131,10 @@
|
||||||
|
|
||||||
$this->eval_rules($rule, Scope::GET);
|
$this->eval_rules($rule, Scope::GET);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform request processing on POST properties (request body)
|
// Perform request processing on POST properties (request body)
|
||||||
public function POST(array $rules): self {
|
public function POST(array $rules): void {
|
||||||
// (Re)enable strict mode if property is null
|
// (Re)enable strict mode if property is null
|
||||||
if ($this->strict === null) {
|
if ($this->strict === null) {
|
||||||
$this->strict = true;
|
$this->strict = true;
|
||||||
|
@ -149,8 +145,6 @@
|
||||||
|
|
||||||
$this->eval_rules($rule, Scope::POST);
|
$this->eval_rules($rule, Scope::POST);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
@ -175,9 +169,4 @@
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue