Vertex Viewer SDK
    Preparing search index...

    Namespace Mapper

    A module for defining functional schemas to map between different types. This module is useful for parsing to or from JSON/protobufs to domain types.

    Mappers support greedy validation, so all validation errors are aggregated and reported vs failing on the first invalid input.

    import { Mapper as M } from '@vertexvis/utils';

    interface Address {
    address: string;
    city: string;
    state: string;
    zip: string;
    }

    interface Person {
    name: string;
    addresses: Address[];
    }

    type AddressJson = Partial<Address>;
    type PersonJson = {
    name?: string;
    addresses?: AddressJson[];
    }

    const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
    M.read(
    M.requireProp('address'),
    M.requireProp('city'),
    M.requireProp('state'),
    M.requireProp('zip')
    ),
    ([address, city, state, zip]) => ({
    address, city, state, zip
    })
    );

    const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
    M.read(
    M.requireProp('name'),
    M.mapProp(
    'addresses',
    M.compose(M.required('addresses'), M.mapArray(mapAddress))
    )
    ),
    ([name, addresses]) => ({ name, addresses })
    );

    const person = mapPerson({
    name: 'John',
    addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
    });

    const invalidPerson = mapPerson({
    addresses: [{ city: 'Ames', state: 'IA', zip: '50010' }]
    });

    // { // errors: ["Name is required.", "Address is required."] // }

    MapperValidationError
    Invalid
    Func
    ThrowIfInvalidFunc
    Validated
    compose
    defineMapper
    getProp
    ifDefined
    ifInvalidThrow
    isInvalid
    mapArray
    mapProp
    mapRequiredProp
    pickFirst
    read
    required
    requiredProp