Function validateRegex

  • Validate an input given a regex

    Type Parameters

    • T extends string

      The validated type

    Parameters

    • regex: RegExp

      The regex

    • reason: string

      The failure explanation

    Returns RegexValidator<T>

    Example

    validateRegex(
    /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/u,
    "Not a yyyy-mm-dd date",
    ) >>
    {
    valid: true,
    input: "1234-12-12",
    parsed: {
    match: "1234-12-12",
    index: 0,
    captured: ["1234", "12", "12"],
    named: { year: "1234", month: "12", day: "12" },
    },
    error: null,
    };