cnastasi/functional-validators 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cnastasi/functional-validators

Composer 安装命令:

composer require cnastasi/functional-validators

包简介

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

README 文档

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.

cnastasi/functional-validators 适用场景与选型建议

cnastasi/functional-validators 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validation」 「functional-programming」 「pipes」 「functor」 「value-objects」 「union-types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cnastasi/functional-validators 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cnastasi/functional-validators 我们能提供哪些服务?
定制开发 / 二次开发

基于 cnastasi/functional-validators 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-23