Extensibility Pattern
While some validators require specific props to function, it is common for validators to accept arbitrary validator props on return them on validation results. This provides opportunities for applications to have very rich user experiences.
Consider the following implementation of our letter validator.
With this approach, the validator can define a default message but allow the message to be overridden by validator props supplied to the factory. The resolved letter prop is also echoed in the validation result. Plus, arbitrary properties flow through the factory to the validation result. This approach can unlock many scenarios where applications need to enrich the validation results.
Note that the isValid
result property should be applied after spreading the validator props. This guards against an application inadvertently passing isValid
and overriding the actual validation results.
Let's see how an application could pass more context through to the letter validator to get rich validation results.
Last updated