All Projects → delvedor → Tyval

delvedor / Tyval

Licence: MIT license
Fast and extensible validator for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Tyval

apint
Arbitrary precision integers library.
Stars: ✭ 23 (-62.9%)
Mutual labels:  utility
fhash
fHash - an open source files hash calculator for Windows and macOS
Stars: ✭ 222 (+258.06%)
Mutual labels:  utility
sublimetext-autobackups
Sublime Text 2/3 Auto backups plugin
Stars: ✭ 70 (+12.9%)
Mutual labels:  utility
GPUUtilization
measure GPU hardware utilization on iOS Devices
Stars: ✭ 102 (+64.52%)
Mutual labels:  utility
lambda
Minecraft utility mod coded in Kotlin
Stars: ✭ 389 (+527.42%)
Mutual labels:  utility
ee-x
Cross-platform library for Cocos2d-x and Unity
Stars: ✭ 13 (-79.03%)
Mutual labels:  utility
KoiCatalog
A card manager for Koikatu. View, search, and sort your collection of character cards.
Stars: ✭ 18 (-70.97%)
Mutual labels:  utility
as-a
Runs a given command with additional environment settings for simple local development
Stars: ✭ 60 (-3.23%)
Mutual labels:  utility
sora
A simple library to display images in Jupyter notebooks
Stars: ✭ 15 (-75.81%)
Mutual labels:  utility
actlist
📦 Actlist is a utility platform to execute your own action list easily and simply.
Stars: ✭ 85 (+37.1%)
Mutual labels:  utility
react-pendulum
A React Context utility library.
Stars: ✭ 15 (-75.81%)
Mutual labels:  utility
codec
Encode keys, values and range options, with built-in or custom encodings.
Stars: ✭ 27 (-56.45%)
Mutual labels:  utility
kubernetes-basico
Demonstração dos componentes do Kubernetes
Stars: ✭ 26 (-58.06%)
Mutual labels:  utility
gendesk
🌿 Generate .desktop files and download .png icons by specifying a minimum of information
Stars: ✭ 101 (+62.9%)
Mutual labels:  utility
AjaxHandler
ASimple PHP Class to help handling Ajax Requests easily
Stars: ✭ 30 (-51.61%)
Mutual labels:  utility
toastify
🍞A commandline tool that shows desktop notifications using notify-rust
Stars: ✭ 60 (-3.23%)
Mutual labels:  utility
transee
Simple and useful tool for quick translation
Stars: ✭ 65 (+4.84%)
Mutual labels:  utility
RunDLL-NG
A better alternative to RunDLL32
Stars: ✭ 23 (-62.9%)
Mutual labels:  utility
BlauBot
A discord bot providing a collection of useful and unrelated commands
Stars: ✭ 16 (-74.19%)
Mutual labels:  utility
autocommand
Autocommand turns a python function into a CLI program
Stars: ✭ 44 (-29.03%)
Mutual labels:  utility

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].