Skip to content

Commit

Permalink
Upgrade to FluentValidation v10 and a bit of tidying up (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissainty authored Apr 8, 2021
1 parent e88697f commit cf5b52b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 29 deletions.
6 changes: 1 addition & 5 deletions samples/BlazorServer/BlazorServer.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.2.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazored.FluentValidation\Blazored.FluentValidation.csproj" />
<ProjectReference Include="..\Shared\SharedModels\SharedModels.csproj" />
Expand Down
1 change: 0 additions & 1 deletion samples/BlazorWebAssembly/BlazorWebAssembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
Expand Down
6 changes: 3 additions & 3 deletions samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<hr class="mb-5" />

<EditForm Model="@Person" OnValidSubmit="@SubmitValidForm">
<FluentValidationValidator @ref="fluentValidationValidator" />
<FluentValidationValidator @ref="_fluentValidationValidator" />
<ValidationSummary />

<p>
Expand Down Expand Up @@ -72,7 +72,7 @@
@code {
Person Person { get; set; } = new Person();

private FluentValidationValidator fluentValidationValidator;
private FluentValidationValidator _fluentValidationValidator;

void SubmitValidForm()
{
Expand All @@ -81,6 +81,6 @@

void PartialValidate()
{
Console.WriteLine($"Partial validation result : {fluentValidationValidator.Validate(options => options.IncludeRuleSets("Names"))}");
Console.WriteLine($"Partial validation result : {_fluentValidationValidator.Validate(options => options.IncludeRuleSets("Names"))}");
}
}
2 changes: 1 addition & 1 deletion samples/Shared/SharedModels/SharedModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.2.2" />
<PackageReference Include="FluentValidation" Version="10.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.9" />
<PackageReference Include="FluentValidation" Version="10.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.14" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace Blazored.FluentValidation
{
public static class EditContextFluentValidationExtensions
{
private readonly static char[] separators = new[] { '.', '[' };

private static List<string> scannedAssembly = new List<string>();

private static List<AssemblyScanResult> assemblyScanResults = new List<AssemblyScanResult>();
private static readonly char[] Separators = { '.', '[' };
private static readonly List<string> ScannedAssembly = new List<string>();
private static readonly List<AssemblyScanResult> AssemblyScanResults = new List<AssemblyScanResult>();

public static EditContext AddFluentValidation(this EditContext editContext, IServiceProvider serviceProvider, bool disableAssemblyScanning, IValidator validator, FluentValidationValidator fluentValidationValidator)

Expand Down Expand Up @@ -47,7 +45,7 @@ private static async void ValidateModel(EditContext editContext,

if (validator is object)
{
var context = ValidationContext<object>.CreateWithOptions(editContext.Model, fluentValidationValidator.options ?? (opt => opt.IncludeAllRuleSets()));
var context = ValidationContext<object>.CreateWithOptions(editContext.Model, fluentValidationValidator.Options ?? (opt => opt.IncludeAllRuleSets()));

var validationResults = await validator.ValidateAsync(context);

Expand Down Expand Up @@ -107,23 +105,23 @@ private static IValidator GetValidatorForModel(IServiceProvider serviceProvider,
return null;
}

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(i => !scannedAssembly.Contains(i.FullName)))
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(i => !ScannedAssembly.Contains(i.FullName)))
{
try
{
assemblyScanResults.AddRange(FindValidatorsInAssembly(assembly));
AssemblyScanResults.AddRange(FindValidatorsInAssembly(assembly));
}
catch (Exception)
{
}

scannedAssembly.Add(assembly.FullName);
ScannedAssembly.Add(assembly.FullName);
}


var interfaceValidatorType = typeof(IValidator<>).MakeGenericType(model.GetType());

Type modelValidatorType = assemblyScanResults.FirstOrDefault(i => interfaceValidatorType.IsAssignableFrom(i.InterfaceType))?.ValidatorType;
Type modelValidatorType = AssemblyScanResults.FirstOrDefault(i => interfaceValidatorType.IsAssignableFrom(i.InterfaceType))?.ValidatorType;

if (modelValidatorType == null)
{
Expand All @@ -147,7 +145,7 @@ private static FieldIdentifier ToFieldIdentifier(EditContext editContext, string

while (true)
{
var nextTokenEnd = propertyPath.IndexOfAny(separators);
var nextTokenEnd = propertyPath.IndexOfAny(Separators);
if (nextTokenEnd < 0)
{
return new FieldIdentifier(obj, propertyPath);
Expand Down
10 changes: 5 additions & 5 deletions src/Blazored.FluentValidation/FluentValidationsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ namespace Blazored.FluentValidation
{
public class FluentValidationValidator : ComponentBase
{
[Inject] IServiceProvider ServiceProvider { get; set; }
[Inject] private IServiceProvider ServiceProvider { get; set; }

[CascadingParameter] EditContext CurrentEditContext { get; set; }
[CascadingParameter] private EditContext CurrentEditContext { get; set; }

[Parameter] public IValidator Validator { get; set; }
[Parameter] public bool DisableAssemblyScanning { get; set; }

internal Action<ValidationStrategy<object>> options;
internal Action<ValidationStrategy<object>> Options;

public bool Validate(Action<ValidationStrategy<object>> options)
{
this.options = options;
Options = options;

try
{
return CurrentEditContext.Validate();
}
finally
{
this.options = null;
Options = null;
}
}

Expand Down

0 comments on commit cf5b52b

Please sign in to comment.