Skip to content

Commit dbb012d

Browse files
authored
Merge pull request #65 from micro-elements/v4
V4
2 parents bf04c72 + 4697cce commit dbb012d

File tree

14 files changed

+112
-35
lines changed

14 files changed

+112
-35
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"cake.tool": {
6-
"version": "0.35.0",
6+
"version": "0.38.0",
77
"commands": [
88
"dotnet-cake"
99
]

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Changes in 4.0.0:
2+
* FluentValidation updated to [9.0.0]
3+
* Swashbuckle.AspNetCore updated to [5.5.1]
4+
* Changed getting included validator (FluentValidation internal API changed)
5+
* New EmailValidator rule compatible with FluentValidation AspNetCoreCompatibleEmailValidator
6+
17
# Changes in 3.2.0:
28
* FluentValidation fix version to [8.3.0, 9)
39
* Swashbuckle.AspNetCore fix version to [5.2.0, 6)

MicroElements.Swashbuckle.FluentValidation.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1515
build.sh = build.sh
1616
CHANGELOG.md = CHANGELOG.md
1717
src\common.props = src\common.props
18+
src\Directory.Build.props = src\Directory.Build.props
1819
README.md = README.md
1920
version.props = version.props
2021
EndProjectSection

build.ps1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
##########################################################################
2-
# This is modified Cake bootstrapper script for PowerShell.
3-
# The initial file was downloaded from https://github.com/cake-build/resources
4-
##########################################################################
1+
#################################################################################################################
2+
# This is the MicroElements.DevOps Cake bootstrapper script for PowerShell.
3+
# For latest version see: https://github.com/micro-elements/MicroElements.DevOps/blob/master/resources/build.ps1
4+
#################################################################################################################
55

66
[CmdletBinding()]
77
Param(
@@ -20,9 +20,8 @@ if(!$PSScriptRoot){
2020
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
2121
}
2222

23-
$CAKE_VERSION = "0.29.0"
24-
$CAKE_BAKERY_VERSION = "0.3.0"
25-
$DEVOPS_VERSION = "1.9.1"
23+
$CAKE_VERSION = "0.38.0"
24+
$DEVOPS_VERSION = "1.11.0"
2625
$NUGET_URL = "https://api.nuget.org/v3/index.json"
2726
$NUGET_BETA_URL = "https://www.myget.org/F/micro-elements/api/v3/index.json"
2827
#$NUGET_URL = "file://C:\NuGet"
@@ -31,16 +30,15 @@ $TOOLS_DIR = Join-Path $PSScriptRoot "tools"
3130
$CAKE_DLL = Join-Path $TOOLS_DIR "Cake.CoreCLR/$CAKE_VERSION/Cake.dll"
3231

3332
# Script to run.
34-
# $Script = Join-Path $TOOLS_DIR "microelements.devops/$DEVOPS_VERSION/scripts/main.cake"
33+
$Script = Join-Path $TOOLS_DIR "microelements.devops/$DEVOPS_VERSION/scripts/main.cake"
3534

3635
$cake_props = @"
3736
<Project Sdk="Microsoft.NET.Sdk">
3837
<PropertyGroup>
39-
<TargetFramework>netcoreapp3.0</TargetFramework>
38+
<TargetFramework>netstandard2.0</TargetFramework>
4039
</PropertyGroup>
4140
<ItemGroup>
4241
<PackageReference Include="Cake.CoreCLR" Version="$CAKE_VERSION" />
43-
<PackageReference Include="Cake.Bakery" Version="$CAKE_BAKERY_VERSION" />
4442
<PackageReference Include="MicroElements.DevOps" Version="$DEVOPS_VERSION" />
4543
</ItemGroup>
4644
</Project>

build.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
echo "Starting build.sh"
88

9-
DEVOPS_VERSION=1.9.1
9+
DEVOPS_VERSION=1.11.0
1010
NUGET_URL=https://api.nuget.org/v3/index.json
11+
NUGET_BETA_URL=https://www.myget.org/F/micro-elements/api/v3/index.json
1112

1213
# Define directories.
1314
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
@@ -17,7 +18,7 @@ TOOLS_DIR=$SCRIPT_DIR/tools
1718
SCRIPT=$TOOLS_DIR/microelements.devops/$DEVOPS_VERSION/scripts/main.cake
1819

1920
CAKE_PROPS_PATH=$TOOLS_DIR/cake.props
20-
CAKE_VERSION="0.35.0"
21+
CAKE_VERSION="0.38.0"
2122
CAKE_ARGUMENTS=()
2223

2324
# Parse arguments.
@@ -40,6 +41,7 @@ echo "SCRIPT_DIR: $SCRIPT_DIR"
4041
echo "SCRIPT: $SCRIPT"
4142
echo "TOOLS_DIR: $TOOLS_DIR"
4243
echo "NUGET_URL: $NUGET_URL"
44+
echo "NUGET_BETA_URL: $NUGET_BETA_URL"
4345
echo "CAKE_PROPS_PATH: $CAKE_PROPS_PATH"
4446
echo "CAKE_ARGUMENTS: ${CAKE_ARGUMENTS[*]}"
4547

@@ -71,7 +73,7 @@ EOL
7173
fi
7274

7375
# Restore Cake and Packages
74-
dotnet restore $CAKE_PROPS_PATH --packages $TOOLS_DIR --source "$NUGET_URL"
76+
dotnet restore $CAKE_PROPS_PATH --packages $TOOLS_DIR --source "$NUGET_URL" --source "$NUGET_BETA_URL"
7577
dotnet tool restore
7678

7779
# Start Cake

samples/SampleWebApi/Contracts/Sample.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.ComponentModel.DataAnnotations;
22
using FluentValidation;
3+
using FluentValidation.Validators;
34

45
namespace SampleWebApi.Contracts
56
{
@@ -9,6 +10,7 @@ public class Sample
910

1011
public string NotNull { get; set; }
1112
public string NotEmpty { get; set; }
13+
public string EmailAddressRegex { get; set; }
1214
public string EmailAddress { get; set; }
1315
public string RegexField { get; set; }
1416

@@ -32,7 +34,8 @@ public SampleValidator()
3234
{
3335
RuleFor(sample => sample.NotNull).NotNull();
3436
RuleFor(sample => sample.NotEmpty).NotEmpty();
35-
RuleFor(sample => sample.EmailAddress).EmailAddress();
37+
RuleFor(sample => sample.EmailAddressRegex).EmailAddress(EmailValidationMode.Net4xRegex);
38+
RuleFor(sample => sample.EmailAddress).EmailAddress(EmailValidationMode.AspNetCoreCompatible);
3639
RuleFor(sample => sample.RegexField).Matches(@"(\d{4})-(\d{2})-(\d{2})");
3740

3841
RuleFor(sample => sample.ValueInRange).GreaterThanOrEqualTo(5).LessThanOrEqualTo(10);

samples/SampleWebApi/Controllers/SampleApiController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using FluentValidation;
33
using Microsoft.AspNetCore.Mvc;
44
using SampleWebApi.Contracts;
5-
using SampleWebApi.Validators;
65

76
namespace SampleWebApi.Controllers
87
{
@@ -52,6 +51,17 @@ public IActionResult AddSampleWithDataAnnotations([FromBody] SampleWithDataAnnot
5251
return Ok();
5352
}
5453

54+
[HttpPost("[action]")]
55+
public IActionResult AddSampleFromQuery([FromQuery] Sample sample)
56+
{
57+
if (!ModelState.IsValid)
58+
{
59+
return BadRequest(ModelState);
60+
}
61+
62+
return Ok();
63+
}
64+
5565
[HttpPost("[action]")]
5666
public IActionResult AddObjectA([FromBody] ObjectA objectA)
5767
{

samples/SampleWebApi/SampleWebApi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="FluentValidation.AspNetCore" Version="8.5.0" />
8+
<PackageReference Include="FluentValidation.AspNetCore" Version="9.1.3" />
99
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
12-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.1" />
12+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

src/Directory.Build.props

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project>
22
<ItemGroup>
3-
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3" PrivateAssets="All" />
4-
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" PrivateAssets="All" />
5-
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.8.0" />
3+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4+
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
65
</ItemGroup>
76
<PropertyGroup>
87
<DebugType>embedded</DebugType>

src/MicroElements.Swashbuckle.FluentValidation/FluentValidationRules.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,40 @@ private void AddRulesFromIncludedValidators(OpenApiSchema schema, SchemaFilterCo
107107
// Note: IValidatorDescriptor doesn't return IncludeRules so we need to get validators manually.
108108
var childAdapters = (validator as IEnumerable<IValidationRule>)
109109
.NotNull()
110-
.OfType<IncludeRule>()
110+
.Where(rule => rule is IIncludeRule)
111+
.OfType<PropertyRule>()
111112
.Where(includeRule => includeRule.HasNoCondition())
112113
.SelectMany(includeRule => includeRule.Validators)
113-
.OfType<ChildValidatorAdaptor>();
114+
.OfType<IChildValidatorAdaptor>();
114115

115-
foreach (var adapter in childAdapters)
116+
foreach (var childAdapter in childAdapters)
116117
{
117-
var propertyValidatorContext = new PropertyValidatorContext(new ValidationContext(null), null, string.Empty);
118-
var includeValidator = adapter.GetValidator(propertyValidatorContext);
119-
ApplyRulesToSchema(schema, context, includeValidator);
120-
AddRulesFromIncludedValidators(schema, context, includeValidator);
118+
IValidator includedValidator = GetValidatorFromChildValidatorAdapter(childAdapter);
119+
if (includedValidator != null)
120+
{
121+
ApplyRulesToSchema(schema, context, includedValidator);
122+
AddRulesFromIncludedValidators(schema, context, includedValidator);
123+
}
121124
}
122125
}
123126

127+
private IValidator GetValidatorFromChildValidatorAdapter(IChildValidatorAdaptor childValidatorAdapter)
128+
{
129+
// Fake context. We have not got real context because no validation yet.
130+
var fakeContext = new PropertyValidatorContext(new ValidationContext<object>(null), null, string.Empty);
131+
132+
// Try to validator with reflection.
133+
var childValidatorAdapterType = childValidatorAdapter.GetType();
134+
var getValidatorMethod = childValidatorAdapterType.GetMethod(nameof(ChildValidatorAdaptor<object, object>.GetValidator));
135+
if (getValidatorMethod != null)
136+
{
137+
var validator = (IValidator)getValidatorMethod.Invoke(childValidatorAdapter, new[] { fakeContext });
138+
return validator;
139+
}
140+
141+
return null;
142+
}
143+
124144
/// <summary>
125145
/// Creates default rules.
126146
/// Can be overriden by name.
@@ -177,6 +197,14 @@ public static FluentValidationRule[] CreateDefaultRules()
177197
context.Schema.Properties[context.PropertyKey].Pattern = regularExpressionValidator.Expression;
178198
}
179199
},
200+
new FluentValidationRule("EMail")
201+
{
202+
Matches = propertyValidator => propertyValidator.GetType().Name.Contains("EmailValidator") && propertyValidator.HasNoCondition(),
203+
Apply = context =>
204+
{
205+
context.Schema.Properties[context.PropertyKey].Format = "email";
206+
}
207+
},
180208
new FluentValidationRule("Comparison")
181209
{
182210
Matches = propertyValidator => propertyValidator is IComparisonValidator && propertyValidator.HasNoCondition(),
@@ -238,6 +266,7 @@ public static FluentValidationRule[] CreateDefaultRules()
238266
}
239267
}
240268
},
269+
241270
};
242271
}
243272
}

0 commit comments

Comments
 (0)