-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from matthetherington/bugfix/204-inconsistent…
…-field-level-validation Fix inconsistent field-level validation (Fixes #204)
- Loading branch information
Showing
4 changed files
with
178 additions
and
22 deletions.
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); | ||
} | ||
} |