Slimgry is a validation middleware for the Slim framework, with a validation syntax similar to Laravel.

1.0.0 2024-03-03 04:26 UTC

This package is auto-updated.

Last update: 2024-05-03 07:45:13 UTC


README

Descrição da Imagem
License: MIT Requires PHP ^8.2 Requires Slim Framework >=   4

Slimgry is a validation middleware for the Slim framework, which validates the request body, with validation syntax similar to Laravel.

Install

composer require nelson-dominici/slimgry

Usage

To add validations to a route, add the NelsonDominici\Slimgry\Slimgry middleware with the validations in the constructor.
If any validation method fails, an NelsonDominici\Slimgry\Exceptions\ValidationMethodException exception will be thrown.

use NelsonDominici\Slimgry\Slimgry;

$app->post('/api/auth', [AuthController::class, 'login'])->add(new Slimgry(
    [
        'email' => ['required','email','trim','string','min:3','max:100'],
        'password' => ['required','trim','string','min:6','max:100']
    ]
));

You can also use |.

[
    'email' => 'required|email|trim|string|min:3|max:100',
    'password' => 'required|trim|string|min:6|max:100'
]

Validating nested fields

You can use "dot notation" to validate nested fields, example:

[
    'users.adm.email' => ['required','email','trim','string','min:3','max:100'],
    'users.adm.password' => ['required','trim','string','min:6','max:100'],
]

Adding custom message when validation method fails

Add a second array in the Slimgry class to store custom messages, choose which field the message refers to along with a "dot" and the validation method that failed.

use NelsonDominici\Slimgry\Slimgry;

$app->post('/api/auth', [AuthController::class, 'auth'])->add(new Slimgry(
    [
        'email' => ['required','email','trim','string','min:3','max:100'],
        'password' => ['required','trim','string','min:6','max:100']
    ],
    [
        'email.email' => 'We need a valid email.',
        'password.required' => 'We need your password.'
    ]
));

Validation Methods List

Validation Method Function Attention!
array The field under validation must be a PHP array
boolean The field under validation must be a boolean Only true or false is accepted
email The field under validation must be a valid email filter_var() is used with FILTER_VALIDATE_EMAIL
gt:value The field under validation must be greater than a numeric value Only numeric values are validated
gte:value The field under validation must be greater than or equal to a numeric value Only numeric values are validated
integer The field under validation must be an integer
ip The field under validation must be an IP address filter_var() is used with FILTER_VALIDATE_IP
max:value The field under validation must have a maximum number of elements Only arrays, strings, and numeric values are validated
min:value The field under validation must have a minimum number of elements Only arrays, strings, and numeric values are validated
nullable The field under validation may be null
numeric The field under validation must be numeric
present The field under validation must exist in request body
regex:pattern The field under validation must match the given regular expression
required The field under validation must be present in request body and not "empty" Values considered "empty" are null, empty string and empty array
size:value The field under validation must have a specific number of elements Only arrays, strings, and numeric values are validated
string The field under validation must be a string
trim Remove white space from The field under validation
uuid The field under validation must be a valid universally unique identifier (UUID) in 4 version