-
Notifications
You must be signed in to change notification settings - Fork 0
Valid Validator Validation Values
Validators allow for a multitude of different return values. This is to allow them to integrate as seamlessly with your possibly already existing validation backend or your own preferred way of validating.
Validators can return synchronously, return a promise that either resolves or rejects, or throw an error.
If you wish to look at the precise logic used to sanitize the validation return values, look at /src/validators/index.js
.
The idiomatic format of the validator return value(from now on called "validation") is the following:
{
validated: "ok" | "error" | "hint",
message: React.ReactNode
}
There is a fourth potentially valid value for validated
("pending"), but this is exclusively meant for internal usage with async validators. Use by third party code is not supported.
All of the following allowed values are sanitized to the format above.
The checks are performed in the order they are written down.
- The validation passes.
- The validation fails with
"timeout"
as message. (This is primarily used internally if an async validator takes too long to validate, but feel free to use it yourself!)
- If the value is falsy the validation passes.
- If the value is an array and has a length of zero the validation passes.
- Otherwise, the validation fails with the value as message.
- The validation fails with either the error
message
,stack
orname
as message.
- The message of the validation is either the
message
or thename
property of the object. - If the object has a
validation
property and the property is a valid validation result, this property is directly used for the sanitizedvalidation
. If it is an invalid validation result the validation fails. - If the object instead has a non-undefined
error
property, the validation passed if it is truthy and fails if it is falsy. - If the object instead has a truthy
message
orname
property the validation fails. - Otherwise the validation passes.
- If the boolean is true the validation passes.
- If the boolean is false the validation fails without message.
- A warning is logged to the console.
- The validation fails without message.