On making the Validation package not-extendable #1402
Replies: 3 comments 3 replies
-
Hello @bartgloudemans! Thanks for using Respect\Validation 🐼 I'm the current maintainer for the package, and also one of its original authors. I agree with you about extensibility in many aspects. However, I can't deny the improve in code quality since @henriquemoody took those decisions back in the 1.0 to 2.0 transition and I stand behind them. It is very likely that we'll continue to make rules I'm sorry that the upgrade from |
Beta Was this translation helpful? Give feedback.
-
@bartgloudemans I was looking at https://github.com/Respondens/Validation/pull/1/files, which I imagine has some of the changes you need. Stuff like For the use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ValidationException;
trait ExtensibleRuleAdapter
{
protected Validatable $rule;
public function __construct(...$args)
{
$this->rule = new (static::ADAPTS)(...$args);
}
public function assert($input): void
{
$this->rule->assert($input);
}
public function check($input): void
{
$this->rule->check($input);
}
public function getName(): ?string
{
return $this->rule->getName();
}
public function reportError($input, array $extraParameters = []): ValidationException
{
return $this->rule->reportError($input, $extraParameters);
}
public function setName(string $name): Validatable
{
return $this->rule->setName($name);
}
public function setTemplate(string $template): Validatable
{
return $this->rule->setTemplate($template);
}
public function validate($input): bool
{
return $this->rule->validate($input);
}
} Then, you set it up in some bootstrapping or configuration file. If you're extending rules already, you probably have this somewhere: use Respect\Validation\Factory;
Factory::setDefaultInstance(
(new Factory())
->withRuleNamespace('My\\Rules\\')
->withExceptionNamespace('My\\Exceptions\\')
); That will enable you to adapt rules like this: namespace My\Rules;
class MyInt implements Validatable
{
use ExtensibleRuleAdapter;
const ADAPTS = \Respect\Validation\Rules\IntVal::class;
} And finally, just call it: v::myInt(123)->check('foo'); That's it. You can then override the methods you want. These will throw the original rule exception. If you want custom exceptions, you can copy the Is this something that you can try and let me know if the approach works for your case? |
Beta Was this translation helpful? Give feedback.
-
It's been a while that there are no comments here. I'm closing this discussion for now, but feel free to open it whenever you want! 🐼 |
Beta Was this translation helpful? Give feedback.
-
Hi @henriquemoody,
Via this discussion I'm trying to make you reconsider the design decisions you've laid out here: #1280 (comment)
First a bit of context:
We're using the Respect/Validation package in our code base and we're currently moving from
^1.1
to^2.2
. This means that we now have to deal with the fundamental changes in the package where rules have becomefinal
and some attributes have becomeprivate
(likeAbstractWrapper->validatable
). So far I have been extending rules (to override messages, or adjusting to our specific business requirements) and accessing attributes that are now private to make it work in our code. It is a tough update.To summarize your reasoning on why the package is now much more constrained as I understand it: people misused the package, did not fully understand the scope of their own responsibility, did not understand how to deal with and anticipate breaking changes and as a result started to complain about lacking support.
I can imagine it is immensely frustrating to deal with these demands as a creator of an open source package. But please do realize that they are a minority: there are much more users of your package that properly use your package. And most of all: they are wrong. Just close those frustrating and unjust complain-tickets, and let them suffer.
I think an important realization here is: every business (like ours) has its own requirements, quirks and legacy circumstances that cannot be foreseen by a vendor creator. So when you wonder why someone needs validation rules to be extendable, and you cannot think of a reason, it doesn't mean that there are multiple legitimate businesses out there that have a valid case for extending your classes/validation rules.
I think limiting the extensibility of software to enforce proper and secure usage is a mindset for an end-product. Respect/Validation is a vendor package, lives in the context of a bigger project and thus being extendable/hackable is a valid a requirement.
Anyway, I hope this perspective adds to the discussion. Thanks for your library, it has been incredibly valuable to us.
Kind regards,
Bart
Beta Was this translation helpful? Give feedback.
All reactions