Interface FunctionSchema<TSchema>

interface FunctionSchema {
    $: FunctionSchema<TSchema>;
    $_super: Schema<any>;
    $_terms: Record<string, any>;
    _flags: Record<string, any>;
    ruleset: FunctionSchema<TSchema>;
    type?: string;
    $_addRule(rule): Schema<any>;
    $_compile(schema, options?): Schema<any>;
    $_createError(code, value, context, state, prefs, options?): Err;
    $_getFlag(name): any;
    $_getRule(name): undefined | GetRuleOptions;
    $_mapLabels(path): string;
    $_match(value, state, prefs): boolean;
    $_modify(options?): Schema<any>;
    $_mutateRebuild(): FunctionSchema<TSchema>;
    $_mutateRegister(schema, options?): void;
    $_property(name): any;
    $_reach(path): Schema<any>;
    $_rootReferences(): any;
    $_setFlag(flag, value, options?): void;
    $_validate(value, state, prefs): ValidationResult<any>;
    allow(...values): FunctionSchema<TSchema>;
    alter(targets): FunctionSchema<TSchema>;
    and(...peers): FunctionSchema<TSchema>;
    append(schema?): FunctionSchema<TSchema>;
    append<TSchemaExtended, T>(schema?): ObjectSchema<T>;
    arity(n): FunctionSchema<TSchema>;
    artifact(id): FunctionSchema<TSchema>;
    assert(ref, schema, message?): FunctionSchema<TSchema>;
    bind(): FunctionSchema<TSchema>;
    cache(cache?): FunctionSchema<TSchema>;
    cast(to): FunctionSchema<TSchema>;
    class(): FunctionSchema<TSchema>;
    concat(schema): FunctionSchema<TSchema>;
    custom(fn, description?): FunctionSchema<TSchema>;
    default(value?): FunctionSchema<TSchema>;
    describe(): Description;
    description(desc): FunctionSchema<TSchema>;
    disallow(...values): FunctionSchema<TSchema>;
    empty(schema?): FunctionSchema<TSchema>;
    equal(...values): FunctionSchema<TSchema>;
    error(err): FunctionSchema<TSchema>;
    example(value, options?): FunctionSchema<TSchema>;
    exist(): FunctionSchema<TSchema>;
    external(method, description?): FunctionSchema<TSchema>;
    extract(path): Schema<any>;
    failover(value): FunctionSchema<TSchema>;
    forbidden(): FunctionSchema<TSchema>;
    fork(key, adjuster): FunctionSchema<TSchema>;
    id(name?): FunctionSchema<TSchema>;
    instance(constructor, name?): FunctionSchema<TSchema>;
    invalid(...values): FunctionSchema<TSchema>;
    keep(): FunctionSchema<TSchema>;
    keys(schema?): FunctionSchema<TSchema>;
    label(name): FunctionSchema<TSchema>;
    length(limit): FunctionSchema<TSchema>;
    max(limit): FunctionSchema<TSchema>;
    maxArity(n): FunctionSchema<TSchema>;
    message(message): FunctionSchema<TSchema>;
    messages(messages): FunctionSchema<TSchema>;
    meta(meta): FunctionSchema<TSchema>;
    min(limit): FunctionSchema<TSchema>;
    minArity(n): FunctionSchema<TSchema>;
    nand(...peers): FunctionSchema<TSchema>;
    not(...values): FunctionSchema<TSchema>;
    note(...notes): FunctionSchema<TSchema>;
    only(): FunctionSchema<TSchema>;
    optional(): FunctionSchema<TSchema>;
    options(options): FunctionSchema<TSchema>;
    or(...peers): FunctionSchema<TSchema>;
    oxor(...peers): FunctionSchema<TSchema>;
    pattern(pattern, schema, options?): FunctionSchema<TSchema>;
    preferences(options): FunctionSchema<TSchema>;
    prefs(options): FunctionSchema<TSchema>;
    presence(mode): FunctionSchema<TSchema>;
    raw(enabled?): FunctionSchema<TSchema>;
    ref(): FunctionSchema<TSchema>;
    regex(): FunctionSchema<TSchema>;
    rename(from, to, options?): FunctionSchema<TSchema>;
    required(): FunctionSchema<TSchema>;
    rule(options): FunctionSchema<TSchema>;
    schema(type?): FunctionSchema<TSchema>;
    shared(ref): FunctionSchema<TSchema>;
    strict(isStrict?): FunctionSchema<TSchema>;
    strip(enabled?): FunctionSchema<TSchema>;
    tag(...tags): FunctionSchema<TSchema>;
    tailor(targets): Schema<any>;
    unit(name): FunctionSchema<TSchema>;
    unknown(allow?): FunctionSchema<TSchema>;
    valid(...values): FunctionSchema<TSchema>;
    validate(value, options?): ValidationResult<TSchema>;
    validateAsync<TOpts>(value, options?): Promise<TOpts extends {
            artifacts: true;
        } | {
            warnings: true;
        }
        ? {
            value: TSchema;
        } & (TOpts extends {
                artifacts: true;
            }
            ? {
                artifacts: Map<any, string[][]>;
            }
            : {}) & (TOpts extends {
                warnings: true;
            }
            ? {
                warning: ValidationWarning;
            }
            : {})
        : TSchema>;
    warn(): FunctionSchema<TSchema>;
    warning(code, context): FunctionSchema<TSchema>;
    when(ref, options): FunctionSchema<TSchema>;
    when(ref, options): FunctionSchema<TSchema>;
    with(key, peers, options?): FunctionSchema<TSchema>;
    without(key, peers, options?): FunctionSchema<TSchema>;
    xor(...peers): FunctionSchema<TSchema>;
}

Type Parameters

  • TSchema = Function

Hierarchy

Properties

$: FunctionSchema<TSchema>

Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called.

$_super: Schema<any>

Parent schema object.

$_terms: Record<string, any>

Terms of current schema.

_flags: Record<string, any>

Flags of current schema.

ruleset: FunctionSchema<TSchema>

Starts a ruleset in order to apply multiple rule options. The set ends when rule(), keep(), message(), or warn() is called.

type?: string

Methods

  • Adds a rule to current validation schema.

    Parameters

    • rule: string | AddRuleOptions

    Returns Schema<any>

  • Internally compiles schema.

    Parameters

    • schema: SchemaLike
    • Optional options: CompileOptions

    Returns Schema<any>

  • Creates a joi error object.

    Parameters

    • code: string
    • value: any
    • context: Context
    • state: State
    • prefs: ValidationOptions
    • Optional options: CreateErrorOptions

    Returns Err

  • Get value from given flag.

    Parameters

    • name: string

    Returns any

  • Retrieve some rule configuration.

    Parameters

    • name: string

    Returns undefined | GetRuleOptions

  • Parameters

    • path: string | string[]

    Returns string

  • Returns true if validations runs fine on given value.

    Parameters

    • value: any
    • state: State
    • prefs: ValidationOptions

    Returns boolean

  • Parameters

    • Optional options: ModifyOptions

    Returns Schema<any>

  • Parameters

    • schema: Schema<any>
    • Optional options: MutateRegisterOptions

    Returns void

  • Get value from given property.

    Parameters

    • name: string

    Returns any

  • Get schema at given path.

    Parameters

    • path: string[]

    Returns Schema<any>

  • Get current schema root references.

    Returns any

  • Set flag to given value.

    Parameters

    • flag: string
    • value: any
    • Optional options: SetFlagOptions

    Returns void

  • Runs internal validations against given value.

    Parameters

    • value: any
    • state: State
    • prefs: ValidationOptions

    Returns ValidationResult<any>

  • Assign target alteration options to a schema that are applied when any.tailor() is called.

    Parameters

    • targets: Record<string, ((schema) => Schema<any>)>

      an object where each key is a target name, and each value is a function that takes an schema and returns an schema.

    Returns FunctionSchema<TSchema>

  • Defines an all-or-nothing relationship between keys where if one of the peers is present, all of them are required as well.

    Optional settings must be the last argument.

    Parameters

    • Rest ...peers: (string | DependencyOptions)[]

    Returns FunctionSchema<TSchema>

  • Specifies the arity of the function where:

    Parameters

    • n: number

      the arity expected.

    Returns FunctionSchema<TSchema>

  • Assigns the schema an artifact id which is included in the validation result if the rule passed validation.

    Parameters

    • id: any

      any value other than undefined which will be returned as-is in the result artifacts map.

    Returns FunctionSchema<TSchema>

  • 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.

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • Optional cache: Cache

    Returns FunctionSchema<TSchema>

  • Casts the validated value to the specified type.

    Parameters

    • to: "string" | "number" | "map" | "set"

    Returns FunctionSchema<TSchema>

  • Requires the function to be a class.

    Returns FunctionSchema<TSchema>

  • Adds a custom validation function.

    Parameters

    • fn: CustomValidator<any, any>
    • Optional description: string

    Returns FunctionSchema<TSchema>

  • Sets a default value if the original value is undefined where:

    Parameters

    • Optional value: BasicType | Reference | ((parent, helpers) => BasicType | Reference)

      the default value. One of:

      • a literal value (string, number, object, etc.)
      • a references
      • a function which returns the default value using the signature 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.

    Returns FunctionSchema<TSchema>

  • Returns a plain object representing the schema's rules and properties

    Returns Description

  • Considers anything that matches the schema to be empty (undefined).

    Parameters

    • Optional schema: SchemaLike

      any object or joi schema to match. An undefined schema unsets that rule.

    Returns FunctionSchema<TSchema>

  • Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.

    Parameters

    • Rest ...values: any[]

    Returns FunctionSchema<TSchema>

  • Overrides the default joi error with a custom error if the rule fails where:

    Parameters

    • err: Error | ValidationErrorFunction

      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).

    Returns FunctionSchema<TSchema>

  • Annotates the key with an example value, must be valid.

    Parameters

    • value: any
    • Optional options: {
          override: boolean;
      }
      • override: boolean

    Returns FunctionSchema<TSchema>

  • Marks a key as required which will not allow undefined as value. All keys are optional by default.

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • method: ExternalValidationFunction<any, any>
    • Optional description: string

    Returns FunctionSchema<TSchema>

  • Returns a sub-schema based on a path of object keys or schema ids.

    Parameters

    • 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 Schema<any>

  • Sets a failover value if the original value fails passing validation.

    Parameters

    • value: any

      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 FunctionSchema<TSchema>

  • Marks a key as forbidden which will not allow any value except undefined. Used to explicitly forbid keys.

    Returns FunctionSchema<TSchema>

  • Returns a new schema where each of the path keys listed have been modified.

    Parameters

    • key: string | string[] | string[][]

      an array of key strings, a single key string, or an array of arrays of pre-split key strings.

    • adjuster: SchemaFunction

      a function which must return a modified schema.

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • Optional name: string

    Returns FunctionSchema<TSchema>

  • Requires the object to be an instance of a given constructor.

    Parameters

    • constructor: Function

      the constructor function that the object must be an instance of.

    • Optional name: string

      an alternate name to use in validation errors. This is useful when the constructor function does not have a name.

    Returns FunctionSchema<TSchema>

  • 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.

    Returns FunctionSchema<TSchema>

  • Specifies the maximum number of keys in the object.

    Parameters

    • limit: number | Reference

    Returns FunctionSchema<TSchema>

  • Specifies the minimal arity of the function where:

    Parameters

    • n: number

      the minimal arity expected.

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • message: string

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • messages: LanguageMessages

    Returns FunctionSchema<TSchema>

  • Specifies the minimum number of keys in the object.

    Parameters

    • limit: number | Reference

    Returns FunctionSchema<TSchema>

  • Specifies the minimal arity of the function where:

    Parameters

    • n: number

      the minimal arity expected.

    Returns FunctionSchema<TSchema>

  • Defines a relationship between keys where not all peers can be present at the same time.

    Optional settings must be the last argument.

    Parameters

    • Rest ...peers: (string | DependencyOptions)[]

    Returns FunctionSchema<TSchema>

  • 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().

    Returns FunctionSchema<TSchema>

  • 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.

    Returns FunctionSchema<TSchema>

  • Overrides the global validate() options for the current key and any sub-key.

    Parameters

    • options: ValidationOptions

    Returns FunctionSchema<TSchema>

  • Defines a relationship between keys where one of the peers is required (and more than one is allowed).

    Optional settings must be the last argument.

    Parameters

    • Rest ...peers: (string | DependencyOptions)[]

    Returns FunctionSchema<TSchema>

  • Defines an exclusive relationship between a set of keys where only one is allowed but none are required.

    Optional settings must be the last argument.

    Parameters

    • Rest ...peers: (string | DependencyOptions)[]

    Returns FunctionSchema<TSchema>

  • Specify validation rules for unknown keys matching a pattern.

    Parameters

    • pattern: RegExp | SchemaLike

      a pattern that can be either a regular expression or a joi schema that will be tested against the unknown key names

    • schema: SchemaLike

      the schema object matching keys must validate against

    • Optional options: ObjectPatternOptions

    Returns FunctionSchema<TSchema>

  • Overrides the global validate() options for the current key and any sub-key.

    Parameters

    • options: ValidationOptions

    Returns FunctionSchema<TSchema>

  • Overrides the global validate() options for the current key and any sub-key.

    Parameters

    • options: ValidationOptions

    Returns FunctionSchema<TSchema>

  • Outputs the original untouched value instead of the casted value.

    Parameters

    • Optional enabled: boolean

    Returns FunctionSchema<TSchema>

  • Renames a key to another name (deletes the renamed key).

    Parameters

    • from: string | RegExp
    • to: string
    • Optional options: RenameOptions

    Returns FunctionSchema<TSchema>

  • Marks a key as required which will not allow undefined as value. All keys are optional by default.

    Returns FunctionSchema<TSchema>

  • 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.

    Parameters

    • options: RuleOptions

    Returns FunctionSchema<TSchema>

  • Registers a schema to be used by descendants of the current schema in named link references.

    Parameters

    Returns FunctionSchema<TSchema>

  • Sets the options.convert options to false which prevent type casting for the current key and any child keys.

    Parameters

    • Optional isStrict: boolean

    Returns FunctionSchema<TSchema>

  • Marks a key to be removed from a resulting object or array after validation. Used to sanitize output.

    Parameters

    • Optional enabled: boolean

      if true, the value is stripped, otherwise the validated value is retained. Defaults to true.

    Returns FunctionSchema<TSchema>

  • Applies any assigned target alterations to a copy of the schema that were applied via any.alter().

    Parameters

    • targets: string | string[]

    Returns Schema<any>

  • Overrides the handling of unknown keys for the scope of the current object only (does not apply to children).

    Parameters

    • Optional allow: boolean

    Returns FunctionSchema<TSchema>

  • Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.

    Parameters

    • Rest ...values: any[]

    Returns FunctionSchema<TSchema>

  • Validates a value using the schema and options.

    Parameters

    • value: any
    • Optional options: ValidationOptions

    Returns ValidationResult<TSchema>

  • Validates a value using the schema and options.

    Type Parameters

    • TOpts extends AsyncValidationOptions

    Parameters

    • value: any
    • Optional options: TOpts

    Returns Promise<TOpts extends {
            artifacts: true;
        } | {
            warnings: true;
        }
        ? {
            value: TSchema;
        } & (TOpts extends {
                artifacts: true;
            }
            ? {
                artifacts: Map<any, string[][]>;
            }
            : {}) & (TOpts extends {
                warnings: true;
            }
            ? {
                warning: ValidationWarning;
            }
            : {})
        : TSchema>

  • 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.

    Returns FunctionSchema<TSchema>

  • 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().

    Parameters

    • code: string
    • context: Context

    Returns FunctionSchema<TSchema>

  • Converts the type into an alternatives type where the conditions are merged into the type definition where:

    Parameters

    • ref: string | Reference
    • options: WhenOptions | WhenOptions[]

    Returns FunctionSchema<TSchema>

  • Converts the type into an alternatives type where the conditions are merged into the type definition where:

    Parameters

    • ref: Schema<any>
    • options: WhenSchemaOptions

    Returns FunctionSchema<TSchema>

  • Requires the presence of other keys whenever the specified key is present.

    Parameters

    • key: string
    • peers: string | string[]
    • Optional options: DependencyOptions

    Returns FunctionSchema<TSchema>

  • Forbids the presence of other keys whenever the specified is present.

    Parameters

    • key: string
    • peers: string | string[]
    • Optional options: DependencyOptions

    Returns FunctionSchema<TSchema>

  • Defines an exclusive relationship between a set of keys. one of them is required but not at the same time.

    Optional settings must be the last argument.

    Parameters

    • Rest ...peers: (string | DependencyOptions)[]

    Returns FunctionSchema<TSchema>

Generated using TypeDoc