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, 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 x 78,669,467 ops/sec ±1.75% joi x 37,540 ops/sec ±0.91% validate.js x 83,675 ops/sec ±1.60% is-my-json-valid x 61,898,685 ops/sec ±1.46% tyval x 81,093,089 ops/sec ±1.56% joi x 22,927 ops/sec ±1.40% validate.js x 96,270 ops/sec ±1.14% is-my-json-valid x 12,099,361 ops/sec ±1.13%
Install
npm install tyval --save
Usage
Easily require it, compose a function with the chainable API and then use it.
const tyval = const stringValidation = tyvalconst numberLimits = tyval { if ! || ! return // . . .}
Were you saying composability? :)
const tyval = const arr = tyval const arrMin = arrconst arrMax = arrconst arrRange = tyval const arrContain = arrconst arrContainMin = arrContain// 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 = const tyval = const generateString = const stringValidation = tyval
Browser version
If you need to use Tyval inside the browser use tyval.min.js
, that is generated via browserify and uglify.
API
-
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()
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
.or
functionality - 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!
I would make a special thanks to @mcollina for helping me to improving the code.
The code follows the Standard code style.
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