Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unserialize method to 'object' datatype #53

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
20 changes: 20 additions & 0 deletions lib/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,26 @@ datatypes = {
// FIXME: Does escaping a JSONized object make sense?
return _serialize(val, opts);
}
, unserialize: function(input, options) {
// unserialize strings only
if (typeof input == 'string') {
// catch exceptions from JSON.parse and return a null object instead
try {
return JSON.parse(input);
}
catch (err) {
return null;
}
}
// if it's already an object we do not need to unserialize
else if (typeof input == 'object') {
return input;
}
// as a fallback return a null object
else {
return null;
}
}
}

, 'date': {
Expand Down
32 changes: 31 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ utils.mixin(model, new (function () {

obj.all = function () {
var args = Array.prototype.slice.call(arguments)
, Query = Query || require('./query/query').Query
, callback = args.pop() || function () {}
, query = args.shift() || {}
, opts = args.shift() || {}
Expand Down Expand Up @@ -580,6 +581,7 @@ utils.mixin(model, new (function () {

obj.update = function () {
var args = Array.prototype.slice.call(arguments)
, Query = Query || require('./query/query').Query
, data
, callback
, query
Expand Down Expand Up @@ -629,6 +631,7 @@ utils.mixin(model, new (function () {

obj.remove = function () {
var args = Array.prototype.slice.call(arguments)
, Query = Query || require('./query/query').Query
, query
, callback
, opts
Expand Down Expand Up @@ -755,13 +758,17 @@ utils.mixin(model, new (function () {
, name = item.type
, type = model.descriptionRegistry[name]
, properties = type.properties
, property
, validated = null
, errs = null
, camelizedKey
, skip = opts.skip
, skipKeys = {}
, datatype
, val;

this.datatypes = this.datatypes || require('./datatypes');

item.emit('beforeValidate')
model[name].emit('beforeValidate', item, passedParams);

Expand All @@ -774,7 +781,28 @@ utils.mixin(model, new (function () {
for (var p in passedParams) {
// Allow leading underscores in the keys for pseudo-privates
camelizedKey = utils.string.camelize(p, {leadingUnderscore: true});
params[camelizedKey] = passedParams[p];

// user defined properties might need unserialization
if (typeof properties[camelizedKey] != 'undefined') {
property = properties[camelizedKey];

if (typeof this.datatypes[property.datatype] != 'undefined') {
datatype = this.datatypes[property.datatype];

// unserialize the property if it's datatype has an unserialize method
if (typeof datatype['unserialize'] == 'function') {
val = datatype.unserialize(passedParams[p]);
} else {
val = passedParams[p];
}
} else {
val = passedParams[p];
}
} else {
val = passedParams[p];
}

params[camelizedKey] = val;
}
}
else {
Expand Down Expand Up @@ -835,6 +863,8 @@ utils.mixin(model, new (function () {

this.validateProperty = function (prop, params, opts) {

this.datatypes = this.datatypes || require('./datatypes');

var options = opts || {}
, name = prop.name
, val = params[name]
Expand Down