# lib-marshal

> marshalled object validation

Latest version **0.8.0** (published 2015-04-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install lib-marshal
pnpm add lib-marshal
yarn add lib-marshal
bun add lib-marshal
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2015-04-02 |
| First published | 2014-04-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | groundwater |

## Links

- npm: https://www.npmjs.com/package/lib-marshal
- Repository: https://github.com/groundwater/node-lib-marshal
- Issues: https://github.com/groundwater/node-lib-marshal/issues
- npm.io page: https://npm.io/package/lib-marshal

## Recent versions

- 0.8.0 (latest) — 2015-04-02
- 0.7.0 — 2014-07-02
- 0.6.1 — 2014-06-28
- 0.6.0 — 2014-06-28
- 0.5.0 — 2014-04-07
- 0.4.0 — 2014-04-04
- 0.3.0 — 2014-04-04

## README

# lib-marshal

> marshalled object validation

[![Build Status](https://travis-ci.org/groundwater/node-lib-marshal.svg?branch=master)](https://travis-ci.org/groundwater/node-lib-marshal)

## Install

```bash
npm install --save lib-marshal
```

## Usage

### define your types

```javascript
var marshal = require('lib-marshal');

// define some types
var stringProto  = new marshal.StringType;
var messageProto = new marshal.StructType;

messageProto.add('from'   , stringProto);
messageProto.add('dest'   , stringProto);
messageProto.add('body'   , stringProto);
messageProto.add('subject', stringProto);
```

### prepare data for the client

Prepare data for the client,
by stripping out unused properties and validating data types.

```javascript
// create a data object
var m = {
  from: 'bob',
  dest: 'kim',
  body: 'hello world'
};

// ready it for the world, or die trying
JSON.stringify(messageProto.marshal(m))
```

### parse incoming data

Validate an incoming object, or throw an error.

```javascript
var message = messageProto.marshal(JSON.parse(str))
```

## options

### numbers

```js
var num = new NumberType({max: 10, min: 0})

num.marshal(-1)
// Error: Value <-1> must be greater than 0 at <var>

num.marshal(20)
// Error: Value <20> must be less than 10 at <var>
```

### strings

```js
var str = new StringType({min: 1, max: 20, match: /.@.\../})

str.marshal('bob AT aol.com')
// Error: Value <bob AT aol.com> must be match the pattern </.@.\../> at <var>
```

### untyped (json)

If you're not ready for a schema yet, you can allow arbitrary data with the `JsonType` type

```js
var type = new JsonType()
var json = type.marshal({one: 1, two: 'two'})
```

## errors

Handy dandy error messages

```js
var message = new StructType;
var address = new StructType;
var content = new StructType;

content.add('subject', new StringType)
content.add('body', new StringType)
content.add('author', address)

address.add('name', new StringType)
address.add('email', new StringType)

message.add('from', address)
message.add('to', address)
message.add('content', content)

message.marshal({
  to: {
    name    : "Kim",
    email   : "kim@outer.space"
  },
  from      : {
    name    : "Bob",
    email   : "bob@outer.space"
  },
  content   : {
    body    : "This is a test",
    subject : "Hello World",
    author  : "Bob"
  }
});
```

You'll receive a helpful error when things fail to parse

```
Error: Expected <object> but Received "Bob" of type <string> at <var>.content.author
```

## see also

- [lib-schema](https://www.npmjs.org/package/lib-schema)
  generate a marshaller from a json schema

---
_Source: https://npm.io/package/lib-marshal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
