From 6340b0360d5088d7804fe586c07d4d18b41371c1 Mon Sep 17 00:00:00 2001 From: Glen Date: Sat, 25 Jun 2022 15:26:18 +0200 Subject: [PATCH] Rename Dont* types --- README.md | 20 ++--- src/FairyBread.Tests/InjectorTests.cs | 16 ++-- .../DisableImplicitValidationAttribute.cs | 82 +++++++++++++++++++ src/FairyBread/DisableValidationAttribute.cs | 74 +++++++++++++++++ src/FairyBread/DontValidateAttribute.cs | 39 --------- .../DontValidateImplicitlyAttribute.cs | 43 ---------- .../ValidationMiddlewareInjector.cs | 4 +- src/FairyBread/WellKnownContextData.cs | 8 +- 8 files changed, 180 insertions(+), 106 deletions(-) create mode 100644 src/FairyBread/DisableImplicitValidationAttribute.cs create mode 100644 src/FairyBread/DisableValidationAttribute.cs delete mode 100644 src/FairyBread/DontValidateAttribute.cs delete mode 100644 src/FairyBread/DontValidateImplicitlyAttribute.cs diff --git a/README.md b/README.md index 9dc2ec0..fc2f4a5 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@

GitHub release Nuget version - NuGet downloads + NuGet downloads - +

@@ -68,14 +68,14 @@ Instead, annotate the validator by having it inherit `IExplicitUsageOnlyValidato Annotation API: * `[Validate(typeof(FooValidator)]` - explicitly add this validator for the argument - * `[DontValidate]` - don't validate this argument at all - * `[DontImplicitlyValidate]` - disable implicit validators for the argument + * `[DisableValidation]` - don't validate this argument at all + * `[DisableImplicitValidation]` - disable implicit validators for the argument Fluent API: * `.Argument("foo").ValidateWith()` - * `.DontValidate()` - * `.DontImplicitlyValidate()` + * `.DisableValidation()` + * `.DisableImplicitValidation()` ### Dealing with multi-threaded execution issues @@ -91,14 +91,14 @@ public class SomeInputValidator , IRequiresOwnScopeValidator { // db will be a unique instance for this validation operation - public SomeInputValidator(SomeDbContext db) { ... } + public SomeInputValidator(SomeDbContext db) { ... } } ``` ### Using MediatR? If you want to let MediatR fire validation, you can set up: -* FairyBread to skip validating `MediatR.IRequest` arguments, +* FairyBread to skip validating `MediatR.IRequest` arguments, * your MediatR pipeline to validate them and throw a `ValidationException`, and * an `IErrorFilter`(in Hot Chocolate) to handle it using `FairyBread.DefaultValidationErrorsHandler` to report the errors. @@ -134,7 +134,7 @@ See issues. FairyBread depends on [HotChocolate.Execution](https://www.nuget.org/packages/HotChocolate.Execution) which can bring breaking changes from time to time and require a major bump our end. This is also the case for FluentValidation. -Compatibility is listed below. +Compatibility is listed below. Note, these are minimum versions, for instance, v12.0.1 through to 12.3.x of Hot Chocolate are supported by FairyBread v8.x.x. @@ -157,7 +157,7 @@ We strive to match Hot Chocolate's supported .NET target frameworks, though this ## What the heck is a fairy bread? -A (bizarre) Australian food served at children's parties. Since I'm Australian and HotChocolate has a lot of +A (bizarre) Australian food served at children's parties. Since I'm Australian and HotChocolate has a lot of project names with sweet tendencies, this seemed like a fun name for the project. According to [wikipedia](https://en.wikipedia.org/wiki/Fairy_bread): diff --git a/src/FairyBread.Tests/InjectorTests.cs b/src/FairyBread.Tests/InjectorTests.cs index ea402b0..7d2ccf0 100644 --- a/src/FairyBread.Tests/InjectorTests.cs +++ b/src/FairyBread.Tests/InjectorTests.cs @@ -199,22 +199,22 @@ public string ReadWithExplicitValidation( int fooInt, // Shouldn't validate implicitly [Validate(typeof(PositiveIntValidator))] - [DontValidateImplicitly] + [DisableImplicitValidation] int barInt, // Shouldn't validate [Validate(typeof(PositiveIntValidator))] - [DontValidate] + [DisableValidation] int lolInt, // Should validate explicitly [Validate(typeof(TestInputExplicitValidator))] TestInput fooInput, // Shouldn't validate implicitly [Validate(typeof(TestInputExplicitValidator))] - [DontValidateImplicitly] + [DisableImplicitValidation] TestInput barInput, // Shouldn't validate [Validate(typeof(TestInputExplicitValidator))] - [DontValidate] + [DisableValidation] TestInput lolInput, // Shouldn't add an implicitly added validator again [Validate(typeof(TestInputValidator))] @@ -318,15 +318,15 @@ protected override void Configure(IObjectTypeDescriptor descriptor) // Should validate explicitly .Argument("fooInt", arg => arg.Type().ValidateWith()) // Shouldn't validate implicitly - .Argument("barInt", arg => arg.Type().ValidateWith().DontValidateImplicitly()) + .Argument("barInt", arg => arg.Type().ValidateWith().DisableImplicitValidation()) // Shouldn't validate - .Argument("lolInt", arg => arg.Type().ValidateWith().DontValidate()) + .Argument("lolInt", arg => arg.Type().ValidateWith().DisableValidation()) // Should validate explicitly .Argument("fooInput", arg => arg.Type().ValidateWith()) // Shouldn't validate implicitly - .Argument("barInput", arg => arg.Type().ValidateWith().DontValidateImplicitly()) + .Argument("barInput", arg => arg.Type().ValidateWith().DisableImplicitValidation()) // Shouldn't validate - .Argument("lolInput", arg => arg.Type().ValidateWith().DontValidate()) + .Argument("lolInput", arg => arg.Type().ValidateWith().DisableValidation()) // Shouldn't add an implicitly added validator again .Argument("dblInput", arg => arg.Type().ValidateWith()) .ResolveWith(q => q.ReadWithExplicitValidation(default, default, default, default!, default!, default!, default!)); diff --git a/src/FairyBread/DisableImplicitValidationAttribute.cs b/src/FairyBread/DisableImplicitValidationAttribute.cs new file mode 100644 index 0000000..0496293 --- /dev/null +++ b/src/FairyBread/DisableImplicitValidationAttribute.cs @@ -0,0 +1,82 @@ +using System; +using System.Reflection; +using HotChocolate.Types; +using HotChocolate.Types.Descriptors; + +namespace FairyBread +{ + /// + /// Instructs FairyBread to not run any validators that + /// are implicitly associated with the annotated argument's type. + /// Explicit validators will still be run. + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + public class DisableImplicitValidationAttribute : ArgumentDescriptorAttribute + { + public override void OnConfigure( + IDescriptorContext context, + IArgumentDescriptor descriptor, + ParameterInfo parameter) + { + descriptor.DisableImplicitValidation(); + } + } + + public static class DisableImplicitValidationArgumentDescriptorExtensions + { + /// + /// Instructs FairyBread to not run any validators that + /// are implicitly associated with this argument's runtime type. + /// Explicit validators will still be run. + /// + public static IArgumentDescriptor DisableImplicitValidation( + this IArgumentDescriptor descriptor) + { + descriptor.Extend().OnBeforeNaming((completionContext, argDef) => + { + argDef.ContextData[WellKnownContextData.DisableImplicitValidation] = true; + }); + + return descriptor; + } + } + + /// + /// Instructs FairyBread to not run any validators that + /// are implicitly associated with the annotated argument's type. + /// Explicit validators will still be run. + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + [Obsolete("Use DisableImplicitValidationAttribute")] + public class DontValidateImplicitlyAttribute : ArgumentDescriptorAttribute + { + public override void OnConfigure( + IDescriptorContext context, + IArgumentDescriptor descriptor, + ParameterInfo parameter) + { + descriptor.DontValidateImplicitly(); + } + } + + [Obsolete("Use DisableImplicitValidationArgumentDescriptorExtensions")] + public static class DontValidateImplicitlyArgumentDescriptorExtensions + { + /// + /// Instructs FairyBread to not run any validators that + /// are implicitly associated with this argument's runtime type. + /// Explicit validators will still be run. + /// + [Obsolete("Use DisableImplicitValidation")] + public static IArgumentDescriptor DontValidateImplicitly( + this IArgumentDescriptor descriptor) + { + descriptor.Extend().OnBeforeNaming((completionContext, argDef) => + { + argDef.ContextData[WellKnownContextData.DisableImplicitValidation] = true; + }); + + return descriptor; + } + } +} diff --git a/src/FairyBread/DisableValidationAttribute.cs b/src/FairyBread/DisableValidationAttribute.cs new file mode 100644 index 0000000..a92f464 --- /dev/null +++ b/src/FairyBread/DisableValidationAttribute.cs @@ -0,0 +1,74 @@ +using System; +using System.Reflection; +using HotChocolate.Types; +using HotChocolate.Types.Descriptors; + +namespace FairyBread +{ + /// + /// Instructs FairyBread to not run any validation on the annotated argument. + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + public class DisableValidationAttribute : ArgumentDescriptorAttribute + { + public override void OnConfigure( + IDescriptorContext context, + IArgumentDescriptor descriptor, + ParameterInfo parameter) + { + descriptor.DisableValidation(); + } + } + + public static class DisableValidationArgumentDescriptorExtensions + { + /// + /// Instructs FairyBread to not run any validation for this argument. + /// + public static IArgumentDescriptor DisableValidation( + this IArgumentDescriptor descriptor) + { + descriptor.Extend().OnBeforeNaming((completionContext, argDef) => + { + argDef.ContextData[WellKnownContextData.DisableValidation] = true; + }); + + return descriptor; + } + } + + /// + /// Instructs FairyBread to not run any validation on the annotated argument. + /// + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] + [Obsolete("Use DisableValidationAttribute")] + public class DontValidateAttribute : ArgumentDescriptorAttribute + { + public override void OnConfigure( + IDescriptorContext context, + IArgumentDescriptor descriptor, + ParameterInfo parameter) + { + descriptor.DontValidate(); + } + } + + [Obsolete("Use DisableValidationArgumentDescriptorExtensions")] + public static class DontValidateArgumentDescriptorExtensions + { + /// + /// Instructs FairyBread to not run any validation for this argument. + /// + [Obsolete("Use DisableValidation")] + public static IArgumentDescriptor DontValidate( + this IArgumentDescriptor descriptor) + { + descriptor.Extend().OnBeforeNaming((completionContext, argDef) => + { + argDef.ContextData[WellKnownContextData.DisableValidation] = true; + }); + + return descriptor; + } + } +} diff --git a/src/FairyBread/DontValidateAttribute.cs b/src/FairyBread/DontValidateAttribute.cs deleted file mode 100644 index 82b33dd..0000000 --- a/src/FairyBread/DontValidateAttribute.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Reflection; -using HotChocolate.Types; -using HotChocolate.Types.Descriptors; - -namespace FairyBread -{ - /// - /// Instructs FairyBread to not run any validation on the annotated argument. - /// - [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] - public class DontValidateAttribute : ArgumentDescriptorAttribute - { - public override void OnConfigure( - IDescriptorContext context, - IArgumentDescriptor descriptor, - ParameterInfo parameter) - { - descriptor.DontValidate(); - } - } - - public static class DontValidateArgumentDescriptorExtensions - { - /// - /// Instructs FairyBread to not run any validation for this argument. - /// - public static IArgumentDescriptor DontValidate( - this IArgumentDescriptor descriptor) - { - descriptor.Extend().OnBeforeNaming((completionContext, argDef) => - { - argDef.ContextData[WellKnownContextData.DontValidate] = true; - }); - - return descriptor; - } - } -} diff --git a/src/FairyBread/DontValidateImplicitlyAttribute.cs b/src/FairyBread/DontValidateImplicitlyAttribute.cs deleted file mode 100644 index afe3d59..0000000 --- a/src/FairyBread/DontValidateImplicitlyAttribute.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Reflection; -using HotChocolate.Types; -using HotChocolate.Types.Descriptors; - -namespace FairyBread -{ - /// - /// Instructs FairyBread to not run any validators that - /// are implicitly associated with the annotated argument's type. - /// Explicit validators will still be run. - /// - [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] - public class DontValidateImplicitlyAttribute : ArgumentDescriptorAttribute - { - public override void OnConfigure( - IDescriptorContext context, - IArgumentDescriptor descriptor, - ParameterInfo parameter) - { - descriptor.DontValidateImplicitly(); - } - } - - public static class DontValidateImplicitlyArgumentDescriptorExtensions - { - /// - /// Instructs FairyBread to not run any validators that - /// are implicitly associated with this argument's runtime type. - /// Explicit validators will still be run. - /// - public static IArgumentDescriptor DontValidateImplicitly( - this IArgumentDescriptor descriptor) - { - descriptor.Extend().OnBeforeNaming((completionContext, argDef) => - { - argDef.ContextData[WellKnownContextData.DontValidateImplicitly] = true; - }); - - return descriptor; - } - } -} diff --git a/src/FairyBread/ValidationMiddlewareInjector.cs b/src/FairyBread/ValidationMiddlewareInjector.cs index 64dcebe..ce53eab 100644 --- a/src/FairyBread/ValidationMiddlewareInjector.cs +++ b/src/FairyBread/ValidationMiddlewareInjector.cs @@ -97,7 +97,7 @@ private static List DetermineValidatorsForArg( ArgumentDefinition argDef) { // If validation is explicitly disabled, return none so validation middleware won't be added - if (argDef.ContextData.ContainsKey(WellKnownContextData.DontValidate)) + if (argDef.ContextData.ContainsKey(WellKnownContextData.DisableValidation)) { return new List(0); } @@ -105,7 +105,7 @@ private static List DetermineValidatorsForArg( var validators = new List(); // Include implicit validator/s first (if allowed) - if (!argDef.ContextData.ContainsKey(WellKnownContextData.DontValidateImplicitly)) + if (!argDef.ContextData.ContainsKey(WellKnownContextData.DisableImplicitValidation)) { // And if we can figure out the arg's runtime type var argRuntimeType = TryGetArgRuntimeType(argDef); diff --git a/src/FairyBread/WellKnownContextData.cs b/src/FairyBread/WellKnownContextData.cs index b8fcd38..ba47100 100644 --- a/src/FairyBread/WellKnownContextData.cs +++ b/src/FairyBread/WellKnownContextData.cs @@ -4,11 +4,11 @@ internal static class WellKnownContextData { public const string Prefix = "FairyBread"; - public const string DontValidate = - Prefix + ".DontValidate"; + public const string DisableValidation = + Prefix + ".DisableValidation"; - public const string DontValidateImplicitly = - Prefix + ".DontValidateImplicitly"; + public const string DisableImplicitValidation = + Prefix + ".DisableImplicitValidation"; public const string ExplicitValidatorTypes = Prefix + ".ExplicitValidatorTypes";