rafinad
v0.1.1
Published
Data validation inspired by refinement type
Downloads
2
Readme
rafinad
Data validation inspired by refinement type
Installation
npm install rafinad
or
yarn add rafinad
Example
const rafinad = require('rafinad')
const isArticle = rafinad.isOk({
caption: rafinad.isString,
tags: rafinad.isEvery(rafinad.isString),
version: rafinad.and(
rafinad.isNumber,
value => value >= 0
)
})
// Let's check valid article
console.log(isArticle.validate({
caption: 'test',
tags: [],
version: 0
}).isValid()) // true
// this article is invalid: it has version less than zero
console.log(isArticle.validate({
caption: 'test',
tags: [],
version: -1
}).isValid()) // false
// this article is invalid: it has number as caption
console.log(isArticle.validate({
caption: 1,
tags: [],
version: 0
}).isValid()) // false
// this article is invalid: it has extra key `extraKey`
console.log(isArticle.validate({
caption: 'test',
tags: [],
version: 0,
extraKey: 1
}).isValid()) // false