Validation

It is considered a best practice to validate the correctness of any data sent into a web application. To automatically validate incoming requests, Assegai offers several pipes that are readily available:

  • ValidationPipe
  • ParseIntPipe
  • ParseBoolPipe
  • ParseArrayPipe
  • ParseUUIDPipe

The ValidationPipe leverages the Validator class found in the assegaiphp/validator package, and makes use of simple annotations in local class/DTO declarations in each module, to enforce validation rules for all incoming client payloads. This approach provides a convenient way to ensure that the incoming data meets the specified criteria.

Installation

To install this package, run the following command in your project's root directory:

$ composer require assegaiphp/validation

Basic Usage

To use this package, import the Assegai\Validation\Validator class and create a new instance. Then, you can use the validate() method to check if a value or array of values meets the specified criteria.

use Assegai\Validation\Validator;

$validator = new Validator();
$isValid = $validator->validate($value, 'required|email');

The second argument to the validate() method is a string containing the validation rules to apply, separated by the | character. In the example above, the required and email rules are applied.

Available Rules

This package includes a number of built-in validation rules, such as:

Rule Description
alpha value must contain only alphabetic characters
alnum value must contain only alphanumeric characters
between:n,m value must be between n and m (inclusive)
domain value must be a valid domain name
email value must be a valid email address
equalTo:n value must be equal to n
float value must be a floating-point number
inlist:l value must be in a specific list l of allowed values
integer value must be an integer
maxLength:n value must be no more than n characters long
max:n value must not exceed a maximum value, n
minLength:n value must be at least n characters long
min:n value must be at least a certain value, n
notEqualTo:n value must not be equal to n
notinlist:l value must not be in a specific list of disallowed values
numeric value must be a number
regex:n value must match the given regex pattern n
required value must be present
string value must be a string
url value must be a valid URL

Error Handling

If the validate() method returns false, you can use the getErrors() method to retrieve an array of error messages.

if (!$isValid)
{
  $errors = $validator->getErrors();
  // handle errors}

DTOs and Attributes

In addition to the validate() method, the Assegai validation package also provides a convenient approach to enforce validation rules using data transfer objects (DTOs) and attributes. With this approach, you declare the validation rules in local class/DTO declarations in each module, allowing you to keep your validation rules and your data structures closely tied together.

To use this approach, simply annotate the properties of your DTOs with the appropriate validation attributes. For example:

use Assegai\Validation\Attributes\IsEmail;
use Assegai\Validation\Attributes\IsRequired;
use Assegai\Validation\Attributes\MaxLength;

class ContactFormDto extends stdClass
{
  #[IsRequired]
  #[MaxLength(255)]
  public string $name = '';

  #[IsRequired]
  #[IsEmail]
  public string $email = '';

  #[IsRequired]
  #[MaxLength(500)]
  public string $message = '';
}

You can then validate instances of this class using the validateDto() method of the Validator class:

$contactForm = new ContactFormDto();
$contactForm->name = 'John Doe';
$contactForm->email = 'john@example.com';
$contactForm->message = 'Hello, I would like to get in touch with you.';

$validator = new Validator();
$isValid = $validator->validateDto($contactForm);

if (!$isValid)
{
  $errors = $validator->getErrors();
  // handle errors}

In conclusion, the Assegai validation package provides a flexible and easy-to-use solution for validating data in your projects. Whether you prefer a simple rule-based approach or a more structured DTO and attribute-based approach, the validation package has got you covered.