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 🙏

© 2024 – Pkg Stats / Ryan Hefner

vitesse

v1.0.1

Published

Vitesse is a high performance object validation library

Downloads

9

Readme

Vitesse

Links

| what | where | |---------------|------------------------------------------------| | documentation | http://christkv.github.io/vitesse/ | | api-doc | http://christkv.github.io/vitesse/api/ | | source | https://github.com/christkv/vitesse |

Description

Vitesse is a high speed object validation framework. It's meant as a target for developers to build validation frameworks or DSL (Domain specific languages) while being able to leverage close to hand-coded performance.

  • Joi test x 139,410 ops/sec ±1.86% (85 runs sampled)
  • Compiler test optimized x 2,555,137 ops/sec ±1.50% (93 runs sampled)
  • Closure compiler test x 2,745,918 ops/sec ±0.86% (83 runs sampled)
  • Manual vitesse test x 2,588,368 ops/sec ±0.83% (92 runs sampled)

The goal of this module is to allow you to avoid the cost of interpreting a set of validation rules by ahead of time compile it (AOT) using eval, allowing you to get close to the performance of manually writing validation code.

With Vitesse as your target you can define whatever DSL you want and have Vitesse optimize it for maximum performance

It's easy to write your own custom DSL. Below is a simple example of a possible custom validation DSL using ES6.

"use strict"

var v = require('vitesse'),
  StringNode = v.StringNode,
  ObjectNode = v.ObjectNode,
  IntegerNode = v.IntegerNode,
  NumberNode = v.NumberNode,
  ArrayNode = v.ArrayNode,
  VitesseCompiler = v.Compiler;

class Validator {
  constructor() {    
  }

  static object() {
    return new ObjectBuilder();
  }

  static string() {
    return new StringBuilder();
  }

  static array() {
    return new ArrayBuilder();
  }
}

class Compiler {
  constructor() {    
  }

  compile(object, options) {
    options = options || {};
    options.optimizer = true;
    // Get the compiler
    return new VitesseCompiler().compile(object.object, options);
  }
}

class ObjectBuilder {
  constructor() {    
    this.object = new ObjectNode(null, null, {typeCheck:true});
  }

  fields(fields) {
    for(var name in fields) {
      this.object.addChild(name, fields[name].object);
    }

    return this;
  }

  require(required) {
    this.object.requiredFields(required);
    return this;
  }
}

class StringBuilder {
  constructor() {    
    this.object = new StringNode(null, null, {typeCheck:true})
  }

  in(values) {
    this.object.addValidation({$in: values});
    return this;
  }
}

class ArrayBuilder {
  constructor() {    
    this.object = new ArrayNode(null, null, {typeCheck:true})
  }

  of(object) {
    this.object.addItemValidation(object.object);
    return this;
  }
}

module.exports = {
  Validator: Validator,
  Compiler: Compiler
}

This simple custom DSL let's you create validations of the following style.

var validator = Validator
  .object()
  .fields({
    id: Validator.string(),
    users: Validator.array().of(Validator.object().fields({
      id: Validator.string(),
      permissions: Validator.array().of(Validator.string().in(['read', 'write', 'delete', 'upate']))
    })
    .require(['id', 'permissions']))
  })
  .require(['id', 'users']);  

var compiler = new Compiler();
var validator = compiler.compile(validator);
assert.equal(1, validator.validate({id:'', users:[{id:'', permissions:['yupp']}]}).length);

Available Nodes

Object Node

The object node defines that validations for an object.

var objectNode = new ObjectNode(null, null, {typeCheck:true})

The following methods are available