Skip to content

Commit

Permalink
Added ability to disable assembly scanning and added some better exce…
Browse files Browse the repository at this point in the history
…ption catching
  • Loading branch information
chrissainty committed May 8, 2020
1 parent dbde4e0 commit 5f166a5
Show file tree
Hide file tree
Showing 55 changed files with 227 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Blazored.FluentValidation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ VisualStudioVersion = 16.0.29006.145
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{D5C6DCA9-C2BD-4117-BCCC-19E36E8406AB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientSideBlazor", "samples\ClientSideBlazor\ClientSideBlazor.csproj", "{862C6849-B8FC-4062-AF84-A2DB37B9E43E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWebAssembly", "samples\BlazorWebAssembly\BlazorWebAssembly.csproj", "{862C6849-B8FC-4062-AF84-A2DB37B9E43E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerSideBlazor", "samples\ServerSideBlazor\ServerSideBlazor.csproj", "{00D9475F-C3D2-4784-82DE-E1D6991761EB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServer", "samples\BlazorServer\BlazorServer.csproj", "{00D9475F-C3D2-4784-82DE-E1D6991761EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.FluentValidation", "src\Blazored.FluentValidation\Blazored.FluentValidation.csproj", "{12C755E1-0D7F-4D89-98C9-BD5AF560E0B5}"
EndProject
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can install from Nuget using the following command:

Or via the Visual Studio package manger.

## Usage
## Basic Usage
Start by add the following using statement to your root `_Imports.razor`.

```csharp
Expand Down Expand Up @@ -55,3 +55,16 @@ You can then use it as follows within a `EditForm` component.
}
}
```

## Finding Validators
By default, the component will check for validators registered with DI first. If it can't find, any it will then try scanning the applications assemblies to find validators using reflection.

You can control this behaviour using the `DisableAssemblyScanning` parameter. If you only wish the component to get validators from DI, set the value to `true` and assembly scanning will be skipped.

```html
<FluentValidationValidator DisableAssemblyScanning="@true" />
```

You can find examples of different configurations in the sample projects. The Blazor Server project is configured to load validators from DI only. The Blazor WebAssembly project is setup to load validators using reflection.

**Note:** When scanning assemblies the component will swallow any exceptions thrown by that process. This is to stop exceptions thrown by scanning third party dependencies crashing your app.
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ trigger:
- master

pool:
vmImage: 'vs2017-win2016'
vmImage: 'windows-latest'

variables:
buildConfiguration: 'Release'
Expand All @@ -12,7 +12,7 @@ steps:
displayName: 'Use .NET Core sdk'
inputs:
packageType: sdk
version: 3.1.100
version: 3.1.201
installationPath: $(Agent.ToolsDirectory)/dotnet

- task: NuGetToolInstaller@0
Expand Down
10 changes: 10 additions & 0 deletions samples/BlazorServer/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.0.0-preview1" />
<PackageReference Include="FluentValidation" Version="8.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
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 />
<FluentValidationValidator DisableAssemblyScanning="@true" />
<ValidationSummary />

<p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@page "/"
@namespace ServerSideBlazor.Pages
@namespace BlazorServer.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Blazored FluentValidation - Blazor Server App</title>
<title>Blazored FluentValidation (Server)</title>
<base href="~/" />
<environment include="Development">
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
Expand All @@ -21,9 +21,20 @@
</environment>
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>@(await Html.RenderComponentAsync<App>(RenderMode.Server))</app>

<script src="_framework/blazor.server.js"></script>
<body>
<app>@(await Html.RenderComponentAsync<App>(RenderMode.Server))</app>

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ServerSideBlazor
namespace BlazorServer
{
public class Program
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using FluentValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SharedModels;

namespace ServerSideBlazor
namespace BlazorServer
{
public class Startup
{
Expand All @@ -20,7 +22,9 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor(c=>c.DetailedErrors = true);
services.AddServerSideBlazor(c => c.DetailedErrors = true);

services.AddTransient<IValidator<Person>, PersonValidator>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using ServerSideBlazor
@using ServerSideBlazor.Shared

@using BlazorServer
@using BlazorServer.Shared
@using SharedModels

@using Blazored.FluentValidation
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ app {
color: red;
}

#blazor-error-ui {
background: lightyellow;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
display: none;
left: 0;
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
position: fixed;
width: 100%;
z-index: 1000;
}

#blazor-error-ui .dismiss {
cursor: pointer;
position: absolute;
right: 0.75rem;
top: 0.5rem;
}

@media (max-width: 767.98px) {
.main .top-row {
display: none;
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions samples/BlazorWebAssembly/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
21 changes: 21 additions & 0 deletions samples/BlazorWebAssembly/BlazorWebAssembly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0-rc1.20223.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0-rc1.20223.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0-rc1.20223.4" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0-rc1.20217.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazored.FluentValidation\Blazored.FluentValidation.csproj" />
<ProjectReference Include="..\Shared\SharedModels\SharedModels.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Blazor.Hosting;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

namespace ClientSideBlazor
namespace BlazorWebAssembly
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using ClientSideBlazor
@using ClientSideBlazor.Shared

@using BlazorWebAssembly
@using BlazorWebAssembly.Shared

@using SharedModels

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ app {
color: red;
}

#blazor-error-ui {
background: lightyellow;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
display: none;
left: 0;
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
position: fixed;
width: 100%;
z-index: 1000;
}

#blazor-error-ui .dismiss {
cursor: pointer;
position: absolute;
right: 0.75rem;
top: 0.5rem;
}

@media (max-width: 767.98px) {
.main .top-row {
display: none;
Expand Down
27 changes: 27 additions & 0 deletions samples/BlazorWebAssembly/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Blazored FluentValidation (Wasm))</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>Loading...</app>

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
9 changes: 0 additions & 9 deletions samples/ClientSideBlazor/App.razor

This file was deleted.

21 changes: 0 additions & 21 deletions samples/ClientSideBlazor/ClientSideBlazor.csproj

This file was deleted.

16 changes: 0 additions & 16 deletions samples/ClientSideBlazor/wwwroot/index.html

This file was deleted.

9 changes: 0 additions & 9 deletions samples/ServerSideBlazor/App.razor

This file was deleted.

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.0.0-preview1" />
<PackageReference Include="FluentValidation" Version="8.6.2" />
</ItemGroup>

</Project>
Loading

0 comments on commit 5f166a5

Please sign in to comment.