Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use different source for custom validation #314

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading