Class StringSchemaBuilder<TResult, TRequired>

Allows to define a schema for a string. It can be: required or optional, restricted to be equal to a certain value, restricted to have a certain length, restricted to start with a certain value, restricted to end with a certain value, restricted to match a certain regular expression.

NOTE this class is exported only to give opportunity to extend it by inheriting. It is not recommended to create an instance of this class directly. Use () function instead.

Example

const schema = string().equals('hello');
const result = await schema.validate('hello');
// result.valid === true
// result.object === 'hello'

Example

const schema = string().equals('hello');
const result = await schema.validate('world');
// result.valid === false
// result.errors[0].message === "is expected to be equal to 'hello'"

Example

const schema = string().minLength(5);
const result = await schema.validate('hello');
// result.valid === true
// result.object === 'hello'

Example

const schema = string().minLength(5);
const result = await schema.validate('hi');
// result.valid === false
// result.errors[0].message === 'is expected to have a length of at least 5'

Example

const schema = string().minLength(2).maxLength(5);
const result = await schema.validate('yes');
// result.valid === true
// result.object === 'yes'

Example

const schema = string('no');
const result = await schema.validate('yes');
// result.valid === false
// result.errors[0].message === "is expected to be equal to 'no'"

See

string

Type Parameters

  • TResult = string

  • TRequired extends boolean = true

Hierarchy

Accessors

  • get preprocessors(): Preprocessor<TResult>[]
  • A list of preprocessors associated with the Builder

    Returns Preprocessor<TResult>[]

  • get validators(): Validator<TResult>[]
  • A list of validators associated with the Builder

    Returns Validator<TResult>[]

Methods

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

    Type Parameters

    • T

    • TReq extends boolean

    Parameters

    • props: Partial<{
          endsWith: undefined | string;
          equalsTo: undefined | string;
          isRequired: boolean;
          matches: undefined | RegExp;
          maxLength: undefined | number;
          minLength: undefined | number;
          preprocessors: Preprocessor<T>[];
          startsWith: undefined | string;
          type: string;
          validators: Validator<T>[];
      }>

      arbitrary props object

    Returns StringSchemaBuilder<TResult, TRequired>

  • Generates a serializable object describing the defined schema

    Returns {
        endsWith: undefined | string;
        equalsTo: undefined | string;
        isRequired: boolean;
        matches: undefined | RegExp;
        maxLength: undefined | number;
        minLength: undefined | number;
        preprocessors: Preprocessor<TResult>[];
        startsWith: undefined | string;
        type: string;
        validators: Validator<TResult>[];
    }

    • endsWith: undefined | string

      If set, restrict string to end with a certain value.

    • equalsTo: undefined | string

      If set, restrict object to be equal to a certain value.

    • isRequired: boolean

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

    • matches: undefined | RegExp

      If set, restrict string to match a certain regular expression.

    • maxLength: undefined | number

      Max length of the string (if defined).

    • minLength: undefined | number

      Min length of the string (if defined).

    • preprocessors: Preprocessor<TResult>[]

      Array of preprocessor functions

    • startsWith: undefined | string

      If set, restrict string to start with a certain value.

    • type: string

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

    • validators: Validator<TResult>[]

      Array of validator functions

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

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

  • Parameters

    • props: Partial<{
          endsWith: undefined | string;
          equalsTo: undefined | string;
          isRequired: boolean;
          matches: undefined | RegExp;
          maxLength: undefined | number;
          minLength: undefined | number;
          preprocessors: Preprocessor<string>[];
          startsWith: undefined | string;
          type: string;
          validators: Validator<string>[];
      }>

    Returns StringSchemaBuilder<string, true>

Generated using TypeDoc