Composition Conventions
We defined early on that all validators must be functions in Strickland. This is technically true, but because every
and objectProps
are used so frequently to validate arrays of validators and object properties, composition conventions are built into Strickland's validate
function to automatically use every
and objectProps
.
If a validator is not a function, but it is instead an array, it is assumed to be an array of validator functions. This array will be wrapped with every
.
If a validator is an object, it is assumed to be an object defining validators for object props. This object will be wrapped with objectProps
.
We can rewrite the example for validating a person's name and address more naturally.
There may be times when you do need to explicitly use every
and objectProps
though. With the object and array conventions, there is no way to pass validator props in that would apply at the object-level or to all validators within the array. But it is quite easy to reintroduce the objectProps
or every
wrapper and pass props in after the object or array as seen previously.
Last updated