Gets the inherited property validation working from the server side.
This commit is contained in:
@@ -123,52 +123,25 @@ function serverValidationManager($timeout, udiService) {
|
||||
* Returns a dictionary of id (of the block) and it's corresponding validation ModelState
|
||||
* @param {any} errorMsg
|
||||
*/
|
||||
function parseComplexEditorError(errorMsg, culture, segment) {
|
||||
|
||||
//normalize culture to "invariant"
|
||||
if (!culture) {
|
||||
culture = "invariant";
|
||||
}
|
||||
//normalize segment to "null" (as a string, in this case it's used for creating a key)
|
||||
if (!segment) {
|
||||
segment = "null";
|
||||
}
|
||||
function parseComplexEditorError(errorMsg) {
|
||||
|
||||
var json = JSON.parse(errorMsg);
|
||||
|
||||
var result = {};
|
||||
|
||||
function extractModelState(validation) {
|
||||
if (validation.$id && validation.ModelState && Object.keys(validation.ModelState).length > 0) {
|
||||
if (validation.$id && validation.ModelState) {
|
||||
result[validation.$id] = validation.ModelState;
|
||||
}
|
||||
else {
|
||||
// we'll still add the id in the dictionary with an empty result, this indicates that this element
|
||||
// has nested errors but no errors itself
|
||||
result[validation.$id] = {};
|
||||
}
|
||||
return result[validation.$id];
|
||||
}
|
||||
|
||||
function iterateErrorBlocks(blocks) {
|
||||
for (var i = 0; i < blocks.length; i++) {
|
||||
var validation = blocks[i];
|
||||
var modelState = extractModelState(validation);
|
||||
var hasModelState = Object.keys(modelState).length > 0;
|
||||
extractModelState(validation);
|
||||
var nested = _.omit(validation, "$id", "$elementTypeAlias", "ModelState");
|
||||
for (const [key, value] of Object.entries(nested)) {
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
|
||||
// The key here is the property type alias of the nested validation.
|
||||
// If the extracted ModelState is empty it indicates that this element
|
||||
// has nested errors but no errors itself. In that case we need to manually populate the
|
||||
// ModelState properties.
|
||||
if (!hasModelState) {
|
||||
var propertyKey = "_Properties." + key + "." + culture + "." + segment;
|
||||
modelState[propertyKey] = null;
|
||||
}
|
||||
|
||||
iterateErrorBlocks(value); // recurse
|
||||
}
|
||||
}
|
||||
@@ -309,7 +282,7 @@ function serverValidationManager($timeout, udiService) {
|
||||
// if the error message is json it's a complex editor validation response that we need to parse
|
||||
if (errorMsg.startsWith("[")) {
|
||||
|
||||
var idsToErrors = parseComplexEditorError(errorMsg, culture, segment);
|
||||
var idsToErrors = parseComplexEditorError(errorMsg);
|
||||
for (const [key, value] of Object.entries(idsToErrors)) {
|
||||
const elementUdi = udiService.build("element", key);
|
||||
addErrorsForModelState(value, elementUdi);
|
||||
|
||||
@@ -316,7 +316,7 @@
|
||||
|
||||
describe('managing complex editor validation errors', function () {
|
||||
|
||||
it('create json paths for complex validation error', function () {
|
||||
it('create dictionary of id to ModelState', function () {
|
||||
|
||||
//arrange
|
||||
var complexValidationMsg = `[
|
||||
@@ -377,6 +377,71 @@
|
||||
|
||||
});
|
||||
|
||||
it('create dictionary of id to ModelState with inherited errors', function () {
|
||||
|
||||
// arrange
|
||||
// this root element doesn't have it's own attached errors, instead it has model state just
|
||||
// showing that it has errors within it's nested properties. that ModelState is automatically
|
||||
// added on the server side.
|
||||
var complexValidationMsg = `[
|
||||
{
|
||||
"$elementTypeAlias": "addressBook",
|
||||
"$id": "34E3A26C-103D-4A05-AB9D-7E14032309C3",
|
||||
"addresses":
|
||||
[
|
||||
{
|
||||
"$elementTypeAlias": "addressInfo",
|
||||
"$id": "FBEAEE8F-4BC9-43EE-8B81-FCA8978850F1",
|
||||
"ModelState":
|
||||
{
|
||||
"_Properties.city.invariant.null.country": [
|
||||
"City is not in Australia"
|
||||
],
|
||||
"_Properties.city.invariant.null.capital": [
|
||||
"Not a capital city"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$elementTypeAlias": "addressInfo",
|
||||
"$id": "7170A4DD-2441-4B1B-A8D3-437D75C4CBC9",
|
||||
"ModelState":
|
||||
{
|
||||
"_Properties.city.invariant.null.country": [
|
||||
"City is not in Australia"
|
||||
],
|
||||
"_Properties.city.invariant.null.capital": [
|
||||
"Not a capital city"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"ModelState":
|
||||
{
|
||||
"_Properties.addresses.invariant.null": [
|
||||
""
|
||||
]
|
||||
}
|
||||
}
|
||||
]`;
|
||||
|
||||
//act
|
||||
var ids = serverValidationManager.parseComplexEditorError(complexValidationMsg);
|
||||
|
||||
//assert
|
||||
var keys = Object.keys(ids);
|
||||
|
||||
expect(keys.length).toEqual(3);
|
||||
expect(keys[0]).toEqual("34E3A26C-103D-4A05-AB9D-7E14032309C3");
|
||||
var item0ModelState = ids["34E3A26C-103D-4A05-AB9D-7E14032309C3"];
|
||||
expect(Object.keys(item0ModelState).length).toEqual(1);
|
||||
expect(item0ModelState["_Properties.addresses.invariant.null"].length).toEqual(1);
|
||||
expect(item0ModelState["_Properties.addresses.invariant.null"][0]).toEqual("");
|
||||
expect(keys[1]).toEqual("FBEAEE8F-4BC9-43EE-8B81-FCA8978850F1");
|
||||
expect(keys[2]).toEqual("7170A4DD-2441-4B1B-A8D3-437D75C4CBC9");
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('validation error subscriptions', function() {
|
||||
|
||||
Reference in New Issue
Block a user