mercredi 25 septembre 2019

What is the name of the design pattern used by this validation code?

I have found it convenient to implement validation directly in the risen exception classes like:

>>> class IsNoneError(ValueError):
...     @classmethod
...     def validate(cls, value):
...         if value is None:
...             raise cls
... 
>>> IsNoneError.validate(1)
>>> IsNoneError.validate(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in validate
__main__.IsNoneError

As there is nothing new under the sun I guess someone had already invented this exception-validator class pattern. What is its name then, which I may use talking to other pythonists to be understood?

Also, what are possible drawbacks of this design pattern?

Aucun commentaire:

Enregistrer un commentaire