truflux-msg-schema
v1.0.0
Published
A schema enforcement mechanism for payloads for truflux-msg
Downloads
13
Maintainers
Readme
Truflux Message Schema
To install
npm install --save truflux-msg-schema
This module is used to wrap message handlers one would use with a truflux-msg module to check that the payload data conforms to a specified schema
var EnforceSchema = require ('truflux-msg-schema');
/*
Lets assume that the login function must be provided with an object that must have an id number property
and a password string property
*/
function loginFunction(id, payload, socket)
{
/**
* Do something
*/
}
function schemaErrorHandler(id,payload,socket)
{
socket.send('SomeErrorMessage','Your message was not in the correct schema');
}
var schema=
{
type:'object',
props:
{
id : {type :'number'},
password : {type :'string'},
}
}
var wrappedLogin = schemaEnforce(loginFunction,schema,schemaErrorHandler),
SomeTrufluxMessageInstance.add('login',wrappedLogin);
API
/**
* Wraps a function in a schema enforce to make sure that the payload matches a specified schema
* @param {Function} handler function(id,payload,socket)
* @param {Object} schema
* @param {Function} schemaFailedCallback A callback that must be called if the schema is not satisfied function(id,payload,socket,)
* @return {Function} function(id,payload,socket) The wrapped function
*/
The schema object's type can be *
to allow any type, or it can be an array if you want to allow multiple types
var someSchema=
{
type : 'object'
props :
{
bufferProperty : { type : 'buffer' },
arrayProperty : { type : 'array' },
booleanProperty : { type : 'boolean'},
stringProperty : { type : 'string' },
numberProperty : { type : 'number' },
anyProperty : { type : '*' },
multipleProperty : { type : ['object','undefined']},
nestedProperty :
{
type:'object',
props:
{
somePropert : {type:'*}
}
}
}
}