https://github.com/kdby-io/redprint
A tool for sharing validations
https://github.com/kdby-io/redprint
validate validation validator
Last synced: 3 months ago
JSON representation
A tool for sharing validations
- Host: GitHub
- URL: https://github.com/kdby-io/redprint
- Owner: kdby-io
- License: artistic-2.0
- Created: 2017-08-26T13:54:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-14T04:21:26.000Z (over 8 years ago)
- Last Synced: 2025-09-07T02:21:32.852Z (3 months ago)
- Topics: validate, validation, validator
- Language: TypeScript
- Homepage:
- Size: 51.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
{
: {
: {
:
}
}
}
```
`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 |