Validator class in SRP principle

I have a validator class.
Is it better to have a validator class which have methods such as validateUrl(), validateEmail(), validateInt(), etc ?
Or, URLValidator class, EmailValidator class, or INTValidator class ?

As with most things it depends. If you know you will only need a few validators then putting them in one class is fine. It’s also a good approach for getting started.

However, breaking them out into individual classes means you can add lot’s more as needed. Make take a look at one of the more popular libraries to see how they do it:

I would highly recommend looking into Laravel since it has a well built, flexible validation layer. Laravel doesn’t use a separate class per “type” but is still extremely powerful and well built.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.