Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A validation library for anything.
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
benchmark
example
test
.gitignore
.travis.yml
LICENSE
README.md
pack.sh
package.json
quality.js

README.md

Build Status

quality

A validation library for anything. ( I mean anything, not just html forms or data... quality uses quality to ensure it's own api ).

Validations are just simple objects, that you can easily pass around between the browser and node via JSON. So you just write once and re-use.


Validation

var webOrderSchema = { 
  id: { _equal: /^web[0-9]+/,  }, 
  created_on: { _type: 'date' }, 
  customer: { userId: { _type: 'number', _length: 5 } },
  cart: [
    // this repeats itself on the array elements
    { productId: { _type: 'number' }, price: { _type: 'number' } } 
  ]
}

quality.test(webOrderSchema, webOrder, function(err) { 
  if (!err) save_to_db;
})

For validations that are more complex or require async, you can use the post keyword.

var webOrderSchema = { 
  id: { 
    _equal: /^web[0-9]+/,  
    _post: function(err, quality, done) {
      // ensure this order id is unique
      if (!ensureUnique(this.remote)) {
        this.registerErr('not_unique_id');
        done();
      }
    }
  }, 
  created_on: { _type: 'date' }, 
  customer: { userId: { _type: 'number', _length: 5 } },
  cart: [ 
    { productId: { _type: 'number' }, price: { _type: 'number' } } 
  ]
}

quality.test(webOrderSchema, webOrder, function(err) { 
  if (!err) save_into_db;
})

Anything

It'll validate anything, even objects with function properties.

(quality uses quality to validates it's api.)

You can even use it for data normalization.

Anywhere

Since rules are simply objects, that can easily be converted to JSON, you can send it or pass it anywhere outside of javascript.

And quality.js can be run in the browser.

For example with express, render it to the template to validate client side:

  res.render('index', { rule: rule});
<script src='./quality.js'></script>
<script>
  var rule = {{{ rule }}};
  quality.test(rule, test, function(err) {
    // ...
  });
</script>

Validation Rules / Keywords

type any common type ( string, number, array, regexp, number, date, boolean, function, objectLiteral )

equal a regexp, a exact value (===), or an exact value of .toString()

length min or max length of the value

post a custom function to run your own validation, but most importantly it is async and is deferred till after all other validations are done

Extendable

You can add your own type

You can add your own rule (or special keyword)

Browser Support

Haven't tested yet, should be IE 9+, FF 4+, Chrome, Safari ??, Opera ??

Something went wrong with that request. Please try again.