@bedrock-oss/bedrock-boost
    Preparing search index...

    Interface Schema<T>

    Type-safe runtime validator and transformer for a value of type T.

    interface Schema<T> {
        assert(value: unknown): asserts value is T;
        nullable(): Schema<null | T>;
        optional(): Schema<undefined | T>;
        parse(value: unknown): T;
        refine(
            pred: (v: T) => boolean,
            message: string,
            code?: ValidationIssueCode,
        ): Schema<T>;
        required(): this;
        safeParse(value: unknown): SafeParseResult<T>;
    }

    Type Parameters

    • T
    Index

    Methods

    • Validates the value in place, throwing if it does not satisfy the schema.

      Parameters

      • value: unknown

        Raw input to validate.

      Returns asserts value is T

      ValidationError Thrown when validation fails.

    • Allows null in addition to the current schema.

      Returns Schema<null | T>

      A schema permitting null.

    • Allows undefined in addition to the current schema.

      Returns Schema<undefined | T>

      A schema permitting undefined.

    • Validates and returns a value when all schema constraints pass.

      Parameters

      • value: unknown

        Raw input to validate.

      Returns T

      The parsed value if validation succeeds.

      ValidationError Thrown when validation fails.

    • Adds a custom predicate to refine acceptable values.

      Parameters

      • pred: (v: T) => boolean

        Predicate returning true when the value is valid.

      • message: string

        Human friendly description shown on failure.

      • Optionalcode: ValidationIssueCode

        Optional machine readable identifier for the failure.

      Returns Schema<T>

      A schema reflecting the additional refinement.

    • Removes undefined from the accepted set of values.

      Returns this

      The current schema with undefined disallowed.

    • Attempts to parse a value without throwing.

      Parameters

      • value: unknown

        Raw input to validate.

      Returns SafeParseResult<T>

      Success with parsed data or failure with validation issues.