Function array

  • The array schema checks that the type of the value is an array, and that all of its elements match the given schema.

    Example

    import { array, string } from "typerun/schema";
    import { is } from "typerun";

    const schema = array(string);
    if (is(schema)(["hello", "world"])) {
    console.log("value is an array of strings");
    }
    if (!is(schema)(["hello", 42])) {
    console.log("value is not an array of strings");
    }

    If you use the validate function, the errors contain all of the different type validation fails from the array.

    Arrays can be combined with any other schemas, to create complex validation patterns.

    import { array, string, number } from "typerun/schema";

    const schema = array(either(string, array(number)));
    if (is(schema)(["hello", [42, 31]])) {
    console.log("value is an array of strings and arrays of numbers");
    }

    Type Parameters

    • S

    Parameters

    • schema: Schema<S>

      The schema of the elements of the array to check.

    Returns Schema<S[]>