2c606432 by Ean Schuessler

Merge branch 'BF-2620' of /home/git/repositories/brainfood/backbone-nested-models

2 parents 0976fcad adc117a1
......@@ -10,10 +10,30 @@ define(
) {
'use strict';
function extractNestedErrors(errors) {
var result = {};
for (var key in errors) {
var value = errors[key];
if (value.errors) {
value = extractNestedErrors(value.errors);
}
result[key] = value;
}
return result;
}
function validateNestedValue(attrValue, attrName) {
attrValue.validate();
var errors = attrValue.validate();
var isValid = attrValue.isValid();
return isValid ? null : (attrName + ' is invalid');
if (isValid) {
return null;
}
var r = {};
r.toString = function() {
return attrName + ' is invalid';
};
r.errors = errors;
return r;
}
function updateValidation(model) {
......@@ -120,6 +140,7 @@ define(
}
var NestedModels = {
extractNestedErrors: extractNestedErrors,
validateNestedValue: validateNestedValue,
wrapSetFunction: wrapSetFunction,
......
......@@ -290,10 +290,15 @@ define(function(require) {
expect(top.isValid()).toBeUndefined();
result = top.validate();
expect(result).not.toBeNull();
expect(result.nested).not.toEqual(jasmine.any(String));
expect(result.nested.toString()).toEqual('nested is invalid');
result = NestedModels.extractNestedErrors(result);
rKeys = _.keys(result).sort();
expect(rKeys).toEqual(['name', 'nested']);
expect(result.name).toEqual(jasmine.any(String));
expect(result.nested).toEqual(jasmine.any(String));
expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
expect(result.nested.address1).toEqual(jasmine.any(String));
expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
expect(top.isValid()).toBe(false);
if (checkCounts) {
expect(top.counts).toEqual({set: 1, isValid: 2, validate: 1});
......@@ -335,10 +340,15 @@ define(function(require) {
];
result = top.validate();
expect(result).not.toBeNull();
expect(result.nested).not.toEqual(jasmine.any(String));
expect(result.nested.toString()).toEqual('nested is invalid');
result = NestedModels.extractNestedErrors(result);
rKeys = _.keys(result).sort();
expect(rKeys).toEqual(['name', 'nested']);
expect(result.name).toEqual('name IS required');
expect(result.nested).toEqual(jasmine.any(String));
expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
expect(result.nested.address1).toEqual(jasmine.any(String));
expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
if (checkCounts) {
expect(top.counts).toEqual({set: 1, isValid: 2, validate: 4});
} else {
......@@ -348,10 +358,14 @@ define(function(require) {
top.get('nested').set('address1', '1234 Main St.');
result = top.validate();
expect(result).not.toBeNull();
expect(result.nested).not.toEqual(jasmine.any(String));
expect(result.nested.toString()).toEqual('nested is invalid');
result = NestedModels.extractNestedErrors(result);
rKeys = _.keys(result).sort();
expect(rKeys).toEqual(['name', 'nested']);
expect(result.name).toEqual('name IS required');
expect(result.nested).toEqual(jasmine.any(String));
expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
if (checkCounts) {
expect(top.counts).toEqual({set: 1, isValid: 2, validate: 5});
} else {
......@@ -361,10 +375,14 @@ define(function(require) {
top.get('nested').set('countryGeoId', 'foobar');
result = top.validate();
expect(result).not.toBeNull();
expect(result.nested).not.toEqual(jasmine.any(String));
expect(result.nested.toString()).toEqual('nested is invalid');
result = NestedModels.extractNestedErrors(result);
rKeys = _.keys(result).sort();
expect(rKeys).toEqual(['name', 'nested']);
expect(result.name).toEqual('name IS required');
expect(result.nested).toEqual(jasmine.any(String));
expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
if (checkCounts) {
expect(top.counts).toEqual({set: 1, isValid: 2, validate: 6});
} else {
......