-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Refactor to stateless validators #15
Comments
Every single custom validator and every extension to a supplied validator will need to be rewritten? Originally posted by @akrabat at zendframework/zend-validator#181 (comment) |
Yes - as My hope is that we can provide some tooling and guidance around this. For instance, something like the following trait would allow adapting an existing validator to implement the new trait LegacyValidator
{
public function validate($value, array $context = null) : Result
{
if ($this->isValid($value, $context)) {
return ValidatorResult::createValidResult($value);
}
return ValidatorResult::createInvalidResult(
$value,
$this->getMessageTemplates(),
$this->abstractOptions['messageVariables']
);
}
}
class ExistingValidator extends AbstractValidator implements Validator
{
use LegacyValidator;
/* ... */
} If we provide the legacy interface in v3, but mark it deprecated, this approach would allow mixing and matching existing v2-capable validators with v3, providing a longer upgrade path. Originally posted by @weierophinney at zendframework/zend-validator#181 (comment) |
I think there's going to have to be a compelling advantage to v3 or upgrading is going to have to be quite easy :) A quick check of one of my projects shows that I have 30 or so Validators that would need upgrading and that won't be an easy sell to the client if there's no tangible benefit if it's more than a day's work. I assume that the trait won't work for validators that extend current validators, so they'll have to be re-done. Originally posted by @akrabat at zendframework/zend-validator#181 (comment) |
One of the issues currently is that stateful validators can lead to hard to debug issues, particularly if you share an instance. As an example: $validator->isValid($value1);
$validator->isValid($value2);
$messages = $validator->getMessages(); // $value1 messages are missing
$value = $validator->getValue(); // $value2 The other issue I've been seeing is maintenance of the
One nice benefit of the approach is that we can finally use callables for validation more cleanly. If they return a Originally posted by @weierophinney at zendframework/zend-validator#181 (comment) |
This work-in-progress is exploring how we might approach stateless validators.
Essentially, instead of an
isValid()
method returning a boolean, and a subsequent call on the validator to retrieve validation error messages, we would instead:Result
interface modeling the results of validation; it would compose the value validated, validation status, and, if present, any validation error messages.validate()
method, accepting both a value and optional context, and return aResult
instance.ResultAggregate
interface for aggregating several results, as is necessary in aValidatorChain
;Result
instances would be pushed upon an aggregate.Translation, value obscuration, and message truncation then become presentation issues, and can be achieved by decorating
Result
instances.Additionally, we'd remove the concept of
$options
for creating individual validator instances; they would instead have concrete constructor arguments. This simplifies a ton of logic internally, but means that each validator would require its own factory class. On the flip side, it also means developers can write factories for specific options combinations, and have shared instances without worrying about state.Migration concerns
We could create a new minor release that adds the new
Validator
,Result
, andResultAggregate
interfaces, and variousResult
implementations.Validator
would definevalidate
instead ofisValid()
, allowing devs to implement both in order to forward-proof their validators. We could also provide a wrapper forValidator
implementations to make them work asValidatorInterface
instances; in such a case, it would pull info from the result in order to populate its members.The bigger issue is existing validators, and extensions to them. Developers extending existing validators may want to copy/paste implementations and begin migration of those to make them forwards-compatible with version 3. Since we would have version 3 released simultaneously to a v2 with the new interface additions, they could even copy those from v3 to aid their migration.
Integration concerns
I have not yet investigated impact on zend-inputfilter; however, that version will require a similar refactor towards stateless inputs/input filters as well.
Originally posted by @weierophinney at zendframework/zend-validator#181
The text was updated successfully, but these errors were encountered: