Class DateSchemaBuilder<TResult, TRequired>

Allows to create Date schema. It can be required or optional. It can be restricted to be: equal to a certain value, in future, in past, in a certain range. Supports parsing from JSON string and UNIX epoch (using preprocessors).

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 date = new Date(2020, 0, 2);
const schema = date().min(new Date(2020, 0, 1));
const result = await schema.validate(date);
// result.valid === true
// result.object === date

Example

const schema = date();
const result = await schema.validate('2020-01-01');
// result.valid === false
// result.errors[0].message === 'is expected to be a date'

Example

const schema = date().parseFromJson();
const result = await schema.validate('2020-01-01T00:00:00.000Z');
// result.valid === true
// result.object is equal to corresponding Date object

Example

const schema = date().parseFromEpoch();
const result = await schema.validate(1577836800000);
// result.valid === true
// result.object is equal to corresponding Date object

See

date

Type Parameters

  • TResult = Date

  • 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

  • Generates a serializable object describing the defined schema

    Returns {
        ensureIsInFuture: boolean;
        ensureIsInPast: boolean;
        equalsTo: undefined | Date;
        isRequired: boolean;
        max: undefined | Date;
        min: undefined | Date;
        parseFromEpoch: boolean;
        parseFromJson: boolean;
        preprocessors: Preprocessor<TResult>[];
        type: string;
        validators: Validator<TResult>[];
    }

    • ensureIsInFuture: boolean

      Make sure that date is in future. false by default.

    • ensureIsInPast: boolean

      Make sure that date is in past. false by default.

    • equalsTo: undefined | Date

      If set, restrict date 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).

    • max: undefined | Date

      Max valid value (if defined).

    • min: undefined | Date

      Min valid value (if defined).

    • parseFromEpoch: boolean

      If set, schema will try to parse date from the UNIX epoch (number). false by default.

    • parseFromJson: boolean

      If set, schema will try to parse date from JSON string. false by default.

    • preprocessors: Preprocessor<TResult>[]

      Array of preprocessor functions

    • 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<{
          ensureIsInFuture: boolean;
          ensureIsInPast: boolean;
          equalsTo: undefined | Date;
          isRequired: boolean;
          max: undefined | Date;
          min: undefined | Date;
          parseFromEpoch: boolean;
          parseFromJson: boolean;
          preprocessors: Preprocessor<Date>[];
          type: string;
          validators: Validator<Date>[];
      }>

    Returns DateSchemaBuilder<Date, true>

Generated using TypeDoc