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.
Optional
typeInternally compiles schema.
Optional
options: CompileOptionsOptional
options: MutateRegisterOptionsAssign 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.
Optional
cache: CacheSets a default value if the original value is undefined
where:
Optional
value: BasicType | Reference | ((parent, helpers) => BasicType | Reference)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 those 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.
Considers anything that matches the schema to be empty (undefined).
Optional
schema: SchemaLikeany object or joi schema to match. An undefined schema unsets that rule.
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
).
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.
Optional
description: stringSets 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.
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.
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.
Optional
name: stringApplies 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.
Validates a value using the schema and options.
Optional
options: TOptsGenerates 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:
Generated using TypeDoc
Starts a ruleset in order to apply multiple rule options. The set ends when
rule()
,keep()
,message()
, orwarn()
is called.