cnastasi/functional-validators

A PHP 8.5+ library implementing Value Objects with functional validation using pipes, functors, and union types

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/cnastasi/functional-validators

dev-main 2025-11-25 11:43 UTC

This package is auto-updated.

Last update: 2025-11-25 11:45:05 UTC


README

A PHP 8.5+ library providing functional validation using the pipe operator (|>), functors, and union types. Perfect for building Value Objects with elegant, composable validation.

๐ŸŽฏ Overview

This library provides a functional approach to Value Objects validation in PHP 8.5+. Instead of throwing exceptions on the first validation error, it accumulates all errors and returns them in a type-safe way using union types.

Key Features:

  • โœ… Functional Validation: Uses PHP 8.5 pipes for elegant validation chains
  • โœ… Error Accumulation: Collects all validation errors, not just the first one
  • โœ… Field-Level Errors: For entities, errors are organized by field name
  • โœ… Type Safety: Strong typing with union types (ValueObject|ErrorsBag or Entity|MultipleFieldErrorsBag)
  • โœ… Reusable Validators: Library of composable validators

๐Ÿ“š Related Articles

This project is part of a blog series on Value Objects in PHP:

  1. Value Objects in PHP 8: Building a better code
  2. Advanced Value Objects in PHP 8
  3. Value Object in PHP 8: Entities
  4. Value Object in PHP 8: Build your own type system
  5. Value Objects in PHP 8: Let's introduce a functional approach (this project)

๐Ÿ“‹ Requirements

  • PHP 8.5 or higher
  • Composer

๐Ÿš€ Installation

composer require cnastasi/functional-validators

Or add it manually to your composer.json:

{
    "require": {
        "cnastasi/functional-validators": "^0.1"
    }
}

๐Ÿ’ก Quick Start

Single Value Object

use CN\FunctionalValidators\Examples\Age;
use CN\FunctionalValidators\Errors\ErrorsBag;

$result = Age::create(25);
if ($result instanceof Age) {
    echo $result->value; // 25
} elseif ($result instanceof ErrorsBag) {
    foreach ($result->getErrors() as $error) {
        echo $error->message . "\n";
    }
}

Entity with Multiple Fields

use CN\FunctionalValidators\Examples\Person;
use CN\FunctionalValidators\Errors\MultipleFieldErrorsBag;

$result = Person::create('', 'invalid-email', -5);
if ($result instanceof MultipleFieldErrorsBag) {
    foreach ($result->getErrorsByField() as $field => $errors) {
        echo "Field '{$field}':\n";
        foreach ($errors as $error) {
            echo "  - {$error->message}\n";
        }
    }
}

Building Your Own Value Objects

use CN\FunctionalValidators\Validators\IntegerValue;
use CN\FunctionalValidators\Errors\ErrorsBag;

readonly final class Price
{
    private function __construct(public int $value) {}

    public static function create(mixed $value): Price|ErrorsBag
    {
        $context = $value
            |> IntegerValue::from(...)
            |> IntegerValue::min(0, "Price cannot be negative")
            |> IntegerValue::max(100000, "Price cannot exceed 1000.00โ‚ฌ");

        return $context->isValid()
            ? new self($context->getValue())
            : $context->getErrors();
    }
}

๐Ÿ“– For detailed usage instructions, see USAGE.md

๐Ÿ—๏ธ Architecture

  • CN\FunctionalValidators\Validators\: Core validation classes (ValidationContext, MultipleValidationContext, IntegerValue, StringValue)
  • CN\FunctionalValidators\Errors\: Error handling (ErrorsBag, MultipleFieldErrorsBag, Error)
  • CN\FunctionalValidators\Examples\: Example Value Objects (Age, Email, Name, Password, Person)

๐Ÿงช Testing

composer test

๐Ÿ“– Documentation

  • Usage Guide: Detailed guide on building your own Value Objects
  • Blog Article: Deep dive into the concepts and design decisions

๐Ÿค Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

๐Ÿ“ License

This project is open-sourced software licensed under the MIT license.

๐Ÿ‘ค Author

Christian Nastasi

๐Ÿ™ Acknowledgments

This project explores functional programming concepts in PHP, specifically:

  • PHP 8.5 pipe operator (|>)
  • Functors for error accumulation
  • Union types instead of Either monads
  • Reusable validator composition

โš ๏ธ Note

This is an experimental project exploring PHP 8.5 features. While functional and tested, it's primarily intended as a learning resource and proof of concept.