opensensemap-client
    Preparing search index...

    Class Struct<T, S>

    Struct objects encapsulate the validation logic for a specific type of values. Once constructed, you use the assert, is or validate helpers to validate unknown input data against the struct.

    Type Parameters

    • T = unknown
    • S = unknown
    Index

    Constructors

    • Type Parameters

      • T = unknown
      • S = unknown

      Parameters

      • props: {
            coercer?: Coercer<unknown>;
            entries?: (
                value: unknown,
                context: Context,
            ) => Iterable<
                [
                    string
                    | number,
                    unknown,
                    Struct<any, unknown> | Struct<never, unknown>,
                ],
            >;
            refiner?: Refiner<T>;
            schema: S;
            type: string;
            validator?: Validator;
        }

      Returns Struct<T, S>

    Properties

    coercer: (value: unknown, context: Context) => unknown
    entries: (
        value: unknown,
        context: Context,
    ) => Iterable<
        [
            string
            | number,
            unknown,
            Struct<any, unknown> | Struct<never, unknown>,
        ],
    >
    refiner: (value: T, context: Context) => Iterable<Failure>
    schema: S
    type: string
    TYPE: T
    validator: (value: unknown, context: Context) => Iterable<Failure>

    Methods

    • Assert that a value passes the struct's validation, throwing if it doesn't.

      Parameters

      • value: unknown
      • Optionalmessage: string

      Returns asserts value is T

    • Create a value with the struct's coercion logic, then validate it.

      Parameters

      • value: unknown
      • Optionalmessage: string

      Returns T

    • Check if a value passes the struct's validation.

      Parameters

      • value: unknown

      Returns value is T

    • Mask a value, coercing and validating it, but returning only the subset of properties defined by the struct's schema. Masking applies recursively to props of object structs only.

      Parameters

      • value: unknown
      • Optionalmessage: string

      Returns T

    • Validate a value with the struct's validation logic, returning a tuple representing the result.

      You may optionally pass true for the coerce argument to coerce the value before attempting to validate it. If you do, the result will contain the coerced result when successful. Also, mask will turn on masking of the unknown object props recursively if passed.

      Parameters

      • value: unknown
      • Optionaloptions: { coerce?: boolean; mask?: boolean; message?: string }

      Returns [StructError, undefined] | [undefined, T]