Fast and extensible validator for JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bench
docs
lib
test
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
build-browser.js
package.json
tyval.js
tyval.min.js

README.md

Tyval

js-standard-style Build Status NPM version

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, but the implementation is very different. Tyval uses code generation 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 comparisons with other libraries:

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.

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? :)

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!

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, that is generated via browserify and uglify.

<script src="./node_modules/tyval/tyval.min.js"></script>

API

TODO

  • Rewrite API to improve performances
  • Implement tyval.array()
  • Implement max/min for array.length
  • Refactor of the tyval object, divide functions by field (string, number, array, object...) for a better maintainability
  • Add Date validator
  • Split test in multiple files
  • New string validation functions
  • Browser version
  • Improve lib code readability
  • In toFunction, move function parameters inside function blocks to avoid naming conflicts
  • Improve generated code readability
  • Add .orfunctionality
  • Remove .toFunction()
  • Add Any type
  • Make compatible extend/getArgs with es6
  • Add .notfunctionality 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!

I would make a special thanks to @mcollina for helping me to improving the code.

The code follows the Standard code style.
js-standard-style

License

MIT

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