Elegant validation library for type-safe input data (for TypeScript and Flow)
MIT License
Bot releases are hidden (Show)
taggedUnion
(thanks for reporting, @programever)Published by nvie 9 months ago
.pipe()
method on Decoder
allows you to pass the output of one decoder into another:
string
.transform((s) => s.split(',')) // transform first...
.pipe(array(nonEmptyString)); // ...then validate that result
This was previously possible already with .then
, but it wasn't as elegant to express..pipe()
can also dynamically select another decoder, based on the input:
string
.transform((s) => s.split(',').map(Number)) // transform first...
.pipe((tup) =>
tup.length === 2
? point2d
: tup.length === 3
? point3d
: never('Invalid coordinate'),
);
"Value at key \"foo\": Must be \"bar\", \"qux\"" // ❌ Previously
"Value at key 'foo': Must be 'bar', 'qux'" // ✅ Now
numericBoolean
decoder, which was deprecated since 2.3.0.Published by nvie 10 months ago
New features:
enum
types are now supported (docs)record(values)
and record(keys, values)
forms (docs)datelike
decoder (docs)bigint
(docs)constant()
and oneOf()
New decoders:
enum_
(see docs)record()
(see docs)select()
(see docs)bigint
(see docs)datelike
(see docs)decimal
(see docs)hexadecimal
(see docs)numeric
(see docs)Renamed decoders:
Some decoders have been renamed because their names were based on Flowisms. Names have been updated to better reflect TypeScript terminology:
dict()
→ record()
maybe()
→ nullish()
set()
→ setFromArray()
(to make room for a better set()
decoder in a futureDeprecated decoders:
The following decoders are deprecated because they were not commonly used, and a bit too specific to be in the standard library. They are also scheduled for removal in a future decoders version.
dict()
(prefer record()
)hardcoded()
(prefer always()
)maybe()
(prefer nullish()
)mixed
(prefer unknown
)numericBoolean()
Other changes:
positiveNumber
and positiveInteger
no longer accept -0
as valid inputseither
return type would sometimes get inferred incorrectly if members partially overlapped (see #941)Published by nvie 10 months ago
Breaking change: Dropped Flow support[^1].
Breaking change: Projects that are not yet using strict: true
in their tsconfig.json
files files are no longer supported. Previously, decoders went to great lenghts to support both configurations, but the internal typing magic was getting too complex to maintain without much benefit.
Breaking change: A small breaking change is introduced that removes the need for some packaging workarounds to support projects using old TypeScript/Node versions. It’s now simpler to use, and simpler to maintain:
-import { formatInline, formatShort } from 'decoders/format'; // ❌
+import { formatInline, formatShort } from 'decoders'; // ✅
-import { Result, ok, err } from 'decoders/result'; // ❌
+import { Result, ok, err } from 'decoders'; // ✅
Other, smaller changes, mostly internal:
strict
mode.[^1]: I'm still open to bundling Flow types within this package, but only if that can be supported in a maintenance-free way, for example by using a script that will generate *.flow
files from TypeScript source files. If someone can add support for that, I'm open to pull requests! 🙏 )
Published by nvie 11 months ago
lib.dom.d.ts
in TypeScriptPublished by nvie about 1 year ago
positiveInteger(-0)
is now 0
, not -0
positiveNumber(-0)
is now 0
, not -0
Thanks, @gcampax!
Published by nvie over 1 year ago
Fix a bug in the url
decoder, which could incorrectly reject URLs with a /
in the query path. Thanks, @gcampax!
Published by nvie over 1 year ago
Fix bundling issue where TypeScript types would not get picked up correctly in old TypeScript versions. Thanks, @robinchow!
Fix TypeScript types for Result
type to allow implicit-undefineds.
Published by nvie almost 2 years ago
formatShort
and formatInline
helper functionsPublished by nvie over 2 years ago
TypeScript-only: Fix definition of JSONObject to reflect that its values might always be undefined
as well.
TypeScript-only: Changed return types of { [key: string]: T }
to Record<string, T>
.
TypeScript-only: Fine-tune the type of instanceOf()
.
Published by nvie over 2 years ago
This is a breaking change, which brings numerous benefits:
Please see the migration guide for precise instructions on how to
adjust your v1 code.
The main change is the brand new Decoder<T>
API! The tl;dr is:
Replace this v1 pattern... | ...with this v2 API | Notes | |
---|---|---|---|
mydecoder(input) |
→ | mydecoder.decode(input) |
migration instructions |
guard(mydecoder)(input) |
→ | mydecoder.verify(input) |
migration instructions |
map(mydecoder, ...) |
→ | mydecoder.transform(...) |
migration instructions |
compose(mydecoder, predicate(...)) |
→ | mydecoder.refine(...) |
migration instructions |
describe(mydecoder, ...) |
→ | mydecoder.describe(...) |
|
mydecoder(input).value() |
→ | mydecoder.value(input) |
|
either , either3 , ..., either9
|
→ | either |
migration instructions |
tuple1 , tuple2 , ... tuple6
|
→ | tuple |
migration instructions |
dispatch |
→ | taggedUnion |
migration instructions |
url(...) |
→ |
httpsUrl / url (signature has changed) |
migration instructions |
The full documentation is available on decoders.cc.
Other features:
Other potentially breaking changes:
lemons
has been removedNew decoders:
Other improvements:
optional()
,nullable()
, andmaybe()
now each take an optional 2nd param toeither
sImplementation changes:
Published by nvie over 2 years ago
Upgrading to v2 can, but doesn't have to be a breaking change for you. If upgrading causes errors for you, please see the migration guide for instructions.
Published by nvie almost 3 years ago
Fix compatibility issue with TypeScript projects configured with
strictNullChecks: false
(or strict: false
) (Thanks, @stevekrouse and @djlauk!)
Officially support Node 16.x
Published by nvie about 3 years ago
nonEmptyArray
function in TypeScript (Thanks, @mszczepanczyk!)Published by nvie about 3 years ago
constant(...)
now has to be scalar value in both Flow and TypeScript,Published by nvie about 3 years ago
constant()
. (Thanks, @schmod!)Published by nvie over 3 years ago
Published by nvie over 3 years ago
Fix signature of oneOf()
to reflect it can only be used with scalar/constant values
In TypeScript, the inferred type for oneOf(['foo', 'bar'])
will now be Decoder<'foo' | 'bar'>
instead of Decoder<string>
🎉
Drop support for Flow versions < 0.115.0
Published by nvie over 3 years ago