I have designed the following input validation implementation with one goal: DRY. I'd like to know whether it is intuitive and what are the possible shortcomings.
$vlad = new \ay\vlad\Vlad();
$test = $vlad->test([ // Test script
[ // Group of selectors and validators. Each selector is assessed against each validator.
[ // Selectors
'foo',
'bar',
'baz'
],
[ // Validators/ validation options
['fail' => 'break'],
// Define Validator failure scenario:
// * 'silent' exclude input from the current validator chain.
// * 'soft' record an error and progress to the next Validator.
// * 'hard' (default) record an error and exclude the selector from the rest of the Test.
// * 'break' record an error and interrupt the Test.
'required',
'not_empty',
['fail' => 'hard'], // Reset default Validator failure scenario.
'email',
['length', 'max' => 10] // Length Validator with 'max' option.
]
],
[
['qux', 'quux'],
[
// Validate email only if it is non-empty string.
['fail' => 'silent'],
'not_empty',
['fail' => 'hard'],
'email'
]
],
[
['password'],
[
// 'password' selector value must match 'password_confirm' selector
['match', 'selector' => 'password_confirm']
]
]
]);
$result = $test->assess($dummy_input);
The entire library documentation and examples are avaiable at http://anuary.com/vlad.
$form->addRule(VALIDATE::EMAIL_TYPE, VALIDATE::REQUIRE_VALUE);
a couple of times, so that I can later see, from the way I build the form, which fields are required and which aren't. I can also easily add/change/remove or evenif(){}
rules out. Your API relies on users writing a single well structured array. That's harder to read \$\endgroup\$