# relax-schema

> Structured data with declarative validation

Latest version **1.0.2** (published 2016-07-10) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install relax-schema
pnpm add relax-schema
yarn add relax-schema
bun add relax-schema
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-07-10 |
| First published | 2016-03-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Infinite Automata Community |
| Maintainers | tokyo-jesus |
| Keywords | validation, declarative |

## Links

- npm: https://www.npmjs.com/package/relax-schema
- Repository: https://github.com/heropunch/relax-schema
- Homepage: https://github.com/heropunch/relax-schema#readme
- Issues: https://github.com/heropunch/relax-schema/issues
- npm.io page: https://npm.io/package/relax-schema

## Dependencies (3)

- [q](https://npm.io/package/q.md) ^1.4.1
- [moment](https://npm.io/package/moment.md) ^2.12.0
- [validate.js](https://npm.io/package/validate.js.md) ^0.9.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-07-10
- 1.0.1 — 2016-03-16
- 1.0.0 — 2016-03-16

## README

[![npm version](https://badge.fury.io/js/relax-schema.svg)](https://badge.fury.io/js/relax-schema)
[![Build Status](https://travis-ci.org/heropunch/relax-schema.svg?branch=master)](https://travis-ci.org/heropunch/relax-schema)
[![Coverage Status](https://coveralls.io/repos/github/heropunch/relax-schema/badge.svg?branch=master)](https://coveralls.io/github/heropunch/relax-schema?branch=master)
[![Dependency Status](https://david-dm.org/heropunch/relax-schema.svg)](https://david-dm.org/heropunch/relax-schema)

# relax-schema

<a name="Schema"></a>

## Schema
Structured data with declarative validation

**Kind**: global class  

* [Schema](#Schema)
    * [new Schema(name, constraints, validators)](#new_Schema_new)
    * [.validate(doc)](#Schema+validate) ⇒ <code>Promise.&lt;Object&gt;</code>
    * [.validateSync(doc)](#Schema+validateSync) ⇒ <code>Object</code>

<a name="new_Schema_new"></a>

### new Schema(name, constraints, validators)
Create a structure for your data. Uses [`validate.js`][validate.js] to
provide declarative validation for your documents. Additional validation
functions may be specified by passing an object to the constructor.

Create a Schema using this simple declaration:

```js
const BlogPost = new Schema("BlogPost", {
  title: {
    presence: true
  },
  datePublished: {
    presence: true,
    date: true
  },
  author: {
    email: true
  }
})
```

[validate.js]: http://validatejs.org/


| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The name of the model. |
| constraints | <code>Object</code> | The constraints this model is subject to. |
| validators | <code>Object</code> | Custom validation functions. |

<a name="Schema+validate"></a>

### schema.validate(doc) ⇒ <code>Promise.&lt;Object&gt;</code>
Ensure that a given document can meet this model's constraints.

Use a simple promise-based API to check that your data is correctly
structured:

```js
BlogPost.validate({
  title: "My Adventure",
  datePublished: "2012-04-20",
  author: "jane@example.com"
}).then((doc) => {
  // do something cool
}).catch((err) => {
  // be strong
})
```

**Kind**: instance method of <code>[Schema](#Schema)</code>  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise for the validated document.  
**Throws**:

- <code>[ValidationError](#ValidationError)</code> 


| Param | Type | Description |
| --- | --- | --- |
| doc | <code>Object</code> | The document being validated. |

<a name="Schema+validateSync"></a>

### schema.validateSync(doc) ⇒ <code>Object</code>
Ensure that a given document can meet this model's constraints.

**Kind**: instance method of <code>[Schema](#Schema)</code>  
**Returns**: <code>Object</code> - The validated document  
**Throws**:

- <code>[ValidationError](#ValidationError)</code> 


| Param | Type | Description |
| --- | --- | --- |
| doc | <code>Object</code> | The document being validated. |


<a name="ValidationError"></a>

## ValidationError
**Kind**: global class  
<a name="new_ValidationError_new"></a>

### new ValidationError(result, doc, constraints)
Create a validation-specific error.


| Param | Type | Description |
| --- | --- | --- |
| result | <code>Array.&lt;Object&gt;</code> | Validation errors. |
| doc | <code>Object</code> | The document that did not validate. |
| constraints | <code>Object</code> | The configured validation rules. |

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