Interface: ArraySchema<TSchema>
Schema type for array validation.
Extends
AnySchema<TSchema>
Type Parameters
| Type Parameter | Default type |
|---|---|
TSchema | any[] |
Properties
| Property | Modifier | Type | Description | Inherited from |
|---|---|---|---|---|
_flags | public | Record<string, any> | Flags of current schema. | AnySchema._flags |
~standard | readonly | Props<TSchema, TSchema> | The Standard Schema properties. | AnySchema.~standard |
$ | public | ArraySchema<TSchema> | Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called. | AnySchema.$ |
$_knex | public | any | - | AnySchema.$_knex |
$_super | public | Schema | Parent schema object. | AnySchema.$_super |
$_terms | public | Record<string, any> | Terms of current schema. | SchemaInternals.$_terms |
ruleset | public | ArraySchema<TSchema> | Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called. | AnySchema.ruleset |
type? | public | string | - | AnySchema.type |
Methods
$_addRule()
$_addRule(rule: string | AddRuleOptions): Schema;Adds a rule to current validation schema.
Parameters
| Parameter | Type |
|---|---|
rule | string | AddRuleOptions |
Returns
Inherited from
$_compile()
$_compile(schema: SchemaLike, options?: CompileOptions): Schema;Internally compiles schema.
Parameters
| Parameter | Type |
|---|---|
schema | SchemaLike |
options? | CompileOptions |
Returns
Inherited from
$_createError()
$_createError(
code: string,
value: any,
context: ValidationContext,
state: State,
prefs: ValidationOptions,
options?: CreateErrorOptions): Err;Creates a joi error object.
Parameters
| Parameter | Type |
|---|---|
code | string |
value | any |
context | ValidationContext |
state | State |
prefs | ValidationOptions |
options? | CreateErrorOptions |
Returns
Inherited from
$_getFlag()
$_getFlag(name: string): any;Get value from given flag.
Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
any
Inherited from
$_getRule()
$_getRule(name: string): GetRuleOptions | undefined;Retrieve some rule configuration.
Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
GetRuleOptions | undefined
Inherited from
$_mapLabels()
$_mapLabels(path: string | string[]): string;Parameters
| Parameter | Type |
|---|---|
path | string | string[] |
Returns
string
Inherited from
$_match()
$_match(
value: any,
state: State,
prefs: ValidationOptions): boolean;Returns true if validations runs fine on given value.
Parameters
| Parameter | Type |
|---|---|
value | any |
state | State |
prefs | ValidationOptions |
Returns
boolean
Inherited from
$_modify()
$_modify(options?: ModifyOptions): Schema;Parameters
| Parameter | Type |
|---|---|
options? | ModifyOptions |
Returns
Inherited from
$_mutateRebuild()
$_mutateRebuild(): this;Resets current schema.
Returns
this
Inherited from
$_mutateRegister()
$_mutateRegister(schema: Schema, options?: MutateRegisterOptions): void;Parameters
| Parameter | Type |
|---|---|
schema | Schema |
options? | MutateRegisterOptions |
Returns
void
Inherited from
SchemaInternals.$_mutateRegister
$_property()
$_property(name: string): any;Get value from given property.
Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
any
Inherited from
$_reach()
$_reach(path: string[]): Schema;Get schema at given path.
Parameters
| Parameter | Type |
|---|---|
path | string[] |
Returns
Inherited from
$_rootReferences()
$_rootReferences(): any;Get current schema root references.
Returns
any
Inherited from
SchemaInternals.$_rootReferences
$_setFlag()
$_setFlag(
flag: string,
value: any,
options?: SetFlagOptions): void;Set flag to given value.
Parameters
| Parameter | Type |
|---|---|
flag | string |
value | any |
options? | SetFlagOptions |
Returns
void
Inherited from
$_validate()
$_validate(
value: any,
state: State,
prefs: ValidationOptions): ValidationResult;Runs internal validations against given value.
Parameters
| Parameter | Type |
|---|---|
value | any |
state | State |
prefs | ValidationOptions |
Returns
Inherited from
allow()
allow(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
alter()
alter(targets: Record<string, (schema: this) => Schema>): this;Parameters
| Parameter | Type |
|---|---|
targets | Record<string, (schema: this) => Schema> |
Returns
this
Inherited from
artifact()
artifact(id: any): this;Parameters
| Parameter | Type |
|---|---|
id | any |
Returns
this
Inherited from
bind()
bind(): this;Returns
this
Inherited from
cache()
cache(cache?: Cache): this;Parameters
| Parameter | Type |
|---|---|
cache? | Cache |
Returns
this
Inherited from
cast()
cast(to: "string" | "number" | "object" | "map" | "set"): this;Parameters
| Parameter | Type |
|---|---|
to | "string" | "number" | "object" | "map" | "set" |
Returns
this
Inherited from
concat()
concat(schema: this): this;Parameters
| Parameter | Type |
|---|---|
schema | this |
Returns
this
Inherited from
custom()
custom(fn: CustomValidator, description?: string): this;Parameters
| Parameter | Type |
|---|---|
fn | CustomValidator |
description? | string |
Returns
this
Inherited from
db()
db(connection: any): this;Alias for .knex(). Sets the database connection for database validation rules.
Parameters
| Parameter | Type | Description |
|---|---|---|
connection | any | A Knex instance, transaction, or connection configuration |
Returns
this
Inherited from
default()
default(value?: DefaultableValue): this;Sets a default value if the original value is undefined.
Parameters
| Parameter | Type | Description |
|---|---|---|
value? | DefaultableValue | The default value. Can be: - A literal array value - A reference to another schema value - A function that returns the default value with signature (parent, helpers) => value - parent - A clone of the object containing the value being validated - helpers - Validation helper functions for custom validation logic |
Returns
this
Overrides
describe()
describe(): Description;Returns a plain object representing the schema's rules and properties
Returns
Inherited from
description()
description(desc: string): this;Parameters
| Parameter | Type |
|---|---|
desc | string |
Returns
this
Inherited from
disallow()
disallow(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
empty()
empty(schema?: SchemaLike): this;Parameters
| Parameter | Type |
|---|---|
schema? | SchemaLike |
Returns
this
Inherited from
equal()
equal(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
error()
error(err:
| Error
| ValidationErrorFunction): this;Parameters
| Parameter | Type |
|---|---|
err | | Error | ValidationErrorFunction |
Returns
this
Inherited from
example()
example(value: any, options?: {
override: boolean;
}): this;Parameters
| Parameter | Type |
|---|---|
value | any |
options? | { override: boolean; } |
options.override? | boolean |
Returns
this
Inherited from
exist()
exist(): this;Returns
this
Inherited from
existsInDb()
existsInDb(
table: string | Reference,
column: string | Reference,
options?: DbValidationOptions): this;Validates that the value exists in the specified database table and column. Requires .knex() or .db() to be called first to set the database connection.
Parameters
| Parameter | Type | Description |
|---|---|---|
table | string | Reference | The database table name (can be a Joi reference) |
column | string | Reference | The column name to check (can be a Joi reference) |
options? | DbValidationOptions | Optional configuration: - caseInsensitive: Perform case-insensitive comparison (default: false) - filter: Async function to add additional WHERE clauses to the query |
Returns
this
Example
const schema = joi.object({
country_id: joi.number().knex(db).existsInDb("countries", "id"),
category: joi.string().knex(db).existsInDb("categories", "name", {
caseInsensitive: true,
}),
});Inherited from
external()
external(method: ExternalValidationFunction, description?: string): this;Parameters
| Parameter | Type |
|---|---|
method | ExternalValidationFunction |
description? | string |
Returns
this
Inherited from
extract()
extract(path: string | string[]): Schema;Returns a sub-schema based on a path of object keys or schema ids.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | string[] | 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). |
Returns
Inherited from
failover()
failover(value: any): this;Parameters
| Parameter | Type |
|---|---|
value | any |
Returns
this
Inherited from
forbidden()
forbidden(): this;Returns
this
Inherited from
fork()
fork(key: string | string[] | string[][], adjuster: SchemaFunction): this;Parameters
| Parameter | Type |
|---|---|
key | string | string[] | string[][] |
adjuster | SchemaFunction |
Returns
this
Inherited from
has()
has(schema: SchemaLike): this;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.
Parameters
| Parameter | Type |
|---|---|
schema | SchemaLike |
Returns
this
id()
id(name?: string): this;Parameters
| Parameter | Type |
|---|---|
name? | string |
Returns
this
Inherited from
invalid()
invalid(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
isAsync()
isAsync(): boolean;Returns a boolean indicating whether this schema contains a rule that requires asynchronous validation.
Returns
boolean
Inherited from
items()
items(...types: SchemaLikeWithoutArray[]): this;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.
Parameters
| Parameter | Type |
|---|---|
...types | SchemaLikeWithoutArray[] |
Returns
this
keep()
keep(): this;Returns
this
Inherited from
knex()
knex(connection: any): this;Sets the database connection for database validation rules. This must be called before using .uniqueInDb() or .existsInDb().
Parameters
| Parameter | Type | Description |
|---|---|---|
connection | any | A Knex instance, transaction, or connection configuration |
Returns
this
Example
import knex from 'knex'
const db = knex({ client: 'pg', connection: {...} })
const schema = joi.string().knex(db).uniqueInDb('users', 'email')Inherited from
label()
label(name: string): this;Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
this
Inherited from
length()
length(limit: number | Reference): this;Specifies the exact number of items in the array.
Parameters
| Parameter | Type |
|---|---|
limit | number | Reference |
Returns
this
max()
max(limit: number | Reference): this;Specifies the maximum number of items in the array.
Parameters
| Parameter | Type |
|---|---|
limit | number | Reference |
Returns
this
message()
message(message: string): this;Parameters
| Parameter | Type |
|---|---|
message | string |
Returns
this
Inherited from
messages()
messages(messages: LanguageMessages): this;Parameters
| Parameter | Type |
|---|---|
messages | LanguageMessages |
Returns
this
Inherited from
meta()
meta(meta: object): this;Parameters
| Parameter | Type |
|---|---|
meta | object |
Returns
this
Inherited from
min()
min(limit: number | Reference): this;Specifies the minimum number of items in the array.
Parameters
| Parameter | Type |
|---|---|
limit | number | Reference |
Returns
this
not()
not(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
note()
note(...notes: string[]): this;Parameters
| Parameter | Type |
|---|---|
...notes | string[] |
Returns
this
Inherited from
only()
only(): this;Returns
this
Inherited from
optional()
optional(): this;Returns
this
Inherited from
options()
options(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
ordered()
ordered(...types: SchemaLikeWithoutArray[]): this;Lists the types in sequence order for the array values where:
Parameters
| Parameter | Type |
|---|---|
...types | SchemaLikeWithoutArray[] |
Returns
this
preferences()
preferences(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
prefs()
prefs(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
presence()
presence(mode: PresenceMode): this;Parameters
| Parameter | Type |
|---|---|
mode | PresenceMode |
Returns
this
Inherited from
raw()
raw(enabled?: boolean): this;Parameters
| Parameter | Type |
|---|---|
enabled? | boolean |
Returns
this
Inherited from
required()
required(): this;Returns
this
Inherited from
rule()
rule(options: RuleOptions): this;Parameters
| Parameter | Type |
|---|---|
options | RuleOptions |
Returns
this
Inherited from
shared()
shared(ref: Schema): this;Parameters
| Parameter | Type |
|---|---|
ref | Schema |
Returns
this
Inherited from
single()
single(enabled?: any): this;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.
Parameters
| Parameter | Type |
|---|---|
enabled? | any |
Returns
this
sort()
sort(options?: ArraySortOptions): this;Sorts the array by given order.
Parameters
| Parameter | Type |
|---|---|
options? | ArraySortOptions |
Returns
this
sparse()
sparse(enabled?: any): this;Allow this array to be sparse. enabled can be used with a falsy value to go back to the default behavior.
Parameters
| Parameter | Type |
|---|---|
enabled? | any |
Returns
this
strict()
strict(isStrict?: boolean): this;Parameters
| Parameter | Type |
|---|---|
isStrict? | boolean |
Returns
this
Inherited from
strip()
strip(enabled?: boolean): this;Parameters
| Parameter | Type |
|---|---|
enabled? | boolean |
Returns
this
Inherited from
tag()
tag(...tags: string[]): this;Parameters
| Parameter | Type |
|---|---|
...tags | string[] |
Returns
this
Inherited from
tailor()
tailor(targets: string | string[]): Schema;Applies any assigned target alterations to a copy of the schema that were applied via any.alter().
Parameters
| Parameter | Type |
|---|---|
targets | string | string[] |
Returns
Inherited from
unique()
unique(comparator?: string | ComparatorFunction, options?: ArrayUniqueOptions): this;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.
Parameters
| Parameter | Type |
|---|---|
comparator? | string | ComparatorFunction |
options? | ArrayUniqueOptions |
Returns
this
uniqueInDb()
uniqueInDb(
table: string | Reference,
column: string | Reference,
options?: DbValidationOptions): this;Validates that the value is unique in the specified database table and column. Requires .knex() or .db() to be called first to set the database connection.
Parameters
| Parameter | Type | Description |
|---|---|---|
table | string | Reference | The database table name (can be a Joi reference) |
column | string | Reference | The column name to check (can be a Joi reference) |
options? | DbValidationOptions | Optional configuration: - caseInsensitive: Perform case-insensitive comparison (default: false) - filter: Async function to add additional WHERE clauses to the query |
Returns
this
Example
const schema = joi.object({
email: joi.string().email().knex(db).uniqueInDb("users", "email"),
username: joi
.string()
.knex(db)
.uniqueInDb("users", "username", {
caseInsensitive: true,
filter: async (query) => query.where("tenant_id", tenantId),
}),
});Inherited from
unit()
unit(name: string): this;Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
this
Inherited from
valid()
valid(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
validate()
validate(value: any, options?: ValidationOptions): ValidationResult<TSchema>;Validates a value using the schema and options.
Parameters
| Parameter | Type |
|---|---|
value | any |
options? | ValidationOptions |
Returns
ValidationResult<TSchema>
Inherited from
validateAsync()
validateAsync<TOpts>(value: any, options?: TOpts): Promise<TOpts extends
| {
artifacts: true;
}
| {
warnings: true;
} ? {
value: TSchema;
} & TOpts<TOpts> extends {
artifacts: true;
} ? {
artifacts: Map<any, string[][]>;
} : {
} & TOpts<TOpts> extends {
warnings: true;
} ? {
warning: ValidationWarning;
} : {
} : TSchema>;Validates a value using the schema and options.
Type Parameters
| Type Parameter |
|---|
TOpts extends AsyncValidationOptions |
Parameters
| Parameter | Type |
|---|---|
value | any |
options? | TOpts |
Returns
Promise<TOpts extends | { artifacts: true; } | { warnings: true; } ? { value: TSchema; } & TOpts<TOpts> extends { artifacts: true; } ? { artifacts: Map<any, string[][]>; } : { } & TOpts<TOpts> extends { warnings: true; } ? { warning: ValidationWarning; } : { } : TSchema>
Inherited from
warn()
warn(): this;Returns
this
Inherited from
warning()
warning(code: string, context: ValidationContext): this;Parameters
| Parameter | Type |
|---|---|
code | string |
context | ValidationContext |
Returns
this
Inherited from
when()
Call Signature
when(ref: string | Reference, options:
| WhenOptions<any, any>
| WhenOptions<any, any>[]): this;Parameters
| Parameter | Type |
|---|---|
ref | string | Reference |
options | | WhenOptions<any, any> | WhenOptions<any, any>[] |
Returns
this
Inherited from
Call Signature
when(ref: Schema, options: WhenSchemaOptions): this;Parameters
| Parameter | Type |
|---|---|
ref | Schema |
options | WhenSchemaOptions |
Returns
this