From 992af1d63c3d1433b3257d08e5ad765867e76910 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Thu, 18 Jul 2019 10:02:42 +0200 Subject: [PATCH] Fixed: only Required attributes were validated in Tapeti.DataAnnotations Added: RequiredGuid attribute for convenience --- .../RequiredGuidAttribute.cs | 34 +++++++++++++++++++ .../Tapeti.DataAnnotations.Extensions.csproj | 11 ++++++ .../Tapeti.DataAnnotations.Extensions.nuspec | 23 +++++++++++++ .../DataAnnotationsMessageMiddleware.cs | 2 +- .../DataAnnotationsPublishMiddleware.cs | 2 +- Tapeti.sln | 8 ++++- appveyor.yml | 11 ++++++ 7 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 Tapeti.DataAnnotations.Extensions/RequiredGuidAttribute.cs create mode 100644 Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.csproj create mode 100644 Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.nuspec diff --git a/Tapeti.DataAnnotations.Extensions/RequiredGuidAttribute.cs b/Tapeti.DataAnnotations.Extensions/RequiredGuidAttribute.cs new file mode 100644 index 0000000..cef8d1e --- /dev/null +++ b/Tapeti.DataAnnotations.Extensions/RequiredGuidAttribute.cs @@ -0,0 +1,34 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Globalization; + +namespace Tapeti.DataAnnotations.Extensions +{ + /// + /// Can be used on Guid fields which are supposed to be Required, as the Required attribute does + /// not work for Guids and making them Nullable is counter-intuitive. + /// + public class RequiredGuidAttribute : ValidationAttribute + { + private const string DefaultErrorMessage = "'{0}' does not contain a valid guid"; + private const string InvalidTypeErrorMessage = "'{0}' is not of type Guid"; + + public RequiredGuidAttribute() : base(DefaultErrorMessage) + { + } + + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value == null) + return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); + + if (value.GetType() != typeof(Guid)) + return new ValidationResult(string.Format(InvalidTypeErrorMessage, validationContext.DisplayName)); + + var guid = (Guid)value; + return guid == Guid.Empty + ? new ValidationResult(FormatErrorMessage(validationContext.DisplayName)) + : null; + } + } +} diff --git a/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.csproj b/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.csproj new file mode 100644 index 0000000..56cdff2 --- /dev/null +++ b/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.nuspec b/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.nuspec new file mode 100644 index 0000000..db70921 --- /dev/null +++ b/Tapeti.DataAnnotations.Extensions/Tapeti.DataAnnotations.Extensions.nuspec @@ -0,0 +1,23 @@ + + + + Tapeti.DataAnnotations.Extensions + $version$ + Tapeti DataAnnotations Extensions + Mark van Renswoude + Mark van Renswoude + https://raw.githubusercontent.com/MvRens/Tapeti/master/UNLICENSE + https://github.com/MvRens/Tapeti + https://raw.githubusercontent.com/MvRens/Tapeti/master/resources/icons/Tapeti.DataAnnotations.png + false + Additional DataAnnotations attributes. Not specific to Tapeti, but useful for annotating message classes. + + rabbitmq tapeti dataannotations + + + + + + + + \ No newline at end of file diff --git a/Tapeti.DataAnnotations/DataAnnotationsMessageMiddleware.cs b/Tapeti.DataAnnotations/DataAnnotationsMessageMiddleware.cs index 5833630..3228671 100644 --- a/Tapeti.DataAnnotations/DataAnnotationsMessageMiddleware.cs +++ b/Tapeti.DataAnnotations/DataAnnotationsMessageMiddleware.cs @@ -10,7 +10,7 @@ namespace Tapeti.DataAnnotations public Task Handle(IMessageContext context, Func next) { var validationContext = new ValidationContext(context.Message); - Validator.ValidateObject(context.Message, validationContext); + Validator.ValidateObject(context.Message, validationContext, true); return next(); } diff --git a/Tapeti.DataAnnotations/DataAnnotationsPublishMiddleware.cs b/Tapeti.DataAnnotations/DataAnnotationsPublishMiddleware.cs index 9cf0408..f3d70b4 100644 --- a/Tapeti.DataAnnotations/DataAnnotationsPublishMiddleware.cs +++ b/Tapeti.DataAnnotations/DataAnnotationsPublishMiddleware.cs @@ -10,7 +10,7 @@ namespace Tapeti.DataAnnotations public Task Handle(IPublishContext context, Func next) { var validationContext = new ValidationContext(context.Message); - Validator.ValidateObject(context.Message, validationContext); + Validator.ValidateObject(context.Message, validationContext, true); return next(); } diff --git a/Tapeti.sln b/Tapeti.sln index 114f0a9..d870188 100644 --- a/Tapeti.sln +++ b/Tapeti.sln @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tapeti.Tests", "Tapeti.Test EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tapeti.Serilog", "Tapeti.Serilog\Tapeti.Serilog.csproj", "{43AA5DF3-49D5-4795-A290-D6511502B564}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tapeti.Transient", "Tapeti.Transient\Tapeti.Transient.csproj", "{A6355E63-19AB-47EA-91FA-49B5E9B41F88}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tapeti.Transient", "Tapeti.Transient\Tapeti.Transient.csproj", "{A6355E63-19AB-47EA-91FA-49B5E9B41F88}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tapeti.DataAnnotations.Extensions", "Tapeti.DataAnnotations.Extensions\Tapeti.DataAnnotations.Extensions.csproj", "{1AAA5A2C-EAA8-4C49-96A6-673EA1EEE831}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -69,6 +71,10 @@ Global {A6355E63-19AB-47EA-91FA-49B5E9B41F88}.Debug|Any CPU.Build.0 = Debug|Any CPU {A6355E63-19AB-47EA-91FA-49B5E9B41F88}.Release|Any CPU.ActiveCfg = Release|Any CPU {A6355E63-19AB-47EA-91FA-49B5E9B41F88}.Release|Any CPU.Build.0 = Release|Any CPU + {1AAA5A2C-EAA8-4C49-96A6-673EA1EEE831}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1AAA5A2C-EAA8-4C49-96A6-673EA1EEE831}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1AAA5A2C-EAA8-4C49-96A6-673EA1EEE831}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1AAA5A2C-EAA8-4C49-96A6-673EA1EEE831}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/appveyor.yml b/appveyor.yml index 8a442c8..43409b3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,21 +9,32 @@ before_build: - ps: gitversion /l console /output buildserver /updateAssemblyInfo after_build: + # Tapeti - cmd: ECHO nuget pack Tapeti\Tapeti.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: nuget pack Tapeti\Tapeti.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.Annotations - cmd: nuget pack Tapeti.Annotations\Tapeti.Annotations.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.Annotations.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.DataAnnotations - cmd: nuget pack Tapeti.DataAnnotations\Tapeti.DataAnnotations.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.DataAnnotations.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.DataAnnotations.Extensions + - cmd: nuget pack Tapeti.DataAnnotations.Extensions\Tapeti.DataAnnotations.Extensions.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" + - cmd: appveyor PushArtifact "Tapeti.DataAnnotations.Extensions.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.Flow - cmd: nuget pack Tapeti.Flow\Tapeti.Flow.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.Flow.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.Flow.SQL - cmd: nuget pack Tapeti.Flow.SQL\Tapeti.Flow.SQL.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.Flow.SQL.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.Transient - cmd: nuget pack Tapeti.Transient\Tapeti.Transient.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.Transient.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.SimpleInjector - cmd: nuget pack Tapeti.SimpleInjector\Tapeti.SimpleInjector.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.SimpleInjector.%GitVersion_NuGetVersion%.nupkg" + # Tapeti.Serilog - cmd: nuget pack Tapeti.Serilog\Tapeti.Serilog.nuspec -version "%GitVersion_NuGetVersion%" -prop "target=%CONFIGURATION%" - cmd: appveyor PushArtifact "Tapeti.Serilog.%GitVersion_NuGetVersion%.nupkg"