Skip to content

Valid Validator Validation Values

Patrick Sachs edited this page Aug 6, 2018 · 9 revisions

Preamble

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.

Idiomatic validator return values

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.

Other allowed validations

The checks are performed in the order they are written down.

null, undefined, empty string

  • The validation passes.

The string "timeout"

  • 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!)

strings, ReactNodes, arrays

  • 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.

Error objects

  • The validation fails with either the error message, stack or name as message.

objects

  • The message of the validation is either the message or the name 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 sanitized validation. 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 or name property the validation fails.
  • Otherwise the validation passes.

booleans

  • If the boolean is true the validation passes.
  • If the boolean is false the validation fails without message.

Fallback

  • A warning is logged to the console.
  • The validation fails without message.
Clone this wiki locally