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( ...@@ -10,10 +10,30 @@ define(
10 ) { 10 ) {
11 'use strict'; 11 'use strict';
12 12
13 function extractNestedErrors(errors) {
14 var result = {};
15 for (var key in errors) {
16 var value = errors[key];
17 if (value.errors) {
18 value = extractNestedErrors(value.errors);
19 }
20 result[key] = value;
21 }
22 return result;
23 }
24
13 function validateNestedValue(attrValue, attrName) { 25 function validateNestedValue(attrValue, attrName) {
14 attrValue.validate(); 26 var errors = attrValue.validate();
15 var isValid = attrValue.isValid(); 27 var isValid = attrValue.isValid();
16 return isValid ? null : (attrName + ' is invalid'); 28 if (isValid) {
29 return null;
30 }
31 var r = {};
32 r.toString = function() {
33 return attrName + ' is invalid';
34 };
35 r.errors = errors;
36 return r;
17 } 37 }
18 38
19 function updateValidation(model) { 39 function updateValidation(model) {
...@@ -120,6 +140,7 @@ define( ...@@ -120,6 +140,7 @@ define(
120 } 140 }
121 141
122 var NestedModels = { 142 var NestedModels = {
143 extractNestedErrors: extractNestedErrors,
123 validateNestedValue: validateNestedValue, 144 validateNestedValue: validateNestedValue,
124 145
125 wrapSetFunction: wrapSetFunction, 146 wrapSetFunction: wrapSetFunction,
......
...@@ -290,10 +290,15 @@ define(function(require) { ...@@ -290,10 +290,15 @@ define(function(require) {
290 expect(top.isValid()).toBeUndefined(); 290 expect(top.isValid()).toBeUndefined();
291 result = top.validate(); 291 result = top.validate();
292 expect(result).not.toBeNull(); 292 expect(result).not.toBeNull();
293 expect(result.nested).not.toEqual(jasmine.any(String));
294 expect(result.nested.toString()).toEqual('nested is invalid');
295 result = NestedModels.extractNestedErrors(result);
293 rKeys = _.keys(result).sort(); 296 rKeys = _.keys(result).sort();
294 expect(rKeys).toEqual(['name', 'nested']); 297 expect(rKeys).toEqual(['name', 'nested']);
295 expect(result.name).toEqual(jasmine.any(String)); 298 expect(result.name).toEqual(jasmine.any(String));
296 expect(result.nested).toEqual(jasmine.any(String)); 299 expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
300 expect(result.nested.address1).toEqual(jasmine.any(String));
301 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
297 expect(top.isValid()).toBe(false); 302 expect(top.isValid()).toBe(false);
298 if (checkCounts) { 303 if (checkCounts) {
299 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 1}); 304 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 1});
...@@ -335,10 +340,15 @@ define(function(require) { ...@@ -335,10 +340,15 @@ define(function(require) {
335 ]; 340 ];
336 result = top.validate(); 341 result = top.validate();
337 expect(result).not.toBeNull(); 342 expect(result).not.toBeNull();
343 expect(result.nested).not.toEqual(jasmine.any(String));
344 expect(result.nested.toString()).toEqual('nested is invalid');
345 result = NestedModels.extractNestedErrors(result);
338 rKeys = _.keys(result).sort(); 346 rKeys = _.keys(result).sort();
339 expect(rKeys).toEqual(['name', 'nested']); 347 expect(rKeys).toEqual(['name', 'nested']);
340 expect(result.name).toEqual('name IS required'); 348 expect(result.name).toEqual('name IS required');
341 expect(result.nested).toEqual(jasmine.any(String)); 349 expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
350 expect(result.nested.address1).toEqual(jasmine.any(String));
351 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
342 if (checkCounts) { 352 if (checkCounts) {
343 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 4}); 353 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 4});
344 } else { 354 } else {
...@@ -348,10 +358,14 @@ define(function(require) { ...@@ -348,10 +358,14 @@ define(function(require) {
348 top.get('nested').set('address1', '1234 Main St.'); 358 top.get('nested').set('address1', '1234 Main St.');
349 result = top.validate(); 359 result = top.validate();
350 expect(result).not.toBeNull(); 360 expect(result).not.toBeNull();
361 expect(result.nested).not.toEqual(jasmine.any(String));
362 expect(result.nested.toString()).toEqual('nested is invalid');
363 result = NestedModels.extractNestedErrors(result);
351 rKeys = _.keys(result).sort(); 364 rKeys = _.keys(result).sort();
352 expect(rKeys).toEqual(['name', 'nested']); 365 expect(rKeys).toEqual(['name', 'nested']);
353 expect(result.name).toEqual('name IS required'); 366 expect(result.name).toEqual('name IS required');
354 expect(result.nested).toEqual(jasmine.any(String)); 367 expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
368 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
355 if (checkCounts) { 369 if (checkCounts) {
356 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 5}); 370 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 5});
357 } else { 371 } else {
...@@ -361,10 +375,14 @@ define(function(require) { ...@@ -361,10 +375,14 @@ define(function(require) {
361 top.get('nested').set('countryGeoId', 'foobar'); 375 top.get('nested').set('countryGeoId', 'foobar');
362 result = top.validate(); 376 result = top.validate();
363 expect(result).not.toBeNull(); 377 expect(result).not.toBeNull();
378 expect(result.nested).not.toEqual(jasmine.any(String));
379 expect(result.nested.toString()).toEqual('nested is invalid');
380 result = NestedModels.extractNestedErrors(result);
364 rKeys = _.keys(result).sort(); 381 rKeys = _.keys(result).sort();
365 expect(rKeys).toEqual(['name', 'nested']); 382 expect(rKeys).toEqual(['name', 'nested']);
366 expect(result.name).toEqual('name IS required'); 383 expect(result.name).toEqual('name IS required');
367 expect(result.nested).toEqual(jasmine.any(String)); 384 expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
385 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
368 if (checkCounts) { 386 if (checkCounts) {
369 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 6}); 387 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 6});
370 } else { 388 } else {
......