承接 jardispsr/validation 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jardispsr/validation

最新稳定版本:1.0.0

Composer 安装命令:

composer require jardispsr/validation

包简介

This package provides validation interfaces for a domain driven design approach

README 文档

README

Build Status License: MIT PHP Version PHPStan Level PSR-4 PSR-12

This package provides validation interfaces for a domain-driven design (DDD) approach.

Installation

Install the package via Composer:

composer require jardispsr/validation

Requirements

  • PHP >= 8.2

Usage

The package provides validation interfaces and an immutable ValidationResult value object for standardized validation across your application.

ValidatorInterface

Generic validator contract for validating any PHP object:

use JardisPsr\Validation\ValidatorInterface;
use JardisPsr\Validation\ValidationResult;

class UserValidator implements ValidatorInterface
{
    public function validate(object $data): ValidationResult
    {
        $errors = [];

        if (empty($data->email)) {
            $errors['email'][] = 'Email is required';
        }

        if (empty($data->password)) {
            $errors['password'][] = 'Password is required';
        }

        return new ValidationResult($errors);
    }
}

ValueValidatorInterface

Contract for value-level validators that are stateless and reusable:

use JardisPsr\Validation\ValueValidatorInterface;

class EmailValidator implements ValueValidatorInterface
{
    public function validateValue(mixed $value, array $options = []): ?string
    {
        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
            return 'Invalid email format';
        }

        return null;
    }
}

ValidationResult

Immutable value object for handling validation results:

use JardisPsr\Validation\ValidationResult;

$result = new ValidationResult([
    'email' => ['Invalid email format'],
    'password' => ['Too short', 'Missing special character']
]);

// Check validation status
if (!$result->isValid()) {
    // Get all errors
    $allErrors = $result->getErrors();

    // Get errors for specific field
    $emailErrors = $result->getErrorsForField('email');

    // Get first error for a field
    $firstError = $result->getFirstError('password'); // "Too short"

    // Check if field has errors
    if ($result->hasFieldError('email')) {
        // Handle email errors
    }

    // Get all fields with errors
    $fieldsWithErrors = $result->getAllFieldsWithErrors(); // ['email', 'password']

    // Get error count
    $count = $result->getErrorCount(); // 2
}

ValidationResult Methods

  • isValid(): bool - Returns true if no errors exist
  • getErrors(): array - Returns all errors in hierarchical structure
  • hasErrorsForField(string $field): bool - Check if field has errors
  • getErrorsForField(string $field): array - Get errors for specific field
  • getFieldErrors(string $field): array - Alias for getErrorsForField
  • hasFieldError(string $field): bool - Check if field has non-empty errors
  • getAllFieldsWithErrors(): array - Get array of field names with errors
  • getErrorCount(): int - Get total count of fields with errors
  • getFirstError(string $field): ?string - Get first error message for field

Development

Running Tests

# Run tests
make phpunit

# Run tests with coverage
make phpunit-coverage

Code Quality

The project uses PHPStan for static analysis and PHP_CodeSniffer for code style checks:

# Run PHPStan
vendor/bin/phpstan analyse

# Run PHP_CodeSniffer
vendor/bin/phpcs

Pre-commit Hook

A pre-commit hook is automatically installed via Composer's post-install script to ensure code quality before commits.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Authors

Keywords

  • validation
  • interfaces
  • JardisPsr
  • Headgent
  • DDD (Domain-Driven Design)

统计信息

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

GitHub 信息

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

其他信息

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