Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Dont* types #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<p>
<a href="https://github.com/benmccallum/fairybread/releases"><img alt="GitHub release" src="https://img.shields.io/github/release/benmccallum/fairybread.svg"></a>
<a href="https://www.nuget.org/packages/FairyBread"><img alt="Nuget version" src="https://img.shields.io/nuget/v/FairyBread"></a>
<a href="https://www.nuget.org/packages/FairyBread"><img alt="NuGet downloads" src="https://img.shields.io/nuget/dt/FairyBread"></a>
<a href="https://www.nuget.org/packages/FairyBread"><img alt="NuGet downloads" src="https://img.shields.io/nuget/dt/FairyBread"></a>
<a href="https://codecov.io/gh/benmccallum/FairyBread">
<img src="https://codecov.io/gh/benmccallum/FairyBread/branch/main/graph/badge.svg?token=HB3O7GR51M"/>
</a>
</a>
</p>
</div>

Expand Down Expand Up @@ -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<FooValidator>()`
* `.DontValidate()`
* `.DontImplicitlyValidate()`
* `.DisableValidation()`
* `.DisableImplicitValidation()`

### Dealing with multi-threaded execution issues

Expand All @@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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):
Expand Down
16 changes: 8 additions & 8 deletions src/FairyBread.Tests/InjectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -318,15 +318,15 @@ protected override void Configure(IObjectTypeDescriptor<QueryI> descriptor)
// Should validate explicitly
.Argument("fooInt", arg => arg.Type<IntType>().ValidateWith<PositiveIntValidator>())
// Shouldn't validate implicitly
.Argument("barInt", arg => arg.Type<IntType>().ValidateWith<PositiveIntValidator>().DontValidateImplicitly())
.Argument("barInt", arg => arg.Type<IntType>().ValidateWith<PositiveIntValidator>().DisableImplicitValidation())
// Shouldn't validate
.Argument("lolInt", arg => arg.Type<IntType>().ValidateWith<PositiveIntValidator>().DontValidate())
.Argument("lolInt", arg => arg.Type<IntType>().ValidateWith<PositiveIntValidator>().DisableValidation())
// Should validate explicitly
.Argument("fooInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputExplicitValidator>())
// Shouldn't validate implicitly
.Argument("barInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputExplicitValidator>().DontValidateImplicitly())
.Argument("barInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputExplicitValidator>().DisableImplicitValidation())
// Shouldn't validate
.Argument("lolInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputExplicitValidator>().DontValidate())
.Argument("lolInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputExplicitValidator>().DisableValidation())
// Shouldn't add an implicitly added validator again
.Argument("dblInput", arg => arg.Type<TestInputType>().ValidateWith<TestInputValidator>())
.ResolveWith<QueryI>(q => q.ReadWithExplicitValidation(default, default, default, default!, default!, default!, default!));
Expand Down
82 changes: 82 additions & 0 deletions src/FairyBread/DisableImplicitValidationAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace FairyBread
{
/// <summary>
/// Instructs FairyBread to not run any validators that
/// are implicitly associated with the annotated argument's type.
/// Explicit validators will still be run.
/// </summary>
[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
{
/// <summary>
/// Instructs FairyBread to not run any validators that
/// are implicitly associated with this argument's runtime type.
/// Explicit validators will still be run.
/// </summary>
public static IArgumentDescriptor DisableImplicitValidation(
this IArgumentDescriptor descriptor)
{
descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
{
argDef.ContextData[WellKnownContextData.DisableImplicitValidation] = true;
});

return descriptor;
}
}

/// <summary>
/// Instructs FairyBread to not run any validators that
/// are implicitly associated with the annotated argument's type.
/// Explicit validators will still be run.
/// </summary>
[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
{
/// <summary>
/// Instructs FairyBread to not run any validators that
/// are implicitly associated with this argument's runtime type.
/// Explicit validators will still be run.
/// </summary>
[Obsolete("Use DisableImplicitValidation")]
public static IArgumentDescriptor DontValidateImplicitly(
this IArgumentDescriptor descriptor)
{
descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
{
argDef.ContextData[WellKnownContextData.DisableImplicitValidation] = true;
});

return descriptor;
}
}
}
74 changes: 74 additions & 0 deletions src/FairyBread/DisableValidationAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace FairyBread
{
/// <summary>
/// Instructs FairyBread to not run any validation on the annotated argument.
/// </summary>
[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
{
/// <summary>
/// Instructs FairyBread to not run any validation for this argument.
/// </summary>
public static IArgumentDescriptor DisableValidation(
this IArgumentDescriptor descriptor)
{
descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
{
argDef.ContextData[WellKnownContextData.DisableValidation] = true;
});

return descriptor;
}
}

/// <summary>
/// Instructs FairyBread to not run any validation on the annotated argument.
/// </summary>
[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
{
/// <summary>
/// Instructs FairyBread to not run any validation for this argument.
/// </summary>
[Obsolete("Use DisableValidation")]
public static IArgumentDescriptor DontValidate(
this IArgumentDescriptor descriptor)
{
descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
{
argDef.ContextData[WellKnownContextData.DisableValidation] = true;
});

return descriptor;
}
}
}
39 changes: 0 additions & 39 deletions src/FairyBread/DontValidateAttribute.cs

This file was deleted.

43 changes: 0 additions & 43 deletions src/FairyBread/DontValidateImplicitlyAttribute.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/FairyBread/ValidationMiddlewareInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ private static List<ValidatorDescriptor> 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<ValidatorDescriptor>(0);
}

var validators = new List<ValidatorDescriptor>();

// 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);
Expand Down
8 changes: 4 additions & 4 deletions src/FairyBread/WellKnownContextData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down