morfu/validator

Composable pipeline of validators.

This package's canonical repository appears to be gone and the package has been frozen as a result.

0.1.0 2022-11-17 05:58 UTC

This package is not auto-updated.

Last update: 2024-04-05 10:26:17 UTC


README

Latest Version on Packagist Software License

NOT for production. API is NOT stable.

Example

use Morfu\Validator\ValidatorPipeline;

$badFormData = [
    'username' => 'Ali_Baba',
    'password' => 'ope sesme',
    'confirmPassword' => 'open sesame',
];

$passValidator = (new ValidatorPipeline())
    ->check(function ($length, $min) {
        return $length >= $min;
    })->message('Password length should be at least %min%.')
    ->param('min', 10)
    ->param('length', 'strlen')
    ->check(function ($input) {
        return (bool) preg_match('/\d+/', $input);
    })->message('Password should contain digits.')
    ->check(function ($input) {
        return (bool) preg_match('/[[:punct:]]+/', $input);
    })->message('Password should contain punctuation marks.');

$formValidator = (new ValidatorPipeline())
    ->checkField('password', $passValidator)
    ->check(function ($password, $confirmPassword) {
        return $password === $confirmPassword;
    })->message('Password and confirm password does not match.');

array_column($formValidator->getErrors($badFormData), 'message');
// => [
//    'Password length should be at least 10.',
//    'Password should contain digits.',
//    'Password should contain punctuation marks.',
//    'Password and confirm password does not match.',
//];

$formData = array_merge($badFormData, [
    'password' => 'open_sesame11',
    'confirmPassword' => 'open_sesame11',
]);

$formValidator->isValid($formData);
// => true

For further examples see tests/.

What is Morfu

Morfu (from Ancient Greek: μετᾰμορφόω, metamorphóō, "transform") is a set of pipeline-oriented PHP libraries.

What is Validator

Validator is a library of Morfu that provides composable pipeline of validators.

Install

Via Composer

$ composer require morfu/validator

Testing

$ make test

Security

If you discover any security related issues, please email kilych@zoho.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.