AnyWhichWay
Introducing JOVIAL

Home > JOVIAL January 26th, 2016

JOVIAL stands for JavaScript Object Validation Interception Augmentation Library.

JOVIAL is light weight, easily extensible MIT license validation mechanism for JavaScript objects that supports both batch and real-time per property validation. It can be used on both the client or the server. It is ideal for a thin validation layer on top of a JSON document store. It includes a Proxy implementation for node.js while Google finishes re-adding Proxies to the v8 engine.

Here is an example Validator:

new Validator({name: {type: 'string', length: [2,25], required: true}, 
                  age: {type: 'number', min:0, max: 110},
                  gender: {in: ['male','female']},
                  ssn: {type: 'SSN'},
                  location: {type: 'latlon'}})

Also supported are RegExp matching, soundex testing, range testing, developer supplied functions.

Using JOVIAL

The structure of a JOVIAL validator parallels the structure of the JavaScript object to be validated, i.e. for each property on which validation is required there should be a similarly named property in the Validator.

Validator properties are objects which describe the nature of constraints on the target object to be validated. These constraints can include such things as type, length, regular expression matching, etc. Types go beyond the built-in primitive types for JavaScript to include SSN, telephone, email, and latitude/longitude.

To enable validation just create a Validator object and bind it to the object to be validated, e.g.

var person = {name: "Bill",age: undefined, gender: undefined, ssn: undefined, email: undefined, 'echoes (true or tru)': undefined}
var validator = new Validator({name: {type: 'string', length: [0,25], required: true, transform:function(value) { return (value && value.length>0 ? value : undefined); }}, 
                  age: {type: 'number', min:0, max: 110, transform:function(value) { return (value!==undefined ? parseInt(value) : undefined); }},
                  gender: {in: ['male','female']},
                  ssn: {type: 'SSN', transform:function(value) { return (value && value.length>0 ? value : undefined); }},
                  email: {type: 'email'},
                  'echoes (true or tru)': {echoes: 'true'}});
validator.bind(person);            
Try typing values into the form below. You should see errors below the form when you provide invalid values.

Additional Info

As you can see Validators have no presentation layer. However, they can be built on the fly to match presentation layer code. Or, presentation layer code could be generated based on Validators. At AnyWhichWay we have tied the same Validators into Ractive based web pages server based code to ensure all objects are consistently tested.

For more documentation, including instructions on adding your own validation types with just one or two lines of code and augmenting existing JavaScript constructors, see http://www.github.com/anywhichway/jovial.

Copyright 2016, AnyWhichWay