# redprint

> A tool for sharing validations

Latest version **0.1.1** (published 2017-09-13) · Artistic-2.0 license · 0 weekly downloads

## Install

```sh
npm install redprint
pnpm add redprint
yarn add redprint
bun add redprint
```

## 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.1.1 |
| Published | 2017-09-13 |
| First published | 2017-08-26 |
| Weekly downloads | 0 |
| License | Artistic-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gloorx |
| Maintainers | gloorx |
| Keywords | validation, validator, validate |

## Links

- npm: https://www.npmjs.com/package/redprint
- Repository: https://github.com/gloorx/redprint
- Issues: https://github.com/gloorx/redprint/issues
- npm.io page: https://npm.io/package/redprint

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [fs-extra](https://npm.io/package/fs-extra.md) ^4.0.1

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.1.1 (latest) — 2017-09-13
- 0.1.0 — 2017-09-10
- 0.0.0 — 2017-08-26

## README

# Redprint

A tool for sharing validations

## Goal
When developing a web application, there were 2 options for validating user input between API suppliers and consumers. A consumer can have validations on its own by refering API documents, or wait the result of validations from a API supplier. On the former, it cannot be ensured that consumer's validations works same with API supplier's. On the latter, there are limits to validate in real-time because of network latency.

Redprint makes it possible to validate in real-time and manage validations at one place.

## Schema
For using Redprint, API validation must fit in `Redprint` schema.

It shapes
```js
{
  <modelName>: {
    <attributeName>: {
      <validationName>: <validation>
    }
  }
}
```

`validation` is a function. It must receive 1 argument, and return boolean.

For example, It can be like
```js
{
  User: {
    username: {
      notEmpty: (input) => (input.length !== 0),
      isAlpha: (input) => (/^[a-z0-9]+$/.test(input)),
    },
    password: {
      notEmpty: (input) => (input.length !== 0),
    },
  }
}
```

## Install
```sh
npm install redprint
```

## Usage

### Supplier-side
If your validation code is
```js
const validation = {
  User: {
    username: {
      notEmpty: (input) => (input.length !== 0)
    }
  }
};
```

Just do like
```js
import { red } from 'redprint';
const validation = red({
  User: {
    username: {
      notEmpty: (input) => (input.length !== 0)
    }
  }
});
```

Then run server. 'redprint.json' is generated in your project root. Provide this file for your API comsumers.

### Comsumer-side
At first, locate 'redprint.json' provided from API supplier in your project root.

```js
import { validate } from 'redprint';

const username = getUsernameFromForm();
try {
  validate('User.username', username);
} catch (err) {
  handleError(err);
  console.log(err.message);
  // RedprintError: '' is invalid User.username for 'notEmpty' validation
}
```

## API

### `red(redprint: Redprint)`
Append `redprint` to 'redprint.json'.

### `validate(key: string, input: any)`
Validate `input` as the `key`, on the basis of 'redprint.json'.

### `RedprintError`
It is thrown if `validate()` failed.

| Property       | Description                             |
|----------------|-----------------------------------------|
| input          | User input                              |
| key            | What the input is for                   |
| validationName | Which validation failed                 |

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