From f5e986fec343857571daeec321e1907af7d33671 Mon Sep 17 00:00:00 2001 From: Rhys Ickeringill Date: Wed, 26 Jun 2024 11:06:33 +1000 Subject: [PATCH 1/2] Add support for net8.0 target & update to latest FluentValidation package (v11.*) --- .../FluentValidationHtmlConventions.cs | 27 ++++++-------- .../FluentValidatorFinder.cs | 37 +++++++++---------- ...hoStack.AspNetCore.FluentValidation.csproj | 12 ++---- ...choStack.AspNetCore.HtmlConventions.csproj | 2 +- .../SchoStack.AspNetCore.Invoker.csproj | 2 +- .../SchoStack.AspNetCore.MediatR.csproj | 2 +- .../SchoStack.AspNetCore.ModelUrls.csproj | 2 +- .../Controllers/HomeController.cs | 32 +++++++--------- .../SchoStack.AspNetCore.Sample.csproj | 2 +- 9 files changed, 51 insertions(+), 67 deletions(-) diff --git a/src/SchoStack.AspNetCore.FluentValidation/FluentValidationHtmlConventions.cs b/src/SchoStack.AspNetCore.FluentValidation/FluentValidationHtmlConventions.cs index 95c0ec3..a2fd122 100644 --- a/src/SchoStack.AspNetCore.FluentValidation/FluentValidationHtmlConventions.cs +++ b/src/SchoStack.AspNetCore.FluentValidation/FluentValidationHtmlConventions.cs @@ -36,16 +36,16 @@ public FluentValidationHtmlConventions(IValidatorFinder validatorFinder, FluentV public static string GetMessage(RequestData requestData, PropertyValidatorResult propertyValidator) { MessageFormatter formatter = new MessageFormatter().AppendPropertyName(propertyValidator.DisplayName); - string message = formatter.BuildMessage(propertyValidator.PropertyValidator.Options.GetErrorMessageTemplate(null)); + string message = formatter.BuildMessage(propertyValidator.PropertyValidator.GetDefaultMessageTemplate(null)); return message; } public void AddEqualToDataAttr(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData request) { - var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is EqualValidator); + var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is IEqualValidator); if (result != null) { - var equal = result.PropertyValidator as EqualValidator; + var equal = result.PropertyValidator as IEqualValidator; if (equal.MemberToCompare != null) { @@ -53,7 +53,7 @@ public void AddEqualToDataAttr(IEnumerable propertyVali .AppendPropertyName(result.DisplayName) .AppendArgument("ComparisonValue", equal.MemberToCompare.Name); - string message = formatter.BuildMessage(equal.Options.GetErrorMessageTemplate(null)); + string message = formatter.BuildMessage(equal.GetDefaultMessageTemplate(null)); htmlTag.Data("val", true); htmlTag.Data("val-equalto", message); @@ -67,8 +67,7 @@ public void AddEqualToDataAttr(IEnumerable propertyVali public void AddRequiredClass(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData requestData) { - var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is NotEmptyValidator - || x.PropertyValidator is NotNullValidator); + var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is INotEmptyValidator or INotNullValidator); if (result != null) { @@ -78,16 +77,16 @@ public void AddRequiredClass(IEnumerable propertyValida public void AddLengthClasses(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData requestData) { - var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is LengthValidator); - if (result != null) + var lengthValidator = propertyValidators.Select(x => x.PropertyValidator).OfType().FirstOrDefault(); + if (lengthValidator != null) { - htmlTag.Attr("maxlength", ((LengthValidator)result.PropertyValidator).Max); + htmlTag.Attr("maxlength", lengthValidator.Max); } } public void AddCreditCardClass(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData requestData) { - var lengthValidator = propertyValidators.Select(x => x.PropertyValidator).OfType().FirstOrDefault(); + var lengthValidator = propertyValidators.Select(x => x.PropertyValidator).OfType().FirstOrDefault(); if (lengthValidator != null) { //if (!_msUnobtrusive) @@ -99,11 +98,11 @@ public void AddCreditCardClass(IEnumerable propertyVali public void AddRegexData(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData requestData) { - var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is RegularExpressionValidator); + var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is IRegularExpressionValidator); if (result != null) { - var regex = result.PropertyValidator as RegularExpressionValidator; + var regex = (IRegularExpressionValidator)result.PropertyValidator; var msg = GetMessage(requestData, result) ?? string.Format("The value did not match the regular expression '{0}'", regex.Expression); htmlTag.Data("val", true).Data("val-regex", msg).Data("val-regex-pattern", regex.Expression); } @@ -111,9 +110,7 @@ public void AddRegexData(IEnumerable propertyValidators public void AddEmailData(IEnumerable propertyValidators, HtmlTag htmlTag, RequestData requestData) { -#pragma warning disable 618 - var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is EmailValidator); -#pragma warning restore 618 + var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is IEmailValidator); if (result != null) { var msg = GetMessage(requestData, result) ?? string.Format("The value is not a valid email address"); diff --git a/src/SchoStack.AspNetCore.FluentValidation/FluentValidatorFinder.cs b/src/SchoStack.AspNetCore.FluentValidation/FluentValidatorFinder.cs index 17f16f2..b77bd22 100644 --- a/src/SchoStack.AspNetCore.FluentValidation/FluentValidatorFinder.cs +++ b/src/SchoStack.AspNetCore.FluentValidation/FluentValidatorFinder.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Reflection; using FluentValidation; using FluentValidation.Validators; @@ -27,11 +26,11 @@ public FluentValidatorFinder(Func resolver) public IEnumerable FindValidators(RequestData requestData) { if (requestData.InputType == null) - return new List(); + return Array.Empty(); var baseValidator = ResolveValidator(requestData.InputType); if (baseValidator == null) - return new List(); + return Array.Empty(); var properties = InputPropertyMatcher.FindPropertyData(requestData); var validators = GetPropertyValidators(baseValidator, properties); @@ -41,49 +40,47 @@ public IEnumerable FindValidators(RequestData requestDa private IEnumerable GetPropertyValidators(IValidator baseValidator, List properties) { var desc = baseValidator.CreateDescriptor(); - var validators = GetNestedPropertyValidators(desc, properties, 0).ToList(); + var validators = GetNestedPropertyValidators(desc, properties, 0); return validators; } private IEnumerable GetNestedPropertyValidators(IValidatorDescriptor desc, List propertyInfo, int i) { if (i == propertyInfo.Count) - return new List(); + yield break; var vals = desc.GetValidatorsForMember(propertyInfo[i].Name); var name = desc.GetName(propertyInfo[i].Name); - var propertyValidators = new List(); - - foreach (var inlineval in vals) + foreach ((dynamic inlineval, _) in vals) { if (i == propertyInfo.Count - 1) { - propertyValidators.Add(new PropertyValidatorResult(inlineval, name)); + yield return new PropertyValidatorResult(inlineval, name); } - - var val = GetValidator(inlineval); + + IValidator val = GetValidator(inlineval); if (val == null) continue; var nestedPropertyValidators = GetNestedPropertyValidators(val.CreateDescriptor(), propertyInfo, i + 1); - propertyValidators.AddRange(nestedPropertyValidators.Select(x => new PropertyValidatorResult(x.PropertyValidator, x.DisplayName))); + foreach (var validator in nestedPropertyValidators) + { + yield return validator; + } } - - return propertyValidators; } - private IValidator GetChildValidator(IChildValidatorAdaptor adaptor) + private static IValidator GetChildValidator(IChildValidatorAdaptor adaptor) { - var validatorContext = new ValidationContext(null); - var propertyValidatorContext = new PropertyValidatorContext(validatorContext, null, null, null); - return ((dynamic) adaptor).GetValidator(propertyValidatorContext); + var validatorContext = new ValidationContext(default); + return ((dynamic)adaptor).GetValidator(validatorContext, null); } - private IValidator GetValidator(IPropertyValidator propertyValidator) + private static IValidator GetValidator(PropertyValidator propertyValidator) { if (propertyValidator is IChildValidatorAdaptor child) - return GetChildValidator(child); + return GetChildValidator(child); return null; } diff --git a/src/SchoStack.AspNetCore.FluentValidation/SchoStack.AspNetCore.FluentValidation.csproj b/src/SchoStack.AspNetCore.FluentValidation/SchoStack.AspNetCore.FluentValidation.csproj index 837dfda..5add1a2 100644 --- a/src/SchoStack.AspNetCore.FluentValidation/SchoStack.AspNetCore.FluentValidation.csproj +++ b/src/SchoStack.AspNetCore.FluentValidation/SchoStack.AspNetCore.FluentValidation.csproj @@ -1,7 +1,7 @@  - net5.0;net6.0;net7.0 + net5.0;net6.0;net7.0;net8.0 Library true true @@ -13,16 +13,12 @@ true - - 7.1 - - - - 7.1 + + 9 - + diff --git a/src/SchoStack.AspNetCore.HtmlConventions/SchoStack.AspNetCore.HtmlConventions.csproj b/src/SchoStack.AspNetCore.HtmlConventions/SchoStack.AspNetCore.HtmlConventions.csproj index fea6e2f..f1fb527 100644 --- a/src/SchoStack.AspNetCore.HtmlConventions/SchoStack.AspNetCore.HtmlConventions.csproj +++ b/src/SchoStack.AspNetCore.HtmlConventions/SchoStack.AspNetCore.HtmlConventions.csproj @@ -1,7 +1,7 @@  - net5.0;net6.0;net7.0 + net5.0;net6.0;net7.0;net8.0 Library true true diff --git a/src/SchoStack.AspNetCore.Invoker/SchoStack.AspNetCore.Invoker.csproj b/src/SchoStack.AspNetCore.Invoker/SchoStack.AspNetCore.Invoker.csproj index dcdd6a5..44e0f4a 100644 --- a/src/SchoStack.AspNetCore.Invoker/SchoStack.AspNetCore.Invoker.csproj +++ b/src/SchoStack.AspNetCore.Invoker/SchoStack.AspNetCore.Invoker.csproj @@ -1,6 +1,6 @@  - net5.0;net6.0;net7.0 + net5.0;net6.0;net7.0;net8.0 Library true true diff --git a/src/SchoStack.AspNetCore.MediatR/SchoStack.AspNetCore.MediatR.csproj b/src/SchoStack.AspNetCore.MediatR/SchoStack.AspNetCore.MediatR.csproj index 7eca2d1..63d07ea 100644 --- a/src/SchoStack.AspNetCore.MediatR/SchoStack.AspNetCore.MediatR.csproj +++ b/src/SchoStack.AspNetCore.MediatR/SchoStack.AspNetCore.MediatR.csproj @@ -1,7 +1,7 @@  - net5.0;net6.0;net7.0 + net5.0;net6.0;net7.0;net8.0 Library true true diff --git a/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj b/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj index 785499a..4fd5a8d 100644 --- a/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj +++ b/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj @@ -1,7 +1,7 @@  - net5.0;net6.0;net7.0 + net5.0;net6.0;net7.0;net8.0 Library true true diff --git a/src/SchoStack.AspNetCore.Sample/Controllers/HomeController.cs b/src/SchoStack.AspNetCore.Sample/Controllers/HomeController.cs index 71711fd..af99d28 100644 --- a/src/SchoStack.AspNetCore.Sample/Controllers/HomeController.cs +++ b/src/SchoStack.AspNetCore.Sample/Controllers/HomeController.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Threading.Tasks; using MediatR; -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; @@ -19,7 +19,6 @@ using FluentValidation; using FluentValidation.Results; using FluentValidation.Validators; -using HtmlTags.Reflection; using SchoStack.AspNetCore.FluentValidation; using SchoStack.AspNetCore.HtmlConventions.Core; @@ -98,7 +97,7 @@ public async Task About(AboutInputModel input) => await _actionBu .For(input) .BeforeSend(async (_, c) => { - c.ActionContext.HttpContext.Response.Headers.Add("X-Test", "test"); + c.ActionContext.HttpContext.Response.Headers.Append("X-Test", "test"); await Task.CompletedTask; }) .Error(async () => await About(new AboutQueryModel())) @@ -218,7 +217,7 @@ public class AboutViewModelValidator : AbstractValidator { public AboutViewModelValidator() { - RuleFor(x => x.Name).SetValidator(new NameValidator()); + RuleFor(x => x.Name).SetValidator(new NameValidator()); RuleFor(x => x.NestedModel).SetValidator(new NestedModelValidator()); } } @@ -231,26 +230,21 @@ public NestedModelValidator() } } - public class NameValidator : IPropertyValidator + public class NameValidator : IPropertyValidator { - public IEnumerable Validate(PropertyValidatorContext context) + public string Name => nameof(NameValidator); + + public string GetDefaultMessageTemplate(string errorCode) => "The Error"; + + public bool IsValid(ValidationContext context, string value) { - if (!(context.InstanceToValidate is string s) || s != "Name") + if (value != "Name") { - yield return new ValidationFailure(context.PropertyName, "The Error"); + context.AddFailure(new ValidationFailure(context.PropertyName, "The Error")); + return false; } - } - - public Task> ValidateAsync(PropertyValidatorContext context, CancellationToken cancellation) - { - return Task.FromResult(Validate(context)); - } - public bool ShouldValidateAsynchronously(IValidationContext context) - { - return false; + return true; } - - public PropertyValidatorOptions Options { get; } } } diff --git a/src/SchoStack.AspNetCore.Sample/SchoStack.AspNetCore.Sample.csproj b/src/SchoStack.AspNetCore.Sample/SchoStack.AspNetCore.Sample.csproj index c4cdda9..b96a097 100644 --- a/src/SchoStack.AspNetCore.Sample/SchoStack.AspNetCore.Sample.csproj +++ b/src/SchoStack.AspNetCore.Sample/SchoStack.AspNetCore.Sample.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 From 278b324311b7a0c8f110bf75a04e9a06806501ac Mon Sep 17 00:00:00 2001 From: Rhys Ickeringill Date: Wed, 26 Jun 2024 11:06:53 +1000 Subject: [PATCH 2/2] Remove unreferenced package --- .../SchoStack.AspNetCore.ModelUrls.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj b/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj index 4fd5a8d..a26ca28 100644 --- a/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj +++ b/src/SchoStack.AspNetCore.ModelUrls/SchoStack.AspNetCore.ModelUrls.csproj @@ -22,7 +22,6 @@ -