Function map

  • The map schema validates that the input is a map of keys and values that match the schemas. It fails for anything that is not a map, or for any key or value of the map that does not match the schemas.

    Example

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

    const mapEx = new Map([
    ["a", 1],
    ["b", 2],
    ]);

    const isMap = is(map(string, number))(mapEx);
    console.log(isMap); // true

    const isNotMap = is(map(string, number))({ a: 1, b: 2 });
    console.log(isNotMap); // false

    const isNotCorrectMapKeys = is(map(number, number))(mapEx);
    console.log(isNotCorrectMapKeys); // false

    const isNotCorrectMapValues = is(map(string, string))(mapEx);
    console.log(isNotCorrectMapValues); // false

    Type Parameters

    • K
    • V

    Parameters

    • keysSchema: Schema<K>

      The schema to validate the keys of the map.

    • valuesSchema: Schema<V>

      The schema to validate the values of the map.

    Returns Schema<Map<K, V>>