How Joi processes input values during validation
masterWhen validate() is called, Joi follows a specific execution order. Understanding this order is crucial when writing extensions, as extending schemas does not change this sequence:
- Schema Generation: Generates a new schema if dynamic rules like
when()orlink()are present. - Option Merging: Merges validation options with those from
any.prefs(). - Caching: Returns the result immediately if caching is enabled and the value is found.
prepare: Runs thepreparemethod (if defined). Errors here abort validation immediately.coerce: Coerces the value (ifconvertis enabled). Errors here abort validation immediately.- Empty Check: If the value matches
any.empty(), it is converted toundefined. - Presence Validation: Validates required/optional presence.
- Allowed/Valid/Invalid: Validates against allowed/invalid value sets.
validate: Runs base validation. Errors here abort validation immediately.- Rules: Runs specific validation rules.