adc117a1 by Adam Heath

Issue #2620: Add a way to store the nested validation errors, and add a

helper method that can be used to extract them.
1 parent 3a21b2ec
...@@ -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) {
...@@ -115,6 +135,7 @@ define( ...@@ -115,6 +135,7 @@ define(
115 } 135 }
116 136
117 var NestedModels = { 137 var NestedModels = {
138 extractNestedErrors: extractNestedErrors,
118 validateNestedValue: validateNestedValue, 139 validateNestedValue: validateNestedValue,
119 140
120 wrapSetFunction: wrapSetFunction, 141 wrapSetFunction: wrapSetFunction,
......
...@@ -263,10 +263,15 @@ define(function(require) { ...@@ -263,10 +263,15 @@ define(function(require) {
263 expect(top.isValid()).toBeUndefined(); 263 expect(top.isValid()).toBeUndefined();
264 result = top.validate(); 264 result = top.validate();
265 expect(result).not.toBeNull(); 265 expect(result).not.toBeNull();
266 expect(result.nested).not.toEqual(jasmine.any(String));
267 expect(result.nested.toString()).toEqual('nested is invalid');
268 result = NestedModels.extractNestedErrors(result);
266 rKeys = _.keys(result).sort(); 269 rKeys = _.keys(result).sort();
267 expect(rKeys).toEqual(['name', 'nested']); 270 expect(rKeys).toEqual(['name', 'nested']);
268 expect(result.name).toEqual(jasmine.any(String)); 271 expect(result.name).toEqual(jasmine.any(String));
269 expect(result.nested).toEqual(jasmine.any(String)); 272 expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
273 expect(result.nested.address1).toEqual(jasmine.any(String));
274 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
270 expect(top.isValid()).toBe(false); 275 expect(top.isValid()).toBe(false);
271 if (checkCounts) { 276 if (checkCounts) {
272 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 1}); 277 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 1});
...@@ -308,10 +313,15 @@ define(function(require) { ...@@ -308,10 +313,15 @@ define(function(require) {
308 ]; 313 ];
309 result = top.validate(); 314 result = top.validate();
310 expect(result).not.toBeNull(); 315 expect(result).not.toBeNull();
316 expect(result.nested).not.toEqual(jasmine.any(String));
317 expect(result.nested.toString()).toEqual('nested is invalid');
318 result = NestedModels.extractNestedErrors(result);
311 rKeys = _.keys(result).sort(); 319 rKeys = _.keys(result).sort();
312 expect(rKeys).toEqual(['name', 'nested']); 320 expect(rKeys).toEqual(['name', 'nested']);
313 expect(result.name).toEqual('name IS required'); 321 expect(result.name).toEqual('name IS required');
314 expect(result.nested).toEqual(jasmine.any(String)); 322 expect(_.keys(result.nested).sort()).toEqual(['address1', 'countryGeoId']);
323 expect(result.nested.address1).toEqual(jasmine.any(String));
324 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
315 if (checkCounts) { 325 if (checkCounts) {
316 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 4}); 326 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 4});
317 } else { 327 } else {
...@@ -321,10 +331,14 @@ define(function(require) { ...@@ -321,10 +331,14 @@ define(function(require) {
321 top.get('nested').set('address1', '1234 Main St.'); 331 top.get('nested').set('address1', '1234 Main St.');
322 result = top.validate(); 332 result = top.validate();
323 expect(result).not.toBeNull(); 333 expect(result).not.toBeNull();
334 expect(result.nested).not.toEqual(jasmine.any(String));
335 expect(result.nested.toString()).toEqual('nested is invalid');
336 result = NestedModels.extractNestedErrors(result);
324 rKeys = _.keys(result).sort(); 337 rKeys = _.keys(result).sort();
325 expect(rKeys).toEqual(['name', 'nested']); 338 expect(rKeys).toEqual(['name', 'nested']);
326 expect(result.name).toEqual('name IS required'); 339 expect(result.name).toEqual('name IS required');
327 expect(result.nested).toEqual(jasmine.any(String)); 340 expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
341 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
328 if (checkCounts) { 342 if (checkCounts) {
329 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 5}); 343 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 5});
330 } else { 344 } else {
...@@ -334,10 +348,14 @@ define(function(require) { ...@@ -334,10 +348,14 @@ define(function(require) {
334 top.get('nested').set('countryGeoId', 'foobar'); 348 top.get('nested').set('countryGeoId', 'foobar');
335 result = top.validate(); 349 result = top.validate();
336 expect(result).not.toBeNull(); 350 expect(result).not.toBeNull();
351 expect(result.nested).not.toEqual(jasmine.any(String));
352 expect(result.nested.toString()).toEqual('nested is invalid');
353 result = NestedModels.extractNestedErrors(result);
337 rKeys = _.keys(result).sort(); 354 rKeys = _.keys(result).sort();
338 expect(rKeys).toEqual(['name', 'nested']); 355 expect(rKeys).toEqual(['name', 'nested']);
339 expect(result.name).toEqual('name IS required'); 356 expect(result.name).toEqual('name IS required');
340 expect(result.nested).toEqual(jasmine.any(String)); 357 expect(_.keys(result.nested).sort()).toEqual(['countryGeoId']);
358 expect(result.nested.countryGeoId).toEqual(jasmine.any(String));
341 if (checkCounts) { 359 if (checkCounts) {
342 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 6}); 360 expect(top.counts).toEqual({set: 1, isValid: 2, validate: 6});
343 } else { 361 } else {
......