Schema-Enforcer is a powerful tool to enforce schema on JS objects. It's based on powerful schema-inspector module https://github.com/Atinux/schema-inspector It's designed to work both client-side and server-side and to be scalable with allowing asynchronous and synchronous calls.
Installation
Node.js
npm install schema-enforcer
Browser
bower install schema-enforcer
(Or download async.js and schema-inspector.js and schema-enforcer.js manually).
Usage
var SchemaEnforcer = ; // Define User Schemavar User = type: 'object' properties: firstname: type: 'string' rules: 'trim' 'title' lastname: type: 'string' rules: 'trim' 'title' jobs: type: 'array' splitWith: ',' items: type: 'string' rules: 'trim' 'title' email: type: 'string' rules: 'trim' 'lower' ; // create new user rob// will throw an error as jobs property is expected to be arrayvar Rob = firstname: 'rob' lastname: 'archer' jobs: 'Special agent, cocaine Dealer' email: 'NEVER!' // create new user alex// will return an instance of uservar alex = firstname: 'alex' lastname: 'knight' jobs: 'Special agent, cocaine Dealer' email: 'NEVER!'
In the browser