Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgun committed Aug 16, 2023
0 parents commit 443ac29
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/Build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: FluentValidation.AutoValidation [Build]

env:
JAVA_VERSION: 17
JAVA_DISTRIBUTION: microsoft
DOTNET_VERSION: 7.0.x
DOTNET_BUILD_CONFIGURATION: Release
SONAR_PATH: .\.sonar\scanner
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST: https://sonarcloud.io
SONAR_ORGANIZATION: sharpgrip
SONAR_PROJECT: SharpGrip_FluentValidation.AutoValidation
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

on:
workflow_dispatch:
push:
branches:
- 'master'
- 'develop'

jobs:
build:
runs-on: windows-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.JAVA_DISTRIBUTION }}

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Install dotnet-coverage
shell: powershell
run: dotnet tool install --global dotnet-coverage

- name: Install SonarCloud scanner
shell: powershell
run: |
New-Item -Path ${{ env.SONAR_PATH }} -ItemType Directory -Force
dotnet tool update dotnet-sonarscanner --tool-path ${{ env.SONAR_PATH }}
- name: Build solution
run: dotnet build -c ${{ env.DOTNET_BUILD_CONFIGURATION }} /warnaserror

- name: Test solution
run: dotnet test --no-build -c ${{ env.DOTNET_BUILD_CONFIGURATION }} --verbosity normal

- name: Cleanup solution
run: dotnet clean

- name: Analyze solution
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ env.SONAR_PROJECT }}" /o:"${{ env.SONAR_ORGANIZATION }}" /d:sonar.token="${{ env.SONAR_TOKEN }}" /d:sonar.host.url="${{ env.SONAR_HOST }}" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
dotnet build -c ${{ env.DOTNET_BUILD_CONFIGURATION }}
dotnet-coverage collect "dotnet test -c ${{ env.DOTNET_BUILD_CONFIGURATION }}" -f xml -o "coverage.xml"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ env.SONAR_TOKEN }}"
34 changes: 34 additions & 0 deletions .github/workflows/Release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: FluentValidation.AutoValidation [Release]

env:
DOTNET_VERSION: 7.0.x
DOTNET_BUILD_CONFIGURATION: Release
DOTNET_PACKAGES_OUTPUT_DIRECTORY: .nuget
NUGET_SOURCE: https://api.nuget.org/v3/index.json
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

on:
push:
tags:
- "*"

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Pack packages
run: dotnet pack --configuration ${{ env.DOTNET_BUILD_CONFIGURATION }} --output "${{ env.DOTNET_PACKAGES_OUTPUT_DIRECTORY }}"

- name: Push packages
run: dotnet nuget push "${{ env.DOTNET_PACKAGES_OUTPUT_DIRECTORY }}/*.nupkg" --source ${{ env.NUGET_SOURCE }} --api-key ${{ env.NUGET_API_KEY }}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea/
.nuget/
NuGet/
[Oo]bj/
[Bb]in/
.DS_Store
*.DotSettings.user
76 changes: 76 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [email protected]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
21 changes: 21 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion>
<NoWarn>NU1701</NoWarn>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<PropertyGroup>
<PackageVersion>1.0.0-beta1</PackageVersion>
<Company>SharpGrip</Company>
<Authors>SharpGrip</Authors>
<Copyright>SharpGrip</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://sharpgrip.net</PackageProjectUrl>
<RepositoryUrl>https://github.com/SharpGrip/FluentValidation.AutoValidation</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>SharpGrip.FluentValidation.AutoValidation.Endpoints</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>SharpGrip.FluentValidation.AutoValidation.Endpoints</AssemblyName>
<PackageId>SharpGrip.FluentValidation.AutoValidation.Endpoints</PackageId>
<Title>SharpGrip FluentValidation AutoValidation Endpoints</Title>
<Description>SharpGrip FluentValidation AutoValidation Endpoints is an extension of the FluentValidation library enabling automatic asynchronous validation in minimal APIs (endpoints).</Description>
<PackageTags>sharpgrip;validation;fluent-validation;minimal-api</PackageTags>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentValidation.AutoValidation.Shared\FluentValidation.AutoValidation.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using SharpGrip.FluentValidation.AutoValidation.Endpoints.Filters;

namespace SharpGrip.FluentValidation.AutoValidation.Endpoints.Extensions
{
public static class EndpointRouteExtensions
{
public static RouteHandlerBuilder AddFluentValidationAutoValidation(this RouteHandlerBuilder routeHandlerBuilder)
{
routeHandlerBuilder.AddEndpointFilter<FluentValidationAutoValidationEndpointFilter>();

return routeHandlerBuilder;
}

public static RouteGroupBuilder AddFluentValidationAutoValidation(this RouteGroupBuilder routeGroupBuilder)
{
routeGroupBuilder.AddEndpointFilter<FluentValidationAutoValidationEndpointFilter>();

return routeGroupBuilder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using Microsoft.AspNetCore.Http;
using SharpGrip.FluentValidation.AutoValidation.Shared.Extensions;

namespace SharpGrip.FluentValidation.AutoValidation.Endpoints.Filters
{
public class FluentValidationAutoValidationEndpointFilter : IEndpointFilter
{
private readonly IServiceProvider serviceProvider;

public FluentValidationAutoValidationEndpointFilter(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
for (var i = 0; i < context.Arguments.Count; i++)
{
var argument = context.Arguments[i];

if (argument == null)
{
continue;
}

if (serviceProvider.GetValidator(argument.GetType()) is IValidator validator)
{
var validationResult = await validator.ValidateAsync(new ValidationContext<object>(argument), context.HttpContext.RequestAborted);

if (!validationResult.IsValid)
{
var errors = new Dictionary<string, string[]>();

foreach (var errorGrouping in validationResult.Errors.GroupBy(error => error.PropertyName))
{
errors.Add(errorGrouping.Key, errorGrouping.Select(error => error.ErrorMessage).ToArray());
}

return TypedResults.ValidationProblem(errors);
}
}
}

return await next(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>SharpGrip.FluentValidation.AutoValidation.Mvc</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<AssemblyName>SharpGrip.FluentValidation.AutoValidation.Mvc</AssemblyName>
<PackageId>SharpGrip.FluentValidation.AutoValidation.Mvc</PackageId>
<Title>SharpGrip FluentValidation AutoValidation MVC</Title>
<Description>SharpGrip FluentValidation AutoValidation MVC is an extension of the FluentValidation library enabling automatic asynchronous validation in MVC controllers.</Description>
<PackageTags>sharpgrip;validation;fluent-validation;mvc</PackageTags>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentValidation.AutoValidation.Shared\FluentValidation.AutoValidation.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using SharpGrip.FluentValidation.AutoValidation.Mvc.Filters;

namespace SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddFluentValidationAutoValidation(this IServiceCollection serviceCollection)
{
var modelStateInvalidFilter = new ModelStateInvalidFilter(new ApiBehaviorOptions {InvalidModelStateResponseFactory = context => new OkResult()}, NullLogger.Instance);

// Make sure we insert the `FluentValidationAutoValidationActionFilter` before the built-in `ModelStateInvalidFilter` to prevent it short-circuiting the request.
serviceCollection.Configure<MvcOptions>(options => options.Filters.Add<FluentValidationAutoValidationActionFilter>(modelStateInvalidFilter.Order - 1));

return serviceCollection;
}
}
}
Loading

0 comments on commit 443ac29

Please sign in to comment.