Skip to content

Commit

Permalink
fix custom validation source (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit authored Sep 25, 2023
1 parent dc40eae commit 87fe4b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Altinn.App.Core/Features/Validation/ValidationAppSI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,38 @@ public async Task<List<ValidationIssue>> ValidateDataElement(Instance instance,
messages.AddRange(layoutErrors);
}

// run Standard mvc validation using the System.ComponentModel.DataAnnotations
ModelStateDictionary validationResults = new ModelStateDictionary();
// Run Standard mvc validation using the System.ComponentModel.DataAnnotations
ModelStateDictionary dataModelValidationResults = new ModelStateDictionary();
var actionContext = new ActionContext(
_httpContextAccessor.HttpContext,
new Microsoft.AspNetCore.Routing.RouteData(),
new ActionDescriptor(),
validationResults);
dataModelValidationResults);
ValidationStateDictionary validationState = new ValidationStateDictionary();
_objectModelValidator.Validate(actionContext, validationState, null, data);

if (!dataModelValidationResults.IsValid)
{
messages.AddRange(MapModelStateToIssueList(actionContext.ModelState, ValidationIssueSources.ModelState, instance, dataElement.Id, data.GetType()));
}

// Call custom validation from the IInstanceValidator
await _instanceValidator.ValidateData(data, validationResults);
ModelStateDictionary customValidationResults = new ModelStateDictionary();
await _instanceValidator.ValidateData(data, customValidationResults);

// Add the validation messages from System.ComponentModel.DataAnnotations and IInstanceValidator to the return list
if (!validationResults.IsValid)
if (!customValidationResults.IsValid)
{
messages.AddRange(MapModelStateToIssueList(actionContext.ModelState, instance, dataElement.Id, data.GetType()));
messages.AddRange(MapModelStateToIssueList(customValidationResults, ValidationIssueSources.Custom, instance, dataElement.Id, data.GetType()));
}

}

return messages;
}

private List<ValidationIssue> MapModelStateToIssueList(
ModelStateDictionary modelState,
string source,
Instance instance,
string dataElementId,
Type modelType)
Expand All @@ -282,7 +289,7 @@ private List<ValidationIssue> MapModelStateToIssueList(
{
InstanceId = instance.Id,
DataElementId = dataElementId,
Source = ValidationIssueSources.ModelState,
Source = source,
Code = severityAndMessage.Message,
Field = ModelKeyToField(modelKey, modelType)!,
Severity = severityAndMessage.Severity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public static class ValidationIssueSources
/// Required field validation
/// </summary>
public static readonly string Required = nameof(Required);

/// <summary>
/// Required field validation
/// </summary>
public static readonly string Custom = nameof(Custom);
}
}

0 comments on commit 87fe4b9

Please sign in to comment.