decoders

Elegant validation library for type-safe input data (for TypeScript and Flow)

MIT License

Downloads
47.8K
Stars
357
Committers
14

Bot releases are visible (Hide)

decoders - v1.16.1

Published by nvie about 5 years ago

  • Internal change to make the code Flow 0.105.x compatible. Basically stops using array spreads ([...things]) in favor of Array.from().
decoders - v1.16.0

Published by nvie about 5 years ago

New feature:

  • Allow map() calls to throw an exception in the mapper function to reject
    the decoder. Previously, these mapper functions were not expected to ever
    throw.
decoders - v1.15.0

Published by nvie over 5 years ago

New features:

  • Support constructors that have required arguments in instanceOf decoders in TypeScript (see #308, thanks @Jessidhia!)
  • Add support for type predicates in predicate() in TypeScript (see #310, thanks @Jessidhia!)

Fixes:

  • Add support for Flow >= 0.101.0
decoders - v1.14.0

Published by nvie over 5 years ago

Potential breaking changes:

  • Stricten pojo criteria. Now, custom classes like new String() or new Error() will not be accepted as a plain old Javascript object (= pojo) anymore.

Fixes:

  • Add support for Flow 0.98+
decoders -

Published by nvie over 5 years ago

Fixes:

  • Don't reject URLs that contains commas (,)
decoders -

Published by nvie over 5 years ago

Potentially breaking changes:

  • Changed the API interface of dispatch(). The previous version was too complicated and was hardly used. The new version is easier to use in practice, and has better type inference support in TypeScript and Flow.

    Previous usage:

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

    New usage: docs

    const shape = dispatch('type', { rectangle, circle });
    

    Where rectangle and circle are decoders of which exactly one will be invoked.

  • Removed the field() decoder. It was not generic enough to stay part of the standard decoder library. (It was typically used in combination with dispatch(), which now isn't needed anymore, see above.)

  • pojo decoder now returns Decoder<{[string]: mixed}> instead of the unsafe Decoder<Object>.

Fixes and cleanup:

  • Internal reorganization of modules
  • Improve TypeScript support
    • Reorganization of TypeScript declarations
    • More robust test suite for TypeScript
    • 100% TypeScript test coverage
decoders -

Published by nvie over 5 years ago

New decoders:

  • oneOf(['foo', 'bar']) will return only values matching the given values
  • instanceOf(...) will return only values that are instances of the given class. For example: instanceOf(TypeError).
decoders -

Published by nvie over 5 years ago

  • Reduce bundle size for web builds
  • New build system
  • Cleaner package output
decoders -

Published by nvie almost 6 years ago

Potentially breaking changes:

  • Decoders now all take mixed (TypeScript: unknown) arguments, instead of
    any 🎉 ! This ensures that the proper type refinements in the
    implementation of your decoder are made. (See migration notes below.)
  • Invalid dates (e.g. new Date('not a date')) won’t be considered valid by
    the date decoder anymore.

New features:

  • guard() now takes a config option to control how to format error
    messages. This is done via the guard(..., { style: 'inline' }) parameter.

    • 'inline': echoes back the input value and inlines errors (default);
    • 'simple': just returns the decoder errors. Useful for use in sensitive contexts.
  • Export $DecoderType utility type so it can be used outside of the decoders
    library.

Fixes:

  • Fixes for some TypeScript type definitions.
  • Add missing documentation.

Migration notes:

If your decoder code breaks after upgrading to 1.11.0, please take the
following measures to upgrade:

  1. If you wrote any custom decoders of this form yourself:

    const mydecoder = (blob: any) => ...
    //                       ^^^ Decoder function taking `any`
    

    You should now convert those to:

    const mydecoder = (blob: mixed) => ...
    //                       ^^^^^ Decoders should take `mixed` from now on
    

    Or, for TypeScript:

    const mydecoder = (blob: unknown) => ...
    //                       ^^^^^^^ `unknown` for TypeScript
    

    Then follow and fix type errors that pop up because you were making
    assumptions that are now caught by the type checker.

  2. If you wrote any decoders based on predicate(), you may have code like
    this:

    const mydecoder: Decoder<string> = predicate(
      s => s.startsWith('x'),
      'Must start with "x"'
    );
    

    You'll have to change the explicit Decoder type of those to take two type
    arguments:

    const mydecoder: Decoder<string, string> = predicate(
    //                               ^^^^^^ Provide the input type to predicate() decoders
      s => s.startsWith('x'),
      'Must start with "x"'
    );
    

    This now explicitly records that predicate() makes assumptions about its
    input type—previously this wasn’t get caught correctly.

decoders -

Published by nvie almost 6 years ago

decoders -

Published by nvie almost 6 years ago

decoders -

Published by nvie almost 6 years ago

  • Flow 0.85 support
decoders -

Published by nvie about 6 years ago

  • Update to latest debrief (which fixes a TypeScript bug)
decoders -

Published by nvie about 6 years ago

  • Drop dependency on babel-runtime to reduce bundle size
decoders -

Published by nvie about 6 years ago

  • Fix minor declaration issue in TypeScript definitions
decoders -

Published by nvie about 6 years ago

  • Tuple decoder error messages now show decoder errors in all positions, not
    just the first occurrence.

New decoders:

  • New tuple decoders: tuple3, tuple4, tuple5, and tuple6
  • unknown decoder is an alias of mixed, which may be more recognizable for
    TypeScript users.
decoders -

Published by nvie about 6 years ago

  • TypeScript support
decoders -

Published by nvie about 6 years ago

Breaking:

  • Private helper function undefined_or_null was accidentally exported in the
    package. This is a private API.
decoders -

Published by nvie about 6 years ago

New decoder:

  • dict() is like mapping(), but will return an object rather than a Map
    instance, which may be more convenient to handle in most cases.

Breaking:

  • optional(..., /* allowNull */ true) has been removed (was deprecated since
    1.8.3)
decoders -

Published by nvie over 6 years ago

Improved error reporting:

  • Fix bug where empty error branches could be shown in complex either expressions