-
Notifications
You must be signed in to change notification settings - Fork 11
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
Start work on new validation #374
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
de315c7
Start work on new validation
ivarne 088c125
Fix sonar cloud issues
ivarne d9026e9
Rename to get more consistent naming
ivarne 50f56fe
Clanup DataType/TaskId to allow *
ivarne df7acc8
Fix more sonarcloud issues
ivarne 698500c
Registrer new validators in DI container
ivarne 92bc3f9
Remove dead code
ivarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Altinn.App.Core.Features.FileAnalysis; | ||
using Altinn.App.Core.Models.Validation; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
|
||
namespace Altinn.App.Core.Features.Validation; | ||
|
||
/// <summary> | ||
/// Validator for data elements. | ||
/// See <see cref="IFormDataValidator"/> for an alternative validator for data elements with app logic. | ||
/// and that support incremental validation on save. | ||
/// For validating the content of files, see <see cref="IFileAnalyser"/> and <see cref="IFileValidator"/> | ||
/// </summary> | ||
public interface IDataElementValidator | ||
{ | ||
/// <summary> | ||
/// The data type that this validator should run for. This is the id of the data type from applicationmetadata.json | ||
/// </summary> | ||
/// <remarks> | ||
/// | ||
/// </remarks> | ||
string DataType { get; } | ||
|
||
/// <summary> | ||
/// Run validations for a data element. This is supposed to run quickly | ||
/// </summary> | ||
/// <param name="instance">The instance to validate</param> | ||
/// <param name="dataElement"></param> | ||
/// <param name="dataType"></param> | ||
/// <returns></returns> | ||
public Task<List<ValidationIssue>> ValidateDataElement(Instance instance, DataElement dataElement, DataType dataType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Altinn.App.Core.Models.Validation; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Altinn.App.Core.Features; | ||
|
||
/// <summary> | ||
/// Interface for handling validation of form data. | ||
/// (i.e. dataElements with AppLogic defined | ||
/// </summary> | ||
public interface IFormDataValidator | ||
{ | ||
/// <summary> | ||
/// The data type this validator is for. Typically either hard coded by implementation or | ||
/// or set by constructor using a <see cref="ServiceKeyAttribute" /> and a keyed service. | ||
/// | ||
/// To validate all types, just use a "*" as value | ||
/// </summary> | ||
string DataType { get; } | ||
|
||
/// <summary> | ||
/// Used for partial validation to ensure that the validator only runs when relevant fields have changed. | ||
/// </summary> | ||
/// <param name="changedFields">List of the json path to all changed fields for incremental validation</param> | ||
bool ShouldRunForIncrementalValidation(List<string>? changedFields = null); | ||
|
||
/// <summary> | ||
/// Returns the group id of the validator. This is used to run partial validations on the backend. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default implementation should work for most cases. | ||
/// </remarks> | ||
public string? Code => $"{this.GetType().FullName}-{DataType}"; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="instance"></param> | ||
/// <param name="dataElement"></param> | ||
/// <param name="data"></param> | ||
/// <param name="changedFields"></param> | ||
/// <returns>List of validation issues</returns> | ||
Task<List<ValidationIssue>> ValidateFormData(Instance instance, DataElement dataElement, object data, | ||
List<string>? changedFields = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Altinn.App.Core.Models.Validation; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Altinn.App.Core.Features; | ||
|
||
/// <summary> | ||
/// Interface for handling validation of tasks. | ||
/// </summary> | ||
public interface ITaskValidator | ||
{ | ||
/// <summary> | ||
/// The task id this validator is for. Typically either hard coded by implementation or | ||
/// or set by constructor using a <see cref="ServiceKeyAttribute" /> and a keyed service. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// string TaskId { get; init; } | ||
/// // constructor | ||
/// public MyTaskValidator([ServiceKey] string taskId) | ||
/// { | ||
/// TaskId = taskId; | ||
/// } | ||
/// </code> | ||
/// </example> | ||
string TaskId { get; } | ||
|
||
/// <summary> | ||
/// Unique code for the validator. Used to run partial validations on the backend. | ||
/// </summary> | ||
public string Code => this.GetType().FullName ?? string.Empty; | ||
|
||
/// <summary> | ||
/// Actual validation logic for the task | ||
/// </summary> | ||
/// <param name="instance">The instance to validate</param> | ||
/// <returns>List of validation issues to add to this task validation</returns> | ||
Task<List<ValidationIssue>> ValidateTask(Instance instance); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Altinn.App.Core/Features/Validation/Default/DataAnnotationValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Altinn.App.Core.Configuration; | ||
using Altinn.App.Core.Features.Validation.Helpers; | ||
using Altinn.App.Core.Models.Validation; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Altinn.App.Core.Features.Validation.Default; | ||
|
||
/// <summary> | ||
/// Runs <see cref="System.ComponentModel.DataAnnotations"/> validation on the data object. | ||
/// </summary> | ||
public class DataAnnotationValidator : IFormDataValidator | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IObjectModelValidator _objectModelValidator; | ||
private readonly GeneralSettings _generalSettings; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public DataAnnotationValidator(IHttpContextAccessor httpContextAccessor, IObjectModelValidator objectModelValidator, IOptions<GeneralSettings> generalSettings) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_objectModelValidator = objectModelValidator; | ||
_generalSettings = generalSettings.Value; | ||
} | ||
|
||
/// <summary> | ||
/// Run Data annotation validation on all data types with app logic | ||
/// </summary> | ||
public string DataType => "*"; | ||
|
||
/// <summary> | ||
/// Disable incremental validation for this validator. | ||
/// </summary> | ||
public bool ShouldRunForIncrementalValidation(List<string>? changedFields = null) => false; | ||
|
||
/// <inheritdoc /> | ||
public Task<List<ValidationIssue>> ValidateFormData(Instance instance, DataElement dataElement, object data, List<string>? changedFields = null) | ||
{ | ||
try | ||
{ | ||
var modelState = new ModelStateDictionary(); | ||
var actionContext = new ActionContext( | ||
_httpContextAccessor.HttpContext!, | ||
new Microsoft.AspNetCore.Routing.RouteData(), | ||
new ActionDescriptor(), | ||
modelState); | ||
ValidationStateDictionary validationState = new ValidationStateDictionary(); | ||
_objectModelValidator.Validate(actionContext, validationState, null!, data); | ||
|
||
return Task.FromResult(ModelStateHelpers.ModelStateToIssueList(modelState, instance, dataElement, _generalSettings, data.GetType(), ValidationIssueSources.ModelState)); | ||
} | ||
catch (Exception e) | ||
{ | ||
return Task.FromException<List<ValidationIssue>>(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Generic catch clause Note