Function custom

  • The custom schema allows the user to define its own schema, if it is not present in the library. This schema can then be used like all other primitive schemas (string, number, …).

    Example

    import { custom } from "typerun/schema";
    import { is } from "typerun";

    const correctLength = custom((data): data is string => {
    return typeof data === "string" && data.length > 3 && data.length < 8;
    });

    if (is(correctLength)("hello!")) {
    console.log("correct length");
    }

    Type Parameters

    • T

    Parameters

    • validationFn: ((data) => data is T)

      A function, taking unknown data as the input and returning a boolean if the data matches the wanted type. This function must be a type predicate (declaring its return type as : is T, where T is the validated type).

        • (data): data is T
        • Parameters

          • data: unknown

          Returns data is T

    • Optional errorMessage: string | ((data) => string)

      An optional error message to report if the validation fails. If not provided, the default error message "Value {input} is not correct" will be used in error reports.

    Returns Schema<T>