Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aliencreations/node-validator

Validator for Alien Creations node apps.
https://github.com/aliencreations/node-validator

Last synced: 28 days ago
JSON representation

Validator for Alien Creations node apps.

Awesome Lists containing this project

README

        

# node-validator
Validator for Alien Creations node apps

## Install

```
$ yarn add @aliencreations/node-validator
```

## Bundled Dependencies
- [prettycats](https://github.com/seancannon/prettycats)
- [jcvd](https://github.com/seancannon/jcvd)

## Usage
`validate` accepts two payloads - one is the schema and one is the payload. If the payload
does not satisfy the schema, the function will throw an error.

Please see [node-error](https://github.com/aliencreations/node-error) for
a complete list of validation errors.

```js
const {
isArrayOf,
isObjectOf,
isOptional,
isRequired,
label,
prr
} = require('@aliencreations/node-validator');

const accountType = label('accountType', isObjectOf({
accountName : isRequired(prr.isStringOfLengthAtMost(30)),
accountNumber : isRequired(prr.isPositiveNumber)
}));

const validateUser = label('validateUser', isObjectOf({
firstName : isRequired(prr.isStringOfLengthAtMost(30)),
lastName : isRequired(prr.isStringOfLengthAtMost(30)),
accounts : isOptional(isArrayOf(accountType))
}));

// Example
try {
validateUser({
firstName : 'Joe',
lastName : 'Swanson',
accounts : [
{
name : 'My Main Account',
number : 12345
}
]
});
} catch (e) {
// threw because accounts -> [0] -> name is unsupported, should have been accountName
}

```
---
## Changelog

##### 1.0.0
- Initial commit

##### 1.0.1
- Tree-shake

##### 1.0.2
- Update [jcvd](https://github.com/seancannon/jcvd) library

##### 1.0.3
- Update [prettycats](https://github.com/seancannon/prettycats) library

##### 1.0.4
- Update README

##### 1.0.5
- Update [prettycats](https://github.com/seancannon/prettycats) library

##### 1.0.6
- Update README