Let’s booty a attending at the afterward example, area we appetite to agreement that the provided user name has a minimum breadth of 8 characters:
This validation action already works as accepted and allotment the adapted absurdity bulletin if the provided ascribe is invalid. Now let’s booty a attending at addition situation, area we ability appetite to ascertain a altered message.
Again, we can dynamically ascertain the absurdity bulletin back needed, but we additionally ability appetite to be able to ascertain the validation action itself. This would leave our checkUserName action to accept no abstraction of what a user name absolutely is.
We renamed our checkUserName action to runValidation, as this action is absolute of any context. It expects the data, a validation action and an absurdity message.
Taking a afterpiece attending at runValidation, we ability apprehension that if we capital to run checkUserName with altered data, we would accept to redefine the validation action and absurdity bulletin every time as well.But we’re mainly absorbed in defining the specific validation action and absurdity bulletin once, and again active that action with altered data.
Our aing attack is to accredit accouterment abstracts as adjoin a authentic validation function.
This access ensures that the validation action can be authentic already and run assorted times with altered inputs. If you recall, we’re calling validateUserName with aloof the data, aloof like in the actual aboriginal archetype now.
Our accepted accomplishing ensures validation functions to be congenital dynamically and this is already absolutely useful. But in the aing section, we will see how we can advance this abstraction and the all-embracing convenience.