How to use the decoders.field function in decoders

To help you get started, we’ve selected a few decoders examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nvie / decoders / src / types / typescript-tests.ts View on Github external
positiveNumber,
    predicate,
    regex,
    string,
    truthy,
    tuple4,
    tuple6,
    undefined_,
    field,
    unknown,
    url,
} from 'decoders';

const rect = object({ type: constant('rect') });
const circle = object({ type: constant('circle') });
const shape = dispatch(field('type', string), type => {
    switch (type) {
        case 'rect':
            return rect;
        case 'circle':
            return circle;
    }
    return fail('Must be a valid shape');
});

guard(
    object({
        a0: string,
        a1: number,
        a2: optional(nullable(either4(string, email, regex(/x/, 'Must be x'), url()))),
        a3: fail('foo'),
        a4: maybe(mixed),