This package provides validation interfaces for a domain-driven design (DDD) approach.
Install the package via Composer:
composer require jardisport/validation- PHP >= 8.2
The package provides validation interfaces and an immutable ValidationResult value object for standardized validation across your application.
Generic validator contract for validating any PHP object:
use JardisPort\Validation\ValidatorInterface;
use JardisPort\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);
}
}Contract for value-level validators that are stateless and reusable:
use JardisPort\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;
}
}Immutable value object for handling validation results:
use JardisPort\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
}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
# Run tests
make phpunit
# Run tests with coverage
make phpunit-coverageThe project uses PHPStan for static analysis and PHP_CodeSniffer for code style checks:
# Run PHPStan
vendor/bin/phpstan analyse
# Run PHP_CodeSniffer
vendor/bin/phpcsA pre-commit hook is automatically installed via Composer's post-install script to ensure code quality before commits.
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Email: jardisCore@headgent.dev
- Jardis Core Development (jardisCore@headgent.dev)
- validation
- interfaces
- JardisPort
- Headgent
- DDD (Domain-Driven Design)