Skip to content

Commit

Permalink
Added old messages validation for backward compatibility
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 ee703eb commit 0409366
Showing 1 changed file with 116 additions and 13 deletions.
129 changes: 116 additions & 13 deletions webinos/core/util/lib/schema.js
Expand Up @@ -26,24 +26,42 @@
var assert = require('assert');

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

// generic message validation
var validation = checkByType(msg, 'generic');

// validation error is false, so generic message validation is ok
if(validation === false) {
return checkByType(msg, msg.type);
}
else {
// generic message validation failed
return validation;
// Validate messages defined in D3.3
// http://dev.webinos.org/redmine/projects/wp3-3/wiki/Messaging_and_Routing
if (version && version === 'D3.3') {
// generic message validation
var validation = checkByType(msg, 'generic');

// validation error is false, so generic message validation is ok
if(validation === false) {
return checkByType(msg, msg.type);
}
else {
// generic message validation failed
return validation;
}

// validate old messages
} else {
// 'type' field validation
var validation = checkByType(msg, 'oldGeneric');

// validation error is false, so validation is ok
if(validation === false) {
return checkByType(msg, msg.type);
} else {
// 'type' field validation failed
return validation;
}
}
};

Expand Down Expand Up @@ -159,6 +177,91 @@
},
'additionalProperties': true
});
// old type, not compliant with D3.3
// added for backward compatibility
} else if (type === 'oldGeneric') {
schema = myEnv.Schema.create({
'type': 'object',
'properties':{
'type': {
'type': 'string',
'enum': ['JSONRPC', 'prop']
}
},
// other fields allowed because in this function only 'type'
// field is tested
'additionalProperties': true
});
// old type, not compliant with D3.3
// added for backward compatibility
} else if (type === 'prop') {
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':[]
}
},
'additionalProperties': false
});
// old type, not compliant with D3.3
// added for backward compatibility
} else if (type === 'JSONRPC') {
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':[]
}
},
'additionalProperties': false
});
} else {
console.log('Invalid message type: ' + type);
return true;
Expand All @@ -167,7 +270,7 @@
validation = schema.validate(message);
assert.strictEqual(validation.isError(), false);
return validation.isError();
} catch (err) {
} catch(err) {
console.log(type + ' message validation failed');
console.log(validation.getError().errors);
return true;
Expand Down

0 comments on commit 0409366

Please sign in to comment.