Skip to content

Repository files navigation

Lemmon Validator

CI Latest Stable Version License

Lemmon Validator is a PHP library with no third-party Composer dependencies for validating and transforming scalar values, indexed arrays, and nested schemas for associative arrays or stdClass objects through a fluent API. It combines form-safe coercion, ordered pipelines, and structured errors for exception and non-exception workflows.

Note

Lemmon Validator is pre-1.0 and under active development. Minor releases may contain breaking changes; review the changelog before upgrading.

Installation

composer require lemmon/validator

Requirements: PHP 8.3 or higher with the mbstring extension

Quick Start

Validators accept null by default. Use required() when a value must be present.

use Lemmon\Validator\ValidationException;
use Lemmon\Validator\Validator;

$input = [
    'name' => 'Ada Lovelace',
    'age' => '36',
    'email' => 'ada@example.com',
];

$userSchema = Validator::isAssociative([
    'name' => Validator::isString()
        ->pipe('trim')
        ->notEmpty()
        ->required(),
    'age' => Validator::isInt()
        ->coerce()
        ->min(18), // Optional; "36" becomes 36
    'email' => Validator::isString()
        ->pipe('trim')
        ->email()
        ->required(),
]);

// Non-exception workflow
[$valid, $user, $errors] = $userSchema->tryValidate($input);
if (!$valid) {
    foreach ($errors as $error) {
        echo $error->getPath() . ': ' . $error->getMessage() . PHP_EOL;
    }
}

// Exception workflow
try {
    $user = $userSchema->validate($input);
} catch (ValidationException $exception) {
    $allErrors = $exception->getErrors();
    $emailErrors = $exception->getErrors('email');
}

Features

  • No third-party Composer dependencies — the runtime depends only on PHP and mbstring
  • Runtime type validation for strings, integers, floats, booleans, indexed arrays, associative arrays, and stdClass objects
  • Form-safe optional fields — validators accept null unless required(); numeric, boolean, and container coercion turns an empty string into null, while strings can opt in with nullifyEmpty()
  • Predictable processing — pipeline steps execute in chain order, then default() and required() resolve the final value
  • Composable schemas — validate nested data, omit undeclared fields by default, or retain them explicitly with passthrough()
  • Structured errors — stable codes, dotted paths, messages, and parameters support programmatic handling, i18n, path filtering, and JSON serialization
  • Extensible rules — use PHP callables with transform(), pipe(), and satisfies(), or compose validators with logical combinators

Philosophy

Lemmon Validator focuses on core validation and predictable schema behavior rather than reimplementing every specialized rule. Its transformation and custom-validation methods accept ordinary PHP callables, so application-specific behavior can be composed without framework integrations or custom validator subclasses.

Documentation

Getting Started

Validation Guides

Factory Reference

Examples

For AI Agents

  • llms.txt — Technical specification with API signatures, parameters, and core behavior

Security

Please report suspected vulnerabilities privately by following the Security Policy. Do not disclose security issues in the public issue tracker.

Contributing

Contributions are welcome. Read the Contributing Guide, or open an issue to report a bug or propose an improvement.

License

Lemmon Validator is licensed under the MIT License.

About

Type-safe validation for PHP with a fluent API, schema-based rules, coercion and transformations. Built for both fun and rigor.

Topics

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages