Skip to content

Commit

Permalink
Generic message validation
Browse files Browse the repository at this point in the history
Jira-issue: WP-349
  • Loading branch information
ccameroni committed Apr 18, 2013
1 parent bc14fb1 commit f7820c7
Showing 1 changed file with 43 additions and 140 deletions.
183 changes: 43 additions & 140 deletions webinos/core/util/lib/schema.js
Expand Up @@ -14,180 +14,83 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011 Torsec -Computer and network security group-
* Copyright 2011-2013 Torsec -Computer and network security group-
* Politecnico di Torino
*******************************************************************************/

var schema = exports;

/**
* Validates messages
* Validate messages defined in D3.3
* http://dev.webinos.org/redmine/projects/wp3-3/wiki/Messaging_and_Routing
* @name checkSchema
* @function
* @param msg Message to validate
*/
schema.checkSchema = function(msg) {

// "type" field validation
var validation = checkTypeSchema(msg);
// generic message validation
var validation = checkGenericSchema(msg);

if(validation === false) { // validation error is false, so validation is ok
if (msg.type === "prop") {
// "prop" type message validation
return checkPropSchema(msg);
}
if (msg.type === "JSONRPC") {
// "JSONRPC" type message validation
return checkJSONRPCSchema(msg);
}
// TODO: check message type and call right schema check
}
else {
// "type" field validation failed
// generic message validation failed
return validation;
}
};

/**
* Validates "prop" type messages
* @name checkPropSchema
* Validate generic message schema
* @name checkGenericSchema
* @function
* @param msg Message to validate
*/
checkPropSchema = function(message) {
checkGenericSchema = function(message) {
var myEnv, assert, schema, validation;

myEnv = require("schema")("myEnvironment", { fallbacks: "STRICT_FALLBACKS" });
assert = require("assert");

// "prop" type package schema
// required fields: "type", "from", "to" and "payload"
// "deliveryNotification", "JSONRPC20Request", "JSONRPC20Response" and "Prop"
// types are allowed
schema = myEnv.Schema.create({
"type": "object",
"properties":{
"type": {
"type": "string",
"enum": ["prop"]
},
"from": {
"type": ["string","null"],
"minLength": 0,
"maxLength": 99,
"default": ""
},
"to": {
"type": ["string","null"],
"minLength": 0,
"maxLength": 99,
"default": ""
},
"payload": {
"type": "object",
"default":[]
}
"properties": {
"type": {
"type": "string",
"enum": ["deliveryNotification", "JSONRPC20Request",
"JSONRPC20Response", "Prop"]
},
"from": {
"type": "string"
},
"to": {
"type": "string"
},
"id": {
"type": "string"
},
"timestamp": {
"type": "string",
"optional": true
},
"expires": {
"type": "string",
"optional": true
},
"deliveryReceipt": {
"type": "boolean",
"optional": true
},
"payload": {
"type": ["object", "string", "null"],
"optional": true
}
},
// no other fields allowed
"additionalProperties": true
});
try {
validation = schema.validate(message);
assert.strictEqual(validation.isError(), false);
return validation.isError();
} catch (err2) {
console.log(validation.getError());
console.log(validation.getError().errors);
return true;
}
};

/**
* Validates "JSONRPC" type messages
* @name checkJSONRPCSchema
* @function
* @param msg Message to validate
*/
checkJSONRPCSchema = function(message) {
var myEnv, assert, schema, validation;

myEnv = require("schema")("myEnvironment", { fallbacks: "STRICT_FALLBACKS" });
assert = require("assert");

// "JSONRPC" package schema
// required fields: "type", "from", "to" and "payload"
// optiona fields: "register", "id", "resp_to"
schema = myEnv.Schema.create({
"type": "object",
"properties":{
"register": {
"type":"boolean",
"optional" : true
},
"id": {
"type": "number",
"optional" : true
},
"type": {
"type": "string",
"enum": ["JSONRPC"]
},
"from": {
"type": ["string","null"],
"minLength": 0,
"maxLength": 99
},
"to": {
"type": ["string","null"],
"minLength": 0,
"maxLength": 99
},
"resp_to": {
"type": "string",
"minLength": 0,
"maxLength": 99,
"optional" : true
},
"payload": {
"type": ["object", "null"],
"default":[]
}
},
// no other fields allowed
"additionalProperties": false
});
try {
validation = schema.validate(message);
assert.strictEqual(validation.isError(), false);
return validation.isError();
} catch (err2) {
console.log(validation.getError());
console.log(validation.getError().errors);
return true;
}
};

/**
* Validates "type" field
* @name checkTypeSchema
* @function
* @param msg Message to validate
*/
checkTypeSchema = function(message) {
var myEnv, assert, schema, validation;

myEnv = require("schema")("myEnvironment", { fallbacks: "STRICT_FALLBACKS" });
assert = require("assert");

// only "prop" and "JSONRPC" types are allowed
schema = myEnv.Schema.create({
"type": "object",
"properties":{
"type": {
"type": "string",
"enum": ["JSONRPC", "prop"]
}
},
// other fields allowed (in this function we test only "type" field)
"additionalProperties": true
});
try {
validation = schema.validate(message);
assert.strictEqual(validation.isError(), false);
Expand Down

0 comments on commit f7820c7

Please sign in to comment.