required
validator is the only validator that ensures a value is present. All other validators in Strickland will return isValid: true
if the value supplied is empty. This approach allows all other validators to be applied to optional values. And as we'll explore shortly, validators can be composed to combine required
with other validators.required
recognizes as empty and invalid are:null
undefined
''
(empty string)false
0
(despite being is falsy), required
will indicate the result is valid.false
boolean value being invalid is commonly used to validate that checkboxes must be checked. For example, when a user must accept terms before submitting a form, the checked
state of the checkbox can be validated with required
.required
: A boolean indicating whether or not the value is required (default: true
)required
validator supports three parameter signatures:required(requiredValue)
where the value is used as the required
named proprequired(propsObject)
where the props object contains a required
named proprequired(propsFunction)
where the props function returns a props object with a required
named proprequired
validator respects a named prop of required
that indicates whether or not the value is required. This is useful for dynamic validation scenarios where your application needs to support conditionally required fields; you can apply the required
validator and dynamically supply the required
named prop. If the required
named prop is false
, the validator will always return isValid: true
.