Skip to content

Schema-Enforcer is a powerful tool to enforce schema on JS objects.

License

Notifications You must be signed in to change notification settings

sbansal6/schema-enforcer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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).

<script type="text/javascript" src="bower_components/async/lib/async.js"></script>
<script type="text/javascript" src="bower_components/schema-inspector/lib/schema-inspector.js"></script>
<script type="text/javascript" src="bower_components/schema-enforcer/lib/schema-enforcer.js"></script>

Usage

var SchemaEnforcer = require('schema-enforcer');

// Define User Schema
var User = new SchemaEnforcer({
    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 array
var Rob = new User({
    firstname: 'rob',
    lastname: 'archer',
    jobs: 'Special agent, cocaine Dealer',
    email: 'NEVER!',
})



// create new user alex
// will return an instance of user
var alex = new User({
    firstname: 'alex',
    lastname: 'knight',
    jobs: ['Special agent, cocaine Dealer'],
    email: 'NEVER!',
})

In the browser

<script type="text/javascript" src="async.js"></script>
<script type="text/javascript" src="schema-inspetor.js"></script>
<script type="text/javascript" src="schema-enforcer.js"></script>
<script type="text/javascript">
     // Define User Schema
     var User = new SchemaEnforcer({
         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 array
     var Rob = new User({
         firstname: 'rob',
         lastname: 'archer',
         jobs: 'Special agent, cocaine Dealer',
         email: 'NEVER!',
     })
     
     
     
     // create new user alex
     // will return and instance of user
     var alex = new User({
         firstname: 'alex',
         lastname: 'knight',
         jobs: ['Special agent, cocaine Dealer'],
         email: 'NEVER!',
     })
</script>

About

Schema-Enforcer is a powerful tool to enforce schema on JS objects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published