Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/204-inconsistent-field-level-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 authored Oct 7, 2024
2 parents ab914a3 + f02c9be commit 80254ae
Show file tree
Hide file tree
Showing 35 changed files with 1,148 additions and 31 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [ published ]

env:
NETCORE_VERSION: '7.0.x'
NETCORE_VERSION: '8.0.x'
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
PROJECT_NAME: FluentValidation
Expand Down Expand Up @@ -42,12 +42,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.NETCORE_VERSION }}

- name: Create Release NuGet package
run: |
arrTag=(${GITHUB_REF//\// })
Expand All @@ -56,17 +56,17 @@ jobs:
VERSION="${VERSION:1}"
echo Clean Version: $VERSION
dotnet pack -v normal -c Release --include-symbols --include-source -p:PackageVersion=$VERSION -o nupkg src/Blazored.$PROJECT_NAME/Blazored.$PROJECT_NAME.csproj
- name: Push to NuGet Feed
run: dotnet nuget push ./nupkg/*.nupkg --source $NUGET_FEED --api-key $NUGET_KEY --skip-duplicate

- name: Publish Sample Site
run: dotnet publish -c Release samples/BlazorWebAssembly/BlazorWebAssembly.csproj

- name: Rewrite base href
uses: SteveSandersonMS/ghaction-rewrite-base-href@v1
with:
html_path: samples/BlazorWebAssembly/bin/Release/net7.0/publish/wwwroot/index.html
html_path: samples/BlazorWebAssembly/bin/Release/net8.0/publish/wwwroot/index.html
base_href: /${{ env.PROJECT_NAME }}/

- name: Deploy to Github Pages
Expand All @@ -75,5 +75,5 @@ jobs:
ACCESS_TOKEN: $GITHUB_TOKEN
BASE_BRANCH: main # The branch the action should deploy from.
BRANCH: gh-pages # The branch the action should deploy to.
FOLDER: samples/BlazorWebAssembly/bin/Release/net7.0/publish/wwwroot # The folder the action should deploy.
FOLDER: samples/BlazorWebAssembly/bin/Release/net8.0/publish/wwwroot # The folder the action should deploy.
SINGLE_COMMIT: true
2 changes: 1 addition & 1 deletion .github/workflows/ci-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
PROJECT_NAME: Blazored.FluentValidation
NETCORE_VERSION: '7.0.x'
NETCORE_VERSION: '8.0.x'

jobs:
build:
Expand Down
17 changes: 17 additions & 0 deletions Blazored.FluentValidation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServer", "samples\Bla
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedModels", "samples\Shared\SharedModels\SharedModels.csproj", "{42276235-5139-41D6-923D-18B7EB5E3E44}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{DACAA0DB-2B93-4FE1-9D21-F45A4E63A640}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazored.FluentValidation.Tests", "tests\Blazored.FluentValidation.Tests\Blazored.FluentValidation.Tests.csproj", "{C92DF59B-B760-4FCC-A34C-A4007529BCC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -78,6 +82,18 @@ Global
{42276235-5139-41D6-923D-18B7EB5E3E44}.Release|x64.Build.0 = Release|Any CPU
{42276235-5139-41D6-923D-18B7EB5E3E44}.Release|x86.ActiveCfg = Release|Any CPU
{42276235-5139-41D6-923D-18B7EB5E3E44}.Release|x86.Build.0 = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|x64.ActiveCfg = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|x64.Build.0 = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|x86.ActiveCfg = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Debug|x86.Build.0 = Debug|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|Any CPU.Build.0 = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|x64.ActiveCfg = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|x64.Build.0 = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|x86.ActiveCfg = Release|Any CPU
{C92DF59B-B760-4FCC-A34C-A4007529BCC5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -86,6 +102,7 @@ Global
{8BC1065A-A71E-4568-8A67-9C3AF039F73A} = {D5C6DCA9-C2BD-4117-BCCC-19E36E8406AB}
{2459CF4B-6548-4031-B784-43E943E270A9} = {D5C6DCA9-C2BD-4117-BCCC-19E36E8406AB}
{42276235-5139-41D6-923D-18B7EB5E3E44} = {D5C6DCA9-C2BD-4117-BCCC-19E36E8406AB}
{C92DF59B-B760-4FCC-A34C-A4007529BCC5} = {DACAA0DB-2B93-4FE1-9D21-F45A4E63A640}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {42B22D99-6E59-4B30-88AD-B9CC07E0DA49}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ The second is when manually validating the model using the `Validate` or `Valida
```

## Access to full `ValidationFailure`
If you need details about the specifics of a validation result (e.g. its `Severity), you can access the result of the
If you need details about the specifics of a validation result (e.g. its `Severity`), you can access the result of the
last validation by calling the `GetFailuresFromLastValidation` method on the `FluentValidationValidator` component.

```razor
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorServer/BlazorServer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
9 changes: 7 additions & 2 deletions samples/BlazorWebAssembly/BlazorWebAssembly.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.6" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazored.FluentValidation\Blazored.FluentValidation.csproj" />
<ProjectReference Include="..\Shared\SharedModels\SharedModels.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions samples/Shared/SharedModels/SharedModels.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.1.0" />
<PackageReference Include="FluentValidation" Version="11.9.2" />
</ItemGroup>

</Project>
16 changes: 11 additions & 5 deletions src/Blazored.FluentValidation/Blazored.FluentValidation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

Expand Down Expand Up @@ -31,14 +31,20 @@
<SupportedPlatform Include="browser" />
</ItemGroup>

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

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.31" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.20" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Results;

using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.DependencyInjection;

using static FluentValidation.AssemblyScanner;

namespace Blazored.FluentValidation;
Expand Down Expand Up @@ -46,12 +48,12 @@ private static async Task ValidateModel(EditContext editContext,

messages.Clear();
fluentValidationValidator.LastValidationResult = new Dictionary<FieldIdentifier, List<ValidationFailure>>();

foreach (var validationResult in validationResults.Errors)
{
var fieldIdentifier = ToFieldIdentifier(editContext, validationResult.PropertyName);
messages.Add(fieldIdentifier, validationResult.ErrorMessage);

if (fluentValidationValidator.LastValidationResult.TryGetValue(fieldIdentifier, out var failures))
{
failures.Add(validationResult);
Expand Down Expand Up @@ -188,7 +190,7 @@ private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in

var obj = editContext.Model;
var nextTokenEnd = propertyPath.IndexOfAny(Separators);

// Optimize for a scenario when parsing isn't needed.
if (nextTokenEnd < 0)
{
Expand All @@ -215,8 +217,8 @@ private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in
// we've got an Item property
var indexerType = prop.GetIndexParameters()[0].ParameterType;
var indexerValue = Convert.ChangeType(nextToken.ToString(), indexerType);
newObj = prop.GetValue(obj, new [] { indexerValue });

newObj = prop.GetValue(obj, new[] { indexerValue });
}
else
{
Expand Down Expand Up @@ -261,7 +263,7 @@ private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in
}

obj = newObj;

nextTokenEnd = propertyPathAsSpan.IndexOfAny(Separators);
if (nextTokenEnd < 0)
{
Expand Down
13 changes: 7 additions & 6 deletions src/Blazored.FluentValidation/FluentValidationsValidator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Results;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using FluentValidation.Results;

namespace Blazored.FluentValidation;

Expand Down Expand Up @@ -48,7 +49,7 @@ public async Task<bool> ValidateAsync(Action<ValidationStrategy<object>>? option
{
throw new NullReferenceException(nameof(CurrentEditContext));
}

ValidateOptions = options;

try
Expand All @@ -61,7 +62,7 @@ public async Task<bool> ValidateAsync(Action<ValidationStrategy<object>>? option
throw new InvalidOperationException("No pending ValidationResult found");
}

await (Task<ValidationResult>) asyncValidationTask;
await (Task<ValidationResult>)asyncValidationTask;

return !CurrentEditContext.GetValidationMessages().Any();
}
Expand Down Expand Up @@ -95,10 +96,10 @@ public ValidationFailure[] GetFailuresFromLastValidation(FieldIdentifier? fieldI

if (fieldIdentifier is null)
return LastValidationResult.Values.SelectMany(f => f).ToArray();

if (!LastValidationResult.TryGetValue(fieldIdentifier.Value, out var failures))
return Array.Empty<ValidationFailure>();
return Array.Empty<ValidationFailure>();

return failures.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<EditForm
Model="@_person"
OnValidSubmit="ValidSubmit"
OnInvalidSubmit="InvalidSubmit">

@if (DisableAssemblyScanning is null)
{
<FluentValidationValidator />
}
else
{
<FluentValidationValidator DisableAssemblyScanning="DisableAssemblyScanning.Value" />
}

<ValidationSummary/>

<p>
<label>First name: </label>
<InputText name="@nameof(_person.FirstName)" @bind-Value="@_person.FirstName"/>
</p>

<button type="submit">Save</button>
</EditForm>

@code {
[Parameter] public bool? DisableAssemblyScanning { get; set; }
private readonly Person _person = new();

internal ValidationResultType Result { get; private set; } = ValidationResultType.Valid;

private void ValidSubmit() => Result = ValidationResultType.Valid;
private void InvalidSubmit() => Result = ValidationResultType.Error;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### What does this test?
This test checks if the assembly scanning works. It leverages, that this test
assembly does not register any `AbstractValidator` by default.

- Setting the `DisableAssemblyScanning` to `true` should not find any validators and ignore errors.
- Setting the `DisableAssemblyScanning` to `false` or not setting the attribute at all, should
find the validators in the assembly and validate normally.
- Setting the `DisableAssemblyScanning` to `true` and registering the validators manually should
find the validators and validate normally.
Loading

0 comments on commit 80254ae

Please sign in to comment.