Parent schema object.
Terms of current schema.
Flags of current schema.
Starts a ruleset in order to apply multiple rule options. The set ends when rule()
, keep()
, message()
, or warn()
is called.
Adds a rule to current validation schema.
Internally compiles schema.
Creates a joi error object.
Get value from given flag.
Retrieve some rule configuration.
Returns true if validations runs fine on given value.
Resets current schema.
Get value from given property.
Get schema at given path.
Get current schema root references.
Set flag to given value.
Runs internal validations against given value.
Whitelists a value
Assign target alteration options to a schema that are applied when any.tailor()
is called.
an object where each key is a target name, and each value is a function that takes an schema and returns an schema.
By default, some Joi methods to function properly need to rely on the Joi instance they are attached to because
they use this
internally.
So Joi.string()
works but if you extract the function from it and call string()
it won't.
bind()
creates a new Joi instance where all the functions relying on this
are bound to the Joi instance.
Adds caching to the schema which will attempt to cache the validation results (success and failures) of incoming inputs.
If no cache is passed, a default cache is provisioned by using cache.provision()
internally.
Casts the validated value to the specified type.
Returns a new type that is the result of adding the rules of one type to another.
Adds a custom validation function.
Sets a default value if the original value is undefined
where:
the default value. One of:
function(parent, helpers)
where:parent
- a clone of the object containing the value being validated. Note that since specifying a
parent
argument performs cloning, do not declare format arguments if you are not using them.helpers
- same as thsoe described in any.custom()
When called without any value
on an object schema type, a default value will be automatically generated
based on the default values of the object keys.
Note that if value is an object, any changes to the object after default()
is called will change the
reference and any future assignment.
Returns a plain object representing the schema's rules and properties
Annotates the key
Disallows values.
Considers anything that matches the schema to be empty (undefined).
any object or joi schema to match. An undefined schema unsets that rule.
Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
Overrides the default joi error with a custom error if the rule fails where:
can be:
an instance of Error
- the override error.
a function(errors)
, taking an array of errors as argument, where it must either:
return a string
- substitutes the error message with this text
return a single object
or an Array
of it, where:
type
- optional parameter providing the type of the error (eg. number.min
).
message
- optional parameter if template
is provided, containing the text of the error.
template
- optional parameter if message
is provided, containing a template string, using the same format as usual joi language errors.
context
- optional parameter, to provide context to your error if you are using the template
.
return an Error
- same as when you directly provide an Error
, but you can customize the error message based on the errors.
Note that if you provide an Error
, it will be returned as-is, unmodified and undecorated with any of the
normal joi error properties. If validation fails and another error is found before the error
override, that error will be returned and the override will be ignored (unless the abortEarly
option has been set to false
).
Annotates the key with an example value, must be valid.
Marks a key as required which will not allow undefined as value. All keys are optional by default.
Adds an external validation rule.
Note that external validation rules are only called after the all other validation rules for the entire schema (from the value root) are checked. This means that any changes made to the value by the external rules are not available to any other validation rules during the non-external validation phase. If schema validation failed, no external validation rules are called.
Returns a sub-schema based on a path of object keys or schema ids.
a dot .
separated path string or a pre-split array of path keys. The keys must match the sub-schema id or object key (if no id was explicitly set).
Sets a failover value if the original value fails passing validation.
the failover value. value supports references. value may be assigned a function which returns the default value.
If value is specified as a function that accepts a single parameter, that parameter will be a context object that can be used to derive the resulting value.
Note that if value is an object, any changes to the object after failover()
is called will change the reference and any future assignment.
Use a function when setting a dynamic value (e.g. the current time).
Using a function with a single argument performs some internal cloning which has a performance impact.
If you do not need access to the context, define the function without any arguments.
Marks a key as forbidden which will not allow any value except undefined. Used to explicitly forbid keys.
Returns a new schema where each of the path keys listed have been modified.
an array of key strings, a single key string, or an array of arrays of pre-split key strings.
a function which must return a modified schema.
Verifies that an assertion passes for at least one item in the array, where:
schema
- the validation rules required to satisfy the assertion. If the schema
includes references, they are resolved against
the array item being tested, not the value of the ref
target.
Sets a schema id for reaching into the schema via any.extract()
.
If no id is set, the schema id defaults to the object key it is associated with.
If the schema is used in an array or alternatives type and no id is set, the schema in unreachable.
Disallows values.
List the types allowed for the array values. If a given type is .required() then there must be a matching item in the array. If a type is .forbidden() then it cannot appear in the array. Required items can be added multiple times to signify that multiple items must be found. Errors will contain the number of items that didn't match. Any unmatched item having a label will be mentioned explicitly.
Same as rule({ keep: true })
.
Note that keep()
will terminate the current ruleset and cannot be followed by another rule option.
Use rule()
to apply multiple rule options.
Overrides the key name in error messages.
Specifies the exact number of items in the array.
Specifies the maximum number of items in the array.
Same as rule({ message })
.
Note that message()
will terminate the current ruleset and cannot be followed by another rule option.
Use rule()
to apply multiple rule options.
Same as any.prefs({ messages })
.
Note that while any.message()
applies only to the last rule or ruleset, any.messages()
applies to the entire schema.
Attaches metadata to the key.
Specifies the minimum number of items in the array.
Disallows values.
Annotates the key
Requires the validated value to match of the provided any.allow()
values.
It has not effect when called together with any.valid()
since it already sets the requirements.
When used with any.allow()
it converts it to an any.valid()
.
Marks a key as optional which will allow undefined as values. Used to annotate the schema for readability as all keys are optional by default.
Overrides the global validate() options for the current key and any sub-key.
Lists the types in sequence order for the array values where:
Overrides the global validate() options for the current key and any sub-key.
Overrides the global validate() options for the current key and any sub-key.
Sets the presence mode for the schema.
Outputs the original untouched value instead of the casted value.
Marks a key as required which will not allow undefined as value. All keys are optional by default.
Applies a set of rule options to the current ruleset or last rule added.
When applying rule options, the last rule (e.g. min()
) is used unless there is an active ruleset defined (e.g. $.min().max()
)
in which case the options are applied to all the provided rules.
Once rule()
is called, the previous rules can no longer be modified and any active ruleset is terminated.
Rule modifications can only be applied to supported rules.
Most of the any
methods do not support rule modifications because they are implemented using schema flags (e.g. required()
) or special
internal implementation (e.g. valid()
).
In those cases, use the any.messages()
method to override the error codes for the errors you want to customize.
Registers a schema to be used by decendents of the current schema in named link references.
Allow single values to be checked against rules as if it were provided as an array. enabled can be used with a falsy value to go back to the default behavior.
Sorts the array by given order.
Allow this array to be sparse. enabled can be used with a falsy value to go back to the default behavior.
Sets the options.convert options to false which prevent type casting for the current key and any child keys.
Marks a key to be removed from a resulting object or array after validation. Used to sanitize output.
Annotates the key
Applies any assigned target alterations to a copy of the schema that were applied via any.alter()
.
Requires the array values to be unique. Remember that if you provide a custom comparator function, different types can be passed as parameter depending on the rules you set on items. Be aware that a deep equality is performed on elements of the array having a type of object, a performance penalty is to be expected for this kind of operation.
Annotates the key with an unit name.
Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
Validates a value using the schema and options.
Validates a value using the schema and options.
Same as rule({ warn: true })
.
Note that warn()
will terminate the current ruleset and cannot be followed by another rule option.
Use rule()
to apply multiple rule options.
Generates a warning.
When calling any.validateAsync()
, set the warning
option to true to enable warnings.
Warnings are reported separately from errors alongside the result value via the warning key (i.e. { value, warning }
).
Warning are always included when calling any.validate()
.
Converts the type into an alternatives type where the conditions are merged into the type definition where:
Converts the type into an alternatives type where the conditions are merged into the type definition where:
Starts a ruleset in order to apply multiple rule options. The set ends when
rule()
,keep()
,message()
, orwarn()
is called.