Skip to content

Commit 857a9cc

Browse files
Add support for endpoint-centric APIs (#48)
* Move Language to DirectoryBuild Props. <LangVersion>10</LangVersion> <RazorLangVersion>3.0</RazorLangVersion> * Make SignAssembly stuff only in Realease mode. * Bump version to 3.0 as this is a breaking change. * Add a delegate that allows for transformation of the model before validation. * Update sample to show how transform model could be used although this example doesn't actualy change the model * Update Readme with whats new in 3.0 Also added 2.0 given it was missing. * RepositoryUrl should have the .git * Update FluentValidation from 11.4.0 to 11.5.2 (latest at this time)
1 parent 32a9b4d commit 857a9cc

File tree

9 files changed

+67
-30
lines changed

9 files changed

+67
-30
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ More sample projects will be added as the framework develops.
4848

4949
## What's new
5050

51+
### New in 3.0.0
52+
- Add OnTransformModel delegate to allow the model to be transformed before validation (This is useful when using endpoint-centric APIs)
53+
- Major change because it changes the signature of the public IValidationProvider interface.
54+
55+
### New in 2.0.0
56+
- Add net7.0 target framework.
57+
5158
### New in 1.8.0
5259
- Use `Services.TryAddScoped` instead of `Services.AddScoped` for validators, in case
5360
the consuming app has already registered validators with a different lifetime.

Source/Lib/Directory.Build.props

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44

5-
<Version>2.0.0</Version>
6-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
7-
<FileVersion>2.0.0.0</FileVersion>
5+
<Version>3.0.0</Version>
6+
<AssemblyVersion>3.0.0.0</AssemblyVersion>
7+
<FileVersion>3.0.0.0</FileVersion>
88

99
<Authors>Peter Morris</Authors>
1010
<Company />
@@ -15,19 +15,18 @@
1515

1616
<PackageProjectUrl>https://github.com/mrpmorris/blazor-validation</PackageProjectUrl>
1717
<PackageIconUrl />
18-
<RepositoryUrl>https://github.com/mrpmorris/blazor-validation</RepositoryUrl>
18+
<RepositoryUrl>https://github.com/mrpmorris/blazor-validation.git</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
20+
<LangVersion>latest</LangVersion>
21+
<RazorLangVersion>3.0</RazorLangVersion>
22+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
23+
</PropertyGroup>
2024

25+
<PropertyGroup Condition="'$(Configuration)'=='Release'">
26+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2127
<AssemblyOriginatorKeyFile>MrPMorris.snk</AssemblyOriginatorKeyFile>
2228
<SignAssembly>true</SignAssembly>
23-
<DelaySign>false</DelaySign>
24-
25-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
26-
27-
</PropertyGroup>
28-
29-
<PropertyGroup>
30-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
29+
<DelaySign>true</DelaySign>
3130
</PropertyGroup>
3231

3332
</Project>

Source/Lib/Morris.Blazor.FluentValidation/FluentValidationValidatorProvider.cs

+25-10
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ namespace Morris.Blazor.FluentValidation
1313
{
1414
public class FluentValidationValidatorProvider : IValidationProvider
1515
{
16-
public void InitializeEditContext(
16+
private Func<object, object> OnTransformModel { get; set; } = null;
17+
18+
public void InitializeEditContext
19+
(
1720
EditContext editContext,
1821
IServiceProvider serviceProvider,
19-
ValidationProperties properties)
22+
ValidationProperties properties,
23+
Func<object, object> transformModel = null
24+
)
2025
{
2126
if (editContext == null)
2227
throw new ArgumentNullException(nameof(editContext));
2328
if (serviceProvider == null)
2429
throw new ArgumentNullException(nameof(serviceProvider));
2530
properties ??= ValidationProperties.Set;
31+
OnTransformModel = transformModel;
2632

2733
var messages = new ValidationMessageStore(editContext);
2834
editContext.OnValidationRequested +=
@@ -56,9 +62,11 @@ private async Task ValidateModel(
5662
messages.Clear();
5763
editContext.NotifyValidationStateChanged();
5864

59-
IEnumerable<IValidator> validators = GetValidatorsForObject(editContext.Model, serviceProvider);
65+
object transformedModel = GetTransformedModel(editContext.Model);
66+
67+
IEnumerable<IValidator> validators = GetValidatorsForObject(transformedModel, serviceProvider);
6068

61-
var validationContext = new ValidationContext<object>(editContext.Model);
69+
var validationContext = new ValidationContext<object>(transformedModel);
6270

6371
var validationResults = new List<ValidationResult>();
6472
foreach (IValidator validator in validators)
@@ -70,13 +78,17 @@ private async Task ValidateModel(
7078
IEnumerable<ValidationFailure> validationFailures = validationResults.SelectMany(x => x.Errors);
7179
foreach (var validationError in validationFailures)
7280
{
73-
GetParentObjectAndPropertyName(editContext.Model, validationError.PropertyName, out object parentObject, out string propertyName);
81+
GetParentObjectAndPropertyName(transformedModel, validationError.PropertyName, out object parentObject, out string propertyName);
7482
if (parentObject != null)
7583
messages.Add(new FieldIdentifier(parentObject, propertyName), validationError.ErrorMessage);
7684
}
7785

7886
editContext.NotifyValidationStateChanged();
7987
}
88+
private object GetTransformedModel(object model)
89+
{
90+
return OnTransformModel is not null ? OnTransformModel(model) : model;
91+
}
8092

8193
private void GetParentObjectAndPropertyName(
8294
object model,
@@ -120,11 +132,13 @@ private void GetParentObjectAndPropertyName(
120132
propertyName = propertyPathParts.Dequeue();
121133
}
122134

123-
private async Task ValidateField(
135+
private async Task ValidateField
136+
(
124137
EditContext editContext,
125138
ValidationMessageStore messages,
126139
FieldIdentifier fieldIdentifier,
127-
IServiceProvider serviceProvider)
140+
IServiceProvider serviceProvider
141+
)
128142
{
129143
if (editContext == null)
130144
throw new ArgumentNullException(nameof(editContext));
@@ -135,18 +149,19 @@ private async Task ValidateField(
135149
if (editContext.Model == null)
136150
throw new NullReferenceException($"{nameof(editContext)}.{nameof(editContext.Model)}");
137151

152+
object transformedModel = GetTransformedModel(editContext.Model);
138153
var propertiesToValidate = new string[] { fieldIdentifier.FieldName };
139-
var fluentValidationContext =
154+
var fluentValidationContext =
140155
new ValidationContext<object>(
141-
instanceToValidate: fieldIdentifier.Model,
156+
instanceToValidate: transformedModel,
142157
propertyChain: new PropertyChain(),
143158
validatorSelector: new MemberNameValidatorSelector(propertiesToValidate)
144159
);
145160

146161
messages.Clear(fieldIdentifier);
147162
editContext.NotifyValidationStateChanged();
148163

149-
IEnumerable<IValidator> validators = GetValidatorsForObject(fieldIdentifier.Model, serviceProvider);
164+
IEnumerable<IValidator> validators = GetValidatorsForObject(transformedModel, serviceProvider);
150165
var validationResults = new List<ValidationResult>();
151166

152167
foreach (IValidator validator in validators)

Source/Lib/Morris.Blazor.FluentValidation/Morris.Blazor.FluentValidation.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="FluentValidation" Version="11.4.0" />
15+
<PackageReference Include="FluentValidation" Version="11.5.2" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

Source/Lib/Morris.Blazor.Validation/DataAnnotationsValidatorProvider.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ namespace Morris.Blazor.Validation
66
{
77
public class DataAnnotationsValidatorProvider : IValidationProvider
88
{
9-
public void InitializeEditContext(
9+
public void InitializeEditContext
10+
(
1011
EditContext editContext,
1112
IServiceProvider serviceProvider,
12-
ValidationProperties properties)
13+
ValidationProperties properties,
14+
Func<object, object> transformModel = null
15+
)
1316
{
1417
#if NET7_0_OR_GREATER
1518
editContext.EnableDataAnnotationsValidation(serviceProvider);

Source/Lib/Morris.Blazor.Validation/IValidationProvider.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace Morris.Blazor.Validation
55
{
66
public interface IValidationProvider
77
{
8-
void InitializeEditContext(EditContext editContext, IServiceProvider serviceProvider, ValidationProperties properties);
8+
void InitializeEditContext
9+
(
10+
EditContext editContext,
11+
IServiceProvider serviceProvider,
12+
ValidationProperties properties,
13+
Func<object, object> transformModel = null
14+
);
915
}
1016
}

Source/Lib/Morris.Blazor.Validation/Morris.Blazor.Validation.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
<PropertyGroup>
44
<IsPackable>true</IsPackable>
5-
<LangVersion>10</LangVersion>
6-
<RazorLangVersion>3.0</RazorLangVersion>
75
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
86
<PackageId>Morris.Blazor.Validation</PackageId>
97
<Product>Morris.Blazor.Validation</Product>

Source/Lib/Morris.Blazor.Validation/Validate.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Validate : ComponentBase
1818

1919
[Inject]
2020
IServiceProvider ServiceProvider { get; set; }
21+
[Parameter]
22+
public Func<object, object> OnTransformModel { get; set; } = null;
2123

2224
public override async Task SetParametersAsync(ParameterView parameters)
2325
{
@@ -39,7 +41,7 @@ private void EditContextChanged()
3941
foreach (Type providerType in Repository.All)
4042
{
4143
var validationProvider = (IValidationProvider)ServiceProvider.GetService(providerType);
42-
validationProvider.InitializeEditContext(CurrentEditContext, ServiceProvider, ValidationProperties);
44+
validationProvider.InitializeEditContext(CurrentEditContext, ServiceProvider, ValidationProperties, OnTransformModel);
4345
}
4446
}
4547
}

Source/Samples/02-FluentValidation/FluentValidationSample/Pages/Index.razor

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@using Morris.Blazor.FluentValidation
66

77
<EditForm Model=@Person OnSubmit=SubmitForm>
8-
<Morris.Blazor.Validation.Validate ValidationProperties=@(ValidationProperties.Set.FluentValidator<PersonValidator>())/>
8+
<Morris.Blazor.Validation.Validate ValidationProperties=@(ValidationProperties.Set.FluentValidator<PersonValidator>()) OnTransformModel=OnTransformModel />
99
<div class="mb-2 row">
1010
<div class="col-3">
1111
<label>Salutation</label>
@@ -74,6 +74,13 @@
7474
@code{
7575
Models.Person Person = new Models.Person();
7676

77+
private object OnTransformModel(object model)
78+
{
79+
// Confirm that the model is of the expected type
80+
// Do transformations here
81+
// return the transformed model
82+
return model;
83+
}
7784
void SubmitForm(EditContext editContext)
7885
{
7986
editContext.ValidateObjectTree();

0 commit comments

Comments
 (0)