lemmon/validator
Composer 安装命令:
composer require lemmon/validator
包简介
A lightweight, fluent validation library for PHP.
README 文档
README
Note
This library is in active development. The API may change in future versions as we refine and improve the developer experience based on real-world usage and feedback.
Lemmon Validator is a comprehensive, fluent validation and data processing library for PHP that prioritizes developer experience, type safety, and real-world practicality. Inspired by modern validation libraries like Valibot and Zod, it brings a chainable, readable API to PHP for validation, transformation, and sanitization with intelligent error handling and form-safe defaults.
Installation
composer require lemmon/validator
Requirements: PHP 8.3 or higher
Quick Start
All runtime classes live under the Lemmon\Validator namespace.
use Lemmon\Validator\Validator; // Simple validation with form-safe coercion $email = Validator::isString() ->nullifyEmpty() // Empty strings become null (form-safe) ->email() ->validate('user@example.com'); // Schema validation with custom logic $userSchema = Validator::isAssociative([ 'name' => Validator::isString()->required(), 'age' => Validator::isInt()->min(18)->coerce(), 'email' => Validator::isString()->nullifyEmpty()->email(), 'password' => Validator::isString()->minLength(8, 'Password too short') ]); // Tuple-based validation (no exceptions) [$valid, $user, $errors] = $userSchema->tryValidate($input); if (!$valid) { // $errors is a flat list of ValidationError objects (path, code, message, params) foreach ($errors as $error) { echo "{$error->getPath()}: {$error->getMessage()}\n"; } } // Exception-based validation try { $user = $userSchema->validate($input); } catch (\Lemmon\Validator\ValidationException $e) { $all = $e->getErrors(); // every ValidationError $nameErrors = $e->getErrors('name'); // just the 'name' field (and its subtree) }
Features
- Type-safe validation for strings, integers, floats, arrays, and objects, powered by PHP enums for IDE autocomplete, refactoring safety, and zero magic strings
- Fluent, chainable API with a guaranteed execution order — methods run exactly as written (
->pipe('trim')->nullifyEmpty()->required()) - Smart null handling — validations skip
nullunlessrequired();transform()/pipe()/nullifyEmpty()skipnullby default - Form-safe coercion — empty strings become
null(not dangerous0/false); opt intocoerce()for form-friendly type conversions - Universal transformations (
transform(),pipe()) that plug in any PHP callable or external library - Custom validation with
satisfies(), plus logical combinators (satisfiesAll()/satisfiesAny()/satisfiesNone()) - Exact-value and enum matching —
const()for single values,enum()forBackedEnum/UnitEnum(available on all validators) - Last-resort defaults —
default()fillsnullafter the pipeline, beforerequired()enforces presence - Structured errors —
getErrors()returnsValidationErrorobjects with a stablecode(seeValidationCode), dottedpath,message, andparamsfor programmatic handling and i18n; pass a path (getErrors('address')) to filter to one field and its subtree.ValidationErrorisJsonSerializable, so the list drops straight into a JSON API response - Fail-fast per field, aggregated per schema — each validator stops at its first failure while schema validation collects errors across all fields
- Accurate schema results — output includes only provided fields and fields with defaults, never unexpected properties
- Strict typing throughout — every file uses
declare(strict_types=1);
Philosophy
Simple and minimal, with extensibility over reinvention. Rather than reimplementing every possible transformation or validation rule, Lemmon Validator provides a solid foundation with generic transform() and pipe() methods that integrate seamlessly with PHP's ecosystem. Need complex string transformations? Plug in Laravel's Str class. Need advanced array operations? Connect Laravel Collections. The library focuses on what it does best — type-safe validation with excellent developer experience — while letting you leverage the entire PHP ecosystem.
Documentation
Getting Started
Validation Guides
- String Validation — Email, URL, patterns, length constraints
- Numeric Validation — Integers, floats, ranges, constraints
- Array Validation — Indexed arrays and item validation
- Object & Schema Validation — Complex nested structures
- Custom Validation — User-defined functions and business logic
- Error Handling — Working with validation errors
API Reference
Examples
For AI Agents
llms.txt- Complete technical specification with full API signatures, method parameters, and core concepts. Download this file and provide it to your AI agent for accurate code generation and assistance with the library.
Contributing
We welcome contributions! Please see our Contributing Guide for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
lemmon/validator 适用场景与选型建议
lemmon/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 489 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「schema」 「validator」 「validation」 「zod」 「valibot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lemmon/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lemmon/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lemmon/validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Build forms from schema
Specifications like JSON schemas and other things for mosparo
统计信息
- 总下载量: 489
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-05