npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tyval

v4.0.0

Published

Fast and extensible validator for JavaScript

Readme

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

  • 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 .orfunctionality
  • [x] 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