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
Requires
- php: ^8.5
Requires (Dev)
- pestphp/pest: ^4.1
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|ErrorsBagorEntity|MultipleFieldErrorsBag) - โ Reusable Validators: Library of composable validators
๐ Related Articles
This project is part of a blog series on Value Objects in PHP:
- Value Objects in PHP 8: Building a better code
- Advanced Value Objects in PHP 8
- Value Object in PHP 8: Entities
- Value Object in PHP 8: Build your own type system
- 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
- Blog: dev.to/cnastasi
- Email: christian.nastasi@gmail.com
๐ 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.