-
-
Notifications
You must be signed in to change notification settings - Fork 86
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 inconsistent field-level validation (Fixes #204) #205
Merged
pwelter34
merged 19 commits into
Blazored:main
from
matthetherington:bugfix/204-inconsistent-field-level-validation
Oct 7, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a7314a0
Fix inconsistent field-level validation (Fixes #204)
matthetherington-pa e0a6702
Undo formatting changes
matthetherington-pa 90f4c91
Undo validator formatting changes
matthetherington-pa c2efc1c
Remove using statements
matthetherington-pa a76ede9
Only run rules for property being validated
matthetherington-pa d8d4443
Only execute rules for property when validating at field level
matthetherington-pa 74e9f32
Undo formatting changes
matthetherington-pa e245c50
Undo formatting changes
matthetherington-pa 77f9310
Move property path code to static helper
matthetherington-pa 43853d0
Use var
matthetherington-pa eec5389
Fix warnings
matthetherington-pa 7120051
Mark helper class as internal
matthetherington-pa 011afd6
Remove factory change
matthetherington-pa b0d9a97
Undo formatting change
matthetherington-pa a3304ae
Extract method
matthetherington-pa 2faff2f
Filter property names for SetValidator rules
matthetherington-pa 97e4394
Merge branch 'main' into bugfix/204-inconsistent-field-level-validation
chrissainty ab914a3
Merge branch 'main' into bugfix/204-inconsistent-field-level-validation
pwelter34 80254ae
Merge branch 'main' into bugfix/204-inconsistent-field-level-validation
pwelter34 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
16 changes: 16 additions & 0 deletions
16
src/Blazored.FluentValidation/IntersectingCompositeValidatorSelector.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,16 @@ | ||
using FluentValidation; | ||
using FluentValidation.Internal; | ||
|
||
namespace Blazored.FluentValidation; | ||
|
||
internal class IntersectingCompositeValidatorSelector : IValidatorSelector { | ||
private readonly IEnumerable<IValidatorSelector> _selectors; | ||
|
||
public IntersectingCompositeValidatorSelector(IEnumerable<IValidatorSelector> selectors) { | ||
_selectors = selectors; | ||
} | ||
|
||
public bool CanExecute(IValidationRule rule, string propertyPath, IValidationContext context) { | ||
return _selectors.All(s => s.CanExecute(rule, propertyPath, context)); | ||
} | ||
} |
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,111 @@ | ||
using System.Collections; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
|
||
namespace Blazored.FluentValidation; | ||
|
||
internal static class PropertyPathHelper | ||
{ | ||
private class Node | ||
{ | ||
public Node? Parent { get; set; } | ||
public object? ModelObject { get; set; } | ||
public string? PropertyName { get; set; } | ||
public int? Index { get; set; } | ||
} | ||
|
||
public static string ToFluentPropertyPath(EditContext editContext, FieldIdentifier fieldIdentifier) | ||
{ | ||
var nodes = new Stack<Node>(); | ||
nodes.Push(new Node() | ||
{ | ||
ModelObject = editContext.Model, | ||
}); | ||
|
||
while (nodes.Any()) | ||
{ | ||
var currentNode = nodes.Pop(); | ||
var currentModelObject = currentNode.ModelObject; | ||
|
||
if (currentModelObject == fieldIdentifier.Model) | ||
{ | ||
return BuildPropertyPath(currentNode, fieldIdentifier); | ||
} | ||
|
||
var nonPrimitiveProperties = currentModelObject?.GetType() | ||
.GetProperties() | ||
.Where(prop => !prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray) ?? new List<PropertyInfo>(); | ||
|
||
foreach (var nonPrimitiveProperty in nonPrimitiveProperties) | ||
{ | ||
var instance = nonPrimitiveProperty.GetValue(currentModelObject); | ||
|
||
if (instance == fieldIdentifier.Model) | ||
{ | ||
var node = new Node() | ||
{ | ||
Parent = currentNode, | ||
PropertyName = nonPrimitiveProperty.Name, | ||
ModelObject = instance | ||
}; | ||
|
||
return BuildPropertyPath(node, fieldIdentifier); | ||
} | ||
|
||
if(instance is IEnumerable enumerable) | ||
{ | ||
var itemIndex = 0; | ||
foreach (var item in enumerable) | ||
{ | ||
nodes.Push(new Node() | ||
{ | ||
ModelObject = item, | ||
Parent = currentNode, | ||
PropertyName = nonPrimitiveProperty.Name, | ||
Index = itemIndex++ | ||
}); | ||
} | ||
} | ||
else if(instance is not null) | ||
{ | ||
nodes.Push(new Node() | ||
{ | ||
ModelObject = instance, | ||
Parent = currentNode, | ||
PropertyName = nonPrimitiveProperty.Name | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
private static string BuildPropertyPath(Node currentNode, FieldIdentifier fieldIdentifier) | ||
{ | ||
var pathParts = new List<string>(); | ||
pathParts.Add(fieldIdentifier.FieldName); | ||
var next = currentNode; | ||
|
||
while (next is not null) | ||
{ | ||
if (!string.IsNullOrEmpty(next.PropertyName)) | ||
{ | ||
if (next.Index is not null) | ||
{ | ||
pathParts.Add($"{next.PropertyName}[{next.Index}]"); | ||
} | ||
else | ||
{ | ||
pathParts.Add(next.PropertyName); | ||
} | ||
} | ||
|
||
next = next.Parent; | ||
} | ||
|
||
pathParts.Reverse(); | ||
|
||
return string.Join('.', pathParts); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had an issue with this bit of code, in a model it kept adding DateTime types on the NodeStack in an endless loop, eventually ending in out of memory. I think additional type guards are needed here for types such as
DateTime
. This change to check for additional simple types fixed the issue with DateTime not being a primitive.jafin@2de01ab