Skip to content

Commit

Permalink
Validate instance against schema on save
Browse files Browse the repository at this point in the history
This replaces the throwing of errors when assigning values to root level
schema properties and means that child elements are now validated when
saving.

NOTE: This still doesn't validate for custom save commands { $set: {} } etc.
  • Loading branch information
notheotherben committed Jan 14, 2014
1 parent 53fbeff commit d8134a9
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/Instance.js
Expand Up @@ -126,6 +126,9 @@ Instance.prototype.save = function(conditions, changes, callback) {
}

if(!changes) {
var validation = validate(schema, this.__state.modified, undefined, this.__state.model.extraValidators);
if(!validation.passed) return callback(validation.toError());

var original = _.cloneDeep(this.__state.original);
var modified = _.cloneDeep(this.__state.modified);

Expand Down Expand Up @@ -232,9 +235,7 @@ Instance.prototype.__extendSchema = function() {
},
set: function(value) {
/// <value type="Object">Set the value of this field. Changes may be committed by calling save() on this instance.</value>
var validation = validate(schema[targetProperty], value, targetProperty, this.__state.model.extraValidators);
if (!validation.passed) throw validation.toError();
$.__state.modified[targetProperty] = value;
$.__state.modified[targetProperty] = value;
},
enumerable: true
});
Expand Down Expand Up @@ -269,8 +270,6 @@ Instance.forModel = function(model) {
return this.__state.modified[name] === undefined ? null : this.__state.modified[name];
},
set: function(value) {
var validation = validate(validator, value, name, model.extraValidators);
if(!validation.passed) throw validation.toError();
this.__state.modified[name] = value;
},
enumerable: true
Expand Down

0 comments on commit d8134a9

Please sign in to comment.