Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/delvedor/Tyval

Fast and extensible validator for JavaScript
https://github.com/delvedor/Tyval

performance utility validation

Last synced: about 2 months ago
JSON representation

Fast and extensible validator for JavaScript

Awesome Lists containing this project

README

        

# Tyval
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/delvedor/Tyval.svg?branch=master)](https://travis-ci.org/delvedor/Tyval) [![NPM version](https://img.shields.io/npm/v/tyval.svg?style=flat)](https://www.npmjs.com/package/tyval)

> Programs should be written for people to read, and only incidentally for machines to execute.
> *[Abelson and Sussman]*

**Tyval** is a validator for JavaScript, focused on **performances** and **extensibility**.

The API is highly inspired from [Joi](https://github.com/hapijs/joi), but the implementation is very different. Tyval uses [code generation](https://github.com/delvedor/Tyval/blob/master/docs/vademecum.md) to achieve maximum speed when evaluating a variable.
Tyval is designed to validate single values in a synchronous way and has not an error management, it always returns a boolean, *true* if all the validations has passed, *false* if at least one has failed, the design of the API forces to write atomic test, in this way the result of a single test does not influence the others.

**Needs Node.js ≥ 4.0.0**

[Benchmark](https://github.com/delvedor/Tyval/blob/master/bench/bench-other-libs.js) comparisons with other libraries:
```bash
tyval (num) x 78,669,467 ops/sec ±1.75% (82 runs sampled)
joi (num) x 37,540 ops/sec ±0.91% (89 runs sampled)
validate.js (num) x 83,675 ops/sec ±1.60% (89 runs sampled)
is-my-json-valid (num) x 61,898,685 ops/sec ±1.46% (88 runs sampled)

tyval (str) x 81,093,089 ops/sec ±1.56% (85 runs sampled)
joi (str) x 22,927 ops/sec ±1.40% (91 runs sampled)
validate.js (str) x 96,270 ops/sec ±1.14% (91 runs sampled)
is-my-json-valid (str) x 12,099,361 ops/sec ±1.13% (85 runs sampled)
```

## Install
```
npm install tyval --save
```

## Usage
Easily require it, compose a function with the chainable API and then use it.
```javascript
const tyval = require('tyval')

const stringValidation = tyval.string().max(10).min(1).alphanum()
const numberLimits = tyval.or(tyval.number().max(1), tyval.number().min(10))

function getSomeData (str, num, callback) {
if (!stringValidation(str) || !numberLimits(num)) {
return callback(new Error('Parameters not as expected!'), null)
}
// . . .
}
```
Were you saying composability? :)
```javascript
const tyval = require('tyval')
const arr = tyval.array()

const arrMin = arr.min(5)
const arrMax = arr.max(20)
const arrRange = tyval.or(arrMin, arrMax)

const arrContain = arr.contains('string')
const arrContainMin = arrContain.min(5)
// Needless to say that the composability
// works only with validations of the same type.
```
You can use it for your unit test as well!
```javascript
const { test } = require('tap')
const tyval = require('tyval')
const generateString = require('../genStr')

const stringValidation = tyval.string().max(10).min(1).alphanum()

test('genStr', (t) => {
t.plan(1)
const result = generateString()
// Here we are testing that generateString function returns
// an alphanumeric string with a length between 1 and 10 characters
t.true(stringValidation(result))
})
```

### Browser version
If you need to use Tyval inside the browser use [`tyval.min.js`](https://github.com/delvedor/Tyval/blob/master/tyval.min.js), that is generated via *browserify* and *uglify*.
```html

```


## API
- tyval.string()
* tyval.string().alphanum()
* tyval.string().regex()
* tyval.string().max()
* tyval.string().min()
* tyval.string().length()
* tyval.string().mail()
* tyval.string().ipv4()
* tyval.string().ipv6()
* tyval.string().base64()
* tyval.string().JSON()
* tyval.string().uuid()
* tyval.string().MAC()
* tyval.string().md5()
* tyval.string().card()

- tyval.number()
* tyval.number().max()
* tyval.number().min()
* tyval.number().positive()
* tyval.number().negative()
* tyval.number().integer()
* tyval.number().float()
* tyval.number().safeInteger()
* tyval.number().finite()
* tyval.number().multiple()
* tyval.number().notNaN()
* tyval.number().port()

- tyval.array()
* tyval.array().max()
* tyval.array().min()
* tyval.array().length()
* tyval.array().contains()
* tyval.array().items()

- tyval.date()
* tyval.date().lower()
* tyval.date().higher()

- tyval.boolean()

- tyval.object()
* tyval.object().empty()
* tyval.object().notNull()
* tyval.object().notArray()
* tyval.object().notDate()
* tyval.object().notRegExp()
* tyval.object().has()
* tyval.object().hasNot()

- tyval.error()
* tyval.error().RangeError()
* tyval.error().ReferenceError()
* tyval.error().SyntaxError()
* tyval.error().TypeError()
* tyval.error().message()

- tyval.or()

- tyval._______.extend()

## TODO
- [x] Rewrite API to improve performances
- [x] Implement tyval.array()
- [x] Implement max/min for array.length
- [x] Refactor of the tyval object, divide functions by field (string, number, array, object...) for a better maintainability
- [x] Add `Date` validator
- [x] Split test in multiple files
- [x] New string validation functions
- [x] Browser version
- [x] Improve lib code readability
- [x] In toFunction, move function parameters inside function blocks to avoid naming conflicts
- [x] Improve generated code readability
- [x] Add `.or`functionality
- [x] Remove `.toFunction()`
- [ ] Add `Any` type
- [ ] Make compatible extend/getArgs with es6
- [ ] Add `.not`functionality eg: `tyval.not.string()`

## Contributing
If you feel you can help in any way, be it with examples, extra testing, or new features please open a pull request or open an issue.

Do you want to know more how this library is built?
Have a look [here](https://github.com/delvedor/Tyval/blob/master/docs/vademecum.md)!

I would make a special thanks to [@mcollina](https://github.com/mcollina) for helping me to improving the code.

The code follows the Standard code style.
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

## License
**[MIT](https://github.com/delvedor/Tyval/blob/master/LICENSE)**

*The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.*

Copyright © 2016 Tomas Della Vedova