Libraries (v1.1.10)
    Preparing search index...

    Class SchemaBuilder<TResult, TRequired>Abstract

    Base class for all schema builders. Provides basic functionality for schema building.

    Note: this class is not intended to be used directly, use one of the subclasses instead.

    TResult Type of the object that will be returned by validate() method.

    TRequired If true, object will be required. If false, object will be optional.

    Type Parameters

    • TResult = any
    • TRequired extends boolean = true

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    • get type(): string

      The string identifier of the schema type (e.g. 'string', 'number', 'object').

      Returns string

    • set type(value: string): void

      Sets the schema type identifier. Must be a non-empty string.

      Parameters

      • value: string

      Returns void

    Methods

    • Adds a preprocessor to a preprocessors list

      Parameters

      • preprocessor: Preprocessor<TResult>

      Returns this

    • Ensures a ValidationErrorMessageProvider is valid. If provider is undefined, falls back to defaultValue. Function providers are bound to this for access to schema state.

      Parameters

      • provider: ValidationErrorMessageProvider<any> | undefined

        the provider to validate, or undefined

      • defaultValue: ValidationErrorMessageProvider<any>

        fallback provider when provider is not supplied

      Returns ValidationErrorMessageProvider<any>

      a valid ValidationErrorMessageProvider

    • Clears type set by call to .hasType<T>(), default schema type inference will be used for schema returned by this call.

      Returns any

    • Remove all preprocessors for this schema.

      Returns this

    • Remove all validators for this schema.

      Returns this

    • Protected method used to create a new instance of the Builder defined by the props object. Should be used to instantiate new builders to keep builder's immutability.

      Parameters

      • props: any

        arbitrary props object

      Returns this

    • Resolves a ValidationErrorMessageProvider to a string error message. Handles both string providers and function providers (sync or async).

      Parameters

      • provider: ValidationErrorMessageProvider<any>

        the error message provider (string or function)

      • seenValue: TResult

        the value that caused the validation error

      Returns Promise<string>

      the resolved error message string

    • Set type of schema explicitly. notUsed param is needed only for case when JS is used. E.g. when you can't call method like schema.hasType<Date>(), so instead you can call schema.hasType(new Date()) with the same result.

      Type Parameters

      • T

      Parameters

      • OptionalnotUsed: T

      Returns any

    • Generates a serializable object describing the defined schema

      Returns {
          isRequired: boolean;
          preprocessors: readonly Preprocessor<TResult>[];
          requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<
              SchemaBuilder<any, any>,
          >;
          type: string;
          validators: readonly Validator<TResult>[];
      }

      • isRequired: boolean

        If set to false, schema will be optional (null or undefined values will be considered as valid).

      • preprocessors: readonly Preprocessor<TResult>[]

        Array of preprocessor functions

      • requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any>>

        Custom error message provider for the 'is required' validation error.

      • type: string

        String id of schema type, e.g. string', numberorobject`.

      • validators: readonly Validator<TResult>[]

        Array of validator functions

    • Makes schema optional (consider null and undefined as valid objects for this schema)

      Returns any

    • Runs preprocessors, validators, and the required/optional check on object. Subclasses call this at the start of their validate() implementation to get a preprocessed value wrapped in a transaction, along with any early errors.

      Parameters

      • object: any

        the value to pre-validate

      • Optionalcontext: ValidationContext<SchemaBuilder<any, any>>

        optional validation context settings

      Returns Promise<PreValidationResult<any, { validatedObject: any }>>

      a PreValidationResult containing the preprocessed transaction, context, and any errors

    • Makes schema required (consider null and undefined as invalid objects for this schema)

      Parameters

      • OptionalerrorMessage: ValidationErrorMessageProvider<SchemaBuilder<any, any>>

        optional custom error message or provider for the 'is required' validation error

      Returns any