Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created April 20, 2011 01:48
Show Gist options
  • Save jimfleming/930167 to your computer and use it in GitHub Desktop.
Save jimfleming/930167 to your computer and use it in GitHub Desktop.
returns an object describing any missing attributes nested within an object
_.mixin({
missing: function(obj, requirements) {
var missing = {};
_.each(requirements, function(requirement, field) {
if (_.isNull(obj[field]) || _.isUndefined(obj[field])) {
missing[field] = false;
return;
}
if (typeof obj[field] === 'object') {
potential = _.missing(obj[field], requirement);
if (typeof potential !== 'null' && (typeof potential == 'object' && _.size(potential) > 0)) {
missing[field] = potential;
}
}
});
return missing;
}
});
var context = { one: {}, two: { one: 'ONE', two: 'TWO', three: 'THREE' } };
var requirements = { one: { two: true } };
var missing = _.missing(context, requirements);
console.log(missing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment