Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/swissmanu/spicery

Runtime type safety for JSON/untyped data.
https://github.com/swissmanu/spicery

json parser runtime typesafe typescript

Last synced: 3 months ago
JSON representation

Runtime type safety for JSON/untyped data.

Awesome Lists containing this project

README

        

# spicery

> Runtime type safety for JSON/untyped data.

[TypeScript](https://www.typescriptlang.org/) gives you compile-time type safety for your JavaScript code. External data which you get through, for example, XHR calls is only checkable at runtime. `spicery` allows you to handle runtime checks in one place using an easy to use API.

[![npm version](https://badge.fury.io/js/spicery.svg)](https://badge.fury.io/js/spicery) [![Build Status](https://travis-ci.org/swissmanu/spicery.svg?branch=master)](https://travis-ci.org/swissmanu/spicery) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

## Examples

```typescript
// You know TypeScript Interfaces:
interface Todo { userId: number; id: number; title: string; completed: boolean; }

// This is new: Define a Todo Parser with spicery:
const todos: ParserFn = x => ({
userId: fromMap(x, 'userId', aNumber),
id: fromMap(x, 'id', aNumber),
title: fromMap(x, 'title', aString),
completed: fromMap(x, 'completed', aBoolean)
});

// Define another Parser for an Array containing Todos using spicery array parser:
const jsonplaceholderTodos = anArrayContaining(todos);

// Fetch Todos and parse them:
window.fetch('https://jsonplaceholder.typicode.com/todos')
.then(r => parse(jsonPlaceholderTodos)(r.body))
.then(t => console.log(t))
```

For further examples refer to the `examples/` directory. To execute them locally, clone this repository and run following commands on your command line of choice:

```bash
yarn
yarn run example:simple
yarn run example:complex
```

*Though the first `yarn` command is only used to install any module dependencies once* 🤓

## Contribution

The `master` branch contains the latest stable release.
Development efforts are integrated with the `develop` branch first. Changes get then merged into `master` as soon as a new release should be published.

When opening a new Pull Request make sure you point them to `develop`. Make use of proper commit messages: `spicery` loves [Commitizen](http://commitizen.github.io/cz-cli/), so take a look there and use `git cz` for the most simple workflow :-)

Thank you for your contribution!