jardispsr / validation
This package provides validation interfaces for a domain driven design approach
Installs: 75
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/jardispsr/validation
Requires
- php: >=8.2
Requires (Dev)
- phpstan/phpstan: ^2.0.4
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.11.2
README
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 existgetErrors(): array- Returns all errors in hierarchical structurehasErrorsForField(string $field): bool- Check if field has errorsgetErrorsForField(string $field): array- Get errors for specific fieldgetFieldErrors(string $field): array- Alias for getErrorsForFieldhasFieldError(string $field): bool- Check if field has non-empty errorsgetAllFieldsWithErrors(): array- Get array of field names with errorsgetErrorCount(): int- Get total count of fields with errorsgetFirstError(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
- Issues: GitHub Issues
- Email: jardisCore@headgent.dev
Authors
- Jardis Core Development (jardisCore@headgent.dev)
Keywords
- validation
- interfaces
- JardisPsr
- Headgent
- DDD (Domain-Driven Design)