Interface: PhoneSchema<TSchema>
Phone number validation schema interface extending AnySchema. Provides methods for validating and formatting phone numbers with various constraints.
Example
import Joi from "joi";
import { phone } from "./phone";
const extended = Joi.extend(phone);
const schema = extended.phone().country("US").mobile();
const result = schema.validate("+1234567890");Extends
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TSchema | string | The schema type, defaults to string |
Properties
| Property | Modifier | Type | Description | Inherited from |
|---|---|---|---|---|
_flags | public | Record<string, any> | Flags of current schema. | Omit._flags |
~standard | readonly | Props<TSchema, TSchema> | The Standard Schema properties. | Omit.~standard |
$ | public | AnySchema<TSchema> | Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called. | Omit.$ |
$_knex | public | any | - | DatetimeSchema.$_knex |
$_super | public | Schema | Parent schema object. | DatetimeSchema.$_super |
$_terms | public | Record<string, any> | Terms of current schema. | SchemaInternals.$_terms |
ruleset | public | AnySchema<TSchema> | Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called. | Omit.ruleset |
type? | public | string | - | Omit.type |
Methods
$_addRule()
$_addRule(rule: string | AddRuleOptions): Schema;Adds a rule to current validation schema.
Parameters
| Parameter | Type |
|---|---|
rule | string | AddRuleOptions |
Returns
Inherited from
Omit.$_addRule;$_compile()
$_compile(schema: SchemaLike, options?: CompileOptions): Schema;Internally compiles schema.
Parameters
| Parameter | Type |
|---|---|
schema | SchemaLike |
options? | CompileOptions |
Returns
Inherited from
Omit.$_compile;$_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
Omit.$_getRule;$_mapLabels()
$_mapLabels(path: string | string[]): string;Parameters
| Parameter | Type |
|---|---|
path | string | string[] |
Returns
string
Inherited from
Omit.$_mapLabels;$_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
Omit.$_modify;$_mutateRebuild()
$_mutateRebuild(): this;Resets current schema.
Returns
this
Inherited from
Omit.$_mutateRebuild;$_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
Omit.$_reach;$_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
Omit.allow;alter()
alter(targets: Record<string, (schema: this) => Schema>): this;Parameters
| Parameter | Type |
|---|---|
targets | Record<string, (schema: this) => Schema> |
Returns
this
Inherited from
Omit.alter;artifact()
artifact(id: any): this;Parameters
| Parameter | Type |
|---|---|
id | any |
Returns
this
Inherited from
Omit.artifact;bind()
bind(): this;Returns
this
Inherited from
Omit.bind;cache()
cache(cache?: Cache): this;Parameters
| Parameter | Type |
|---|---|
cache? | Cache |
Returns
this
Inherited from
Omit.cache;cast()
cast(to: "string" | "number" | "object"): this;Parameters
| Parameter | Type |
|---|---|
to | "string" | "number" | "object" |
Returns
this
concat()
concat(schema: this): this;Parameters
| Parameter | Type |
|---|---|
schema | this |
Returns
this
Inherited from
Omit.concat;country()
country(country:
| CountryOrUnknown
| Reference
| null): this;Sets the country context for phone number validation.
Parameters
| Parameter | Type | Description |
|---|---|---|
country | | CountryOrUnknown | Reference | null | Country code (ISO 3166-1 alpha-2), country name, Joi reference, or null |
Returns
this
The schema instance for chaining
Example
schema.country("US");
schema.country("United States");
schema.country(Joi.ref("countryField"));custom()
custom(fn: CustomValidator, description?: string): this;Parameters
| Parameter | Type |
|---|---|
fn | CustomValidator |
description? | string |
Returns
this
Inherited from
Omit.custom;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
Omit.db;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 value (string, number, object, etc.) - 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
Example
const schema = joi.string().default("hello world");
const schemaWithFunction = joi
.number()
.default((parent, helpers) => parent.someOtherField * 2);Inherited from
Omit.default;describe()
describe(): Description;Returns a plain object representing the schema's rules and properties
Returns
Inherited from
Omit.describe;description()
description(desc: string): this;Parameters
| Parameter | Type |
|---|---|
desc | string |
Returns
this
Inherited from
Omit.description;disallow()
disallow(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
Omit.disallow;empty()
empty(schema?: SchemaLike): this;Parameters
| Parameter | Type |
|---|---|
schema? | SchemaLike |
Returns
this
Inherited from
Omit.empty;equal()
equal(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
Omit.equal;error()
error(err:
| Error
| ValidationErrorFunction): this;Parameters
| Parameter | Type |
|---|---|
err | | Error | ValidationErrorFunction |
Returns
this
Inherited from
Omit.error;example()
example(value: any, options?: {
override: boolean;
}): this;Parameters
| Parameter | Type |
|---|---|
value | any |
options? | { override: boolean; } |
options.override? | boolean |
Returns
this
Inherited from
Omit.example;exist()
exist(): this;Returns
this
Inherited from
Omit.exist;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
Omit.existsInDb;external()
external(method: ExternalValidationFunction, description?: string): this;Parameters
| Parameter | Type |
|---|---|
method | ExternalValidationFunction |
description? | string |
Returns
this
Inherited from
Omit.external;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
Omit.extract;failover()
failover(value: any): this;Parameters
| Parameter | Type |
|---|---|
value | any |
Returns
this
Inherited from
Omit.failover;fixedLine()
fixedLine(): this;Validates that the phone number is a fixed line or fixed line/mobile number.
Returns
this
The schema instance for chaining
fixedLineOrMobile()
fixedLineOrMobile(): this;Validates that the phone number is either a fixed line or mobile number.
Returns
this
The schema instance for chaining
forbidden()
forbidden(): this;Returns
this
Inherited from
Omit.forbidden;fork()
fork(key: string | string[] | string[][], adjuster: SchemaFunction): this;Parameters
| Parameter | Type |
|---|---|
key | string | string[] | string[][] |
adjuster | SchemaFunction |
Returns
this
Inherited from
Omit.fork;format()
format(as:
| "raw"
| "type"
| "e164"
| "international"
| "national"
| "timezone"
| "country"): this;Sets the output format for the phone number.
Parameters
| Parameter | Type | Description |
|---|---|---|
as | | "raw" | "type" | "e164" | "international" | "national" | "timezone" | "country" | The desired output format |
Returns
this
The schema instance for chaining
Example
schema.format("international"); // +1 234 567 8900
schema.format("national"); // (234) 567-8900
schema.format("e164"); // +12345678900id()
id(name?: string): this;Parameters
| Parameter | Type |
|---|---|
name? | string |
Returns
this
Inherited from
Omit.id;invalid()
invalid(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
Omit.invalid;isAsync()
isAsync(): boolean;Returns a boolean indicating whether this schema contains a rule that requires asynchronous validation.
Returns
boolean
Inherited from
Omit.isAsync;keep()
keep(): this;Returns
this
Inherited from
Omit.keep;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
Omit.knex;label()
label(name: string): this;Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
this
Inherited from
Omit.label;message()
message(message: string): this;Parameters
| Parameter | Type |
|---|---|
message | string |
Returns
this
Inherited from
Omit.message;messages()
messages(messages: LanguageMessages): this;Parameters
| Parameter | Type |
|---|---|
messages | LanguageMessages |
Returns
this
Inherited from
Omit.messages;meta()
meta(meta: object): this;Parameters
| Parameter | Type |
|---|---|
meta | object |
Returns
this
Inherited from
Omit.meta;mobile()
mobile(): this;Validates that the phone number is a mobile or fixed line/mobile number.
Returns
this
The schema instance for chaining
not()
not(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
Omit.not;note()
note(...notes: string[]): this;Parameters
| Parameter | Type |
|---|---|
...notes | string[] |
Returns
this
Inherited from
Omit.note;only()
only(): this;Returns
this
Inherited from
Omit.only;optional()
optional(): this;Returns
this
Inherited from
Omit.optional;options()
options(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
Omit.options;pager()
pager(): this;Validates that the phone number is a pager number.
Returns
this
The schema instance for chaining
personalNumber()
personalNumber(): this;Validates that the phone number is a personal number.
Returns
this
The schema instance for chaining
preferences()
preferences(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
Omit.preferences;prefs()
prefs(options: ValidationOptions): this;Parameters
| Parameter | Type |
|---|---|
options | ValidationOptions |
Returns
this
Inherited from
Omit.prefs;premiumRate()
premiumRate(): this;Validates that the phone number is a premium rate number.
Returns
this
The schema instance for chaining
presence()
presence(mode: PresenceMode): this;Parameters
| Parameter | Type |
|---|---|
mode | PresenceMode |
Returns
this
Inherited from
Omit.presence;raw()
raw(enabled?: boolean): this;Parameters
| Parameter | Type |
|---|---|
enabled? | boolean |
Returns
this
Inherited from
Omit.raw;required()
required(): this;Returns
this
Inherited from
Omit.required;rule()
rule(options: RuleOptions): this;Parameters
| Parameter | Type |
|---|---|
options | RuleOptions |
Returns
this
Inherited from
Omit.rule;shared()
shared(ref: Schema): this;Parameters
| Parameter | Type |
|---|---|
ref | Schema |
Returns
this
Inherited from
Omit.shared;sharedCost()
sharedCost(): this;Validates that the phone number is a shared cost number.
Returns
this
The schema instance for chaining
strict()
strict(isStrict?: boolean): this;Parameters
| Parameter | Type |
|---|---|
isStrict? | boolean |
Returns
this
Inherited from
Omit.strict;strictFixedLine()
strictFixedLine(): this;Validates that the phone number is strictly a fixed line number only.
Returns
this
The schema instance for chaining
strictMobile()
strictMobile(): this;Validates that the phone number is strictly a mobile number only.
Returns
this
The schema instance for chaining
strip()
strip(enabled?: boolean): this;Parameters
| Parameter | Type |
|---|---|
enabled? | boolean |
Returns
this
Inherited from
Omit.strip;tag()
tag(...tags: string[]): this;Parameters
| Parameter | Type |
|---|---|
...tags | string[] |
Returns
this
Inherited from
Omit.tag;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
Omit.tailor;tollFree()
tollFree(): this;Validates that the phone number is a toll-free number.
Returns
this
The schema instance for chaining
types()
types(...types: (
| "FIXED_LINE"
| "MOBILE"
| "FIXED_LINE_OR_MOBILE"
| "TOLL_FREE"
| "PREMIUM_RATE"
| "SHARED_COST"
| "VOIP"
| "PERSONAL_NUMBER"
| "PAGER"
| "UAN"
| "VOICEMAIL"
| "UNKNOWN"
| "INVALID")[]): this;Validates that the phone number matches one of the specified types.
Parameters
| Parameter | Type | Description |
|---|---|---|
...types | ( | "FIXED_LINE" | "MOBILE" | "FIXED_LINE_OR_MOBILE" | "TOLL_FREE" | "PREMIUM_RATE" | "SHARED_COST" | "VOIP" | "PERSONAL_NUMBER" | "PAGER" | "UAN" | "VOICEMAIL" | "UNKNOWN" | "INVALID")[] | Array of phone types to allow |
Returns
this
The schema instance for chaining
Example
schema.types("MOBILE", "FIXED_LINE");uan()
uan(): this;Validates that the phone number is a UAN (Universal Access Number).
Returns
this
The schema instance for chaining
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
Omit.uniqueInDb;unit()
unit(name: string): this;Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
this
Inherited from
Omit.unit;unknown()
unknown(): this;Validates that the phone number is of unknown type.
Returns
this
The schema instance for chaining
valid()
valid(...values: any[]): this;Parameters
| Parameter | Type |
|---|---|
...values | any[] |
Returns
this
Inherited from
Omit.valid;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
Omit.validate;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
Omit.validateAsync;voicemail()
voicemail(): this;Validates that the phone number is a voicemail number.
Returns
this
The schema instance for chaining
voip()
voip(): this;Validates that the phone number is a VoIP number.
Returns
this
The schema instance for chaining
warn()
warn(): this;Returns
this
Inherited from
Omit.warn;warning()
warning(code: string, context: ValidationContext): this;Parameters
| Parameter | Type |
|---|---|
code | string |
context | ValidationContext |
Returns
this
Inherited from
Omit.warning;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
Omit.when;Call Signature
when(ref: Schema, options: WhenSchemaOptions): this;Parameters
| Parameter | Type |
|---|---|
ref | Schema |
options | WhenSchemaOptions |
Returns
this
Inherited from
Omit.when;