A powerful, well tested, data decoder for Typescript.
API Documentation: Decoder Github: Decoder
Simply run
npm i elm-decodersOr
yarn add elm-decodersThen at the top of your file add:
import { Decoder } from 'elm-decoders';
Typescript is great, however it provides no tools for checking runtime data. This means that we need a tool to check that incoming data follows the correct typings. If we do not validate the data, errors can occur anywhere in the code, introducing odd behaviors and tracking down where the error comes from becomes difficult. By instead validating our data at the start (for example when receiving an incoming request), we can handle the error early and give better error messages to the developer. This creates a better developer experience and gives us stronger guarantees that the code works correctly.
Another benefit of using Decoders is that you can pick the best data model for your problem and convert all incoming data sources to fit that model. This makes it easier to write business logic separately from the acquisition of the data.
Decoders are great for validating and converting data from various sources: Kafka, request bodies or APIs to name a few examples.
For more motivation, see this blog post
Decoder provides us with a few primitive decoders and a few methods to craft new ones. Let's say we have the following data:
const incomingData: any = {
name: 'Nick',
age: 30,
};
And we have an interface User:
interface User {
name: string;
age: number;
}
To validate that incomingData is a User, Decoder provides an object primitive.
import { Decoder } from 'elm-decoders';
const userDecoder: Decoder<User> = Decoder.object({
name: Decoder.string,
age: Decoder.number,
});
Now we can validate incomingData and ensure the data is correct.
const result = userDecoder.run(incomingData);
run returns a Discriminated
union,
meaning is returns either {type: "OK", value: T} or {type: "FAIL": error: string}. This means that we are forced to check if the data received is correct or contains an error. Doing so
is as simple as a switch case:
switch (result.type) {
case 'OK':
doUserThing(result.value);
case 'FAIL':
handleError(result.error);
}
Decoder also provides a few methods for creating new decoders. For example, if
we want to create a set decoder, we can use the map method.
const intSetDecoder: Decoder<Set<number>> = Decoder.array(Decoder.number).map(
(numberArray) => new Set(numberArray)
);
If there is an error, Decoder will also generate a helpful error report:
> let userDecoder = Decoder.object({
name: Decoder.string,
auth: Decoder.object({
jwt: Decoder.string
})
})
> JSON.stringify(userDecoder.run({wrong: 'hi', auth: {wrongAgain: 'hi'}}))
'{"type":"FAIL","error":{"name":"Not a string","auth":{"jwt":"Not a string"}}}'
This was a brief introduction. From here, please check the API documentation to find out more what you can do and try it for yourself!
This library is essentially a rewrite of Nvie's decoders.js with some small changes. decoders.js is inspired by Elm's decoders.
Below is a list of commands you will probably find useful.
npm start or yarn startRuns the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.
Your library will be rebuilt if you make edits.
npm run build or yarn buildBundles the package to the dist folder.
The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).
npm test or yarn testRuns the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.
Generated using TypeDoc