Compare commits

...

5 commits

Author SHA1 Message Date
vlw
01cc1eea02 doc: add deprecation note to README (#19)
This plugin has been deprecated as of Reflect version 3.8.5

Reviewed-on: https://codeberg.org/reflect/rules-plugin/pulls/19
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2025-04-04 06:00:01 +00:00
aa7d969350 feat: add return Ruleset instance to Ruleset->GET() and Ruleset->POST() (#17)
Quick PR that adds return values to `Ruleset->GET()` and `Ruleset->POST()` to allow for method chaining with for example `Ruleset->validate_or_exit()`. This can come handy for streamlining simple rulesets.

# Example
```php
$ruleset->GET(new Rules("something")->type(Type::STRING))->validate_or_exit();
```

Reviewed-on: https://codeberg.org/reflect/reflect-rules-plugin/pulls/17
2024-11-28 17:05:16 +00:00
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 22 additions and 6 deletions

View file

@ -1,11 +1,14 @@
# Request validation plugin for the [Reflect API Framework](https://github.com/victorwesterlund/reflect)
> [!IMPORTANT]
> 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.
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 +19,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 +74,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

View file

@ -4,6 +4,8 @@
use \ReflectRules\Rules;
use \Reflect\Response;
require_once "Rules.php";
// Available superglobal scopes
@ -120,7 +122,7 @@
// ----
// Perform request processing on GET properties (search parameters)
public function GET(array $rules): void {
public function GET(array $rules): self {
// (Re)enable strict mode if property is null
if ($this->strict === null) {
$this->strict = true;
@ -131,10 +133,12 @@
$this->eval_rules($rule, Scope::GET);
}
return $this;
}
// Perform request processing on POST properties (request body)
public function POST(array $rules): void {
public function POST(array $rules): self {
// (Re)enable strict mode if property is null
if ($this->strict === null) {
$this->strict = true;
@ -145,6 +149,8 @@
$this->eval_rules($rule, Scope::POST);
}
return $this;
}
// ----
@ -169,4 +175,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);
}
}