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(
) {
'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) {
......@@ -115,6 +135,7 @@ define(
}
var NestedModels = {
extractNestedErrors: extractNestedErrors,
validateNestedValue: validateNestedValue,
wrapSetFunction: wrapSetFunction,
......
......@@ -263,10 +263,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});
......@@ -308,10 +313,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 {
......@@ -321,10 +331,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 {
......@@ -334,10 +348,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 {
......