Skip to content

Commit

Permalink
feat: Control whether middleware sets context.Result to null on valid…
Browse files Browse the repository at this point in the history
…ation error (#48)


Co-authored-by: Ben McCallum <[email protected]>
  • Loading branch information
markuswebert and benmccallum authored Jun 14, 2021
1 parent 84af2a9 commit fc3a65b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageIconUrl>https://github.com/benmccallum/fairybread/raw/master/logo-400x400.png</PackageIconUrl>
<PackageLicenseUrl>https://github.com/benmccallum/fairybread/blob/master/LICENSE</PackageLicenseUrl>

<Version>6.0.1</Version>
<Version>6.1.0</Version>

<HotChocolateVersion>11.0.9</HotChocolateVersion>

Expand Down
3 changes: 3 additions & 0 deletions src/FairyBread/DefaultFairyBreadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class DefaultFairyBreadOptions : IFairyBreadOptions
{
public virtual bool ThrowIfNoValidatorsFound { get; set; } = true;

/// <inheritdoc/>
public virtual bool SetNullResultOnValidationError { get; set; } = true;

/// <inheritdoc/>
public virtual Func<IMiddlewareContext, IInputField, bool> ShouldValidate { get; set; }
= DefaultImplementations.ShouldValidate;
Expand Down
6 changes: 6 additions & 0 deletions src/FairyBread/IFairyBreadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public interface IFairyBreadOptions
/// </summary>
bool ThrowIfNoValidatorsFound { get; set; }

/// <summary>
/// If true, FairyBread will set the current field's <c>IResolverContext.Result</c>
/// to <c>null</c> when a validation error occurs.
/// </summary>
bool SetNullResultOnValidationError { get; set; }

/// <summary>
/// Function used to determine if an argument should be validated by
/// FairyBread's <see cref="InputValidationMiddleware"/>.
Expand Down
6 changes: 5 additions & 1 deletion src/FairyBread/InputValidationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public async Task InvokeAsync(IMiddlewareContext context)
if (invalidResults.Any())
{
_validationErrorsHandler.Handle(context, invalidResults);
context.Result = null;

if (_options.SetNullResultOnValidationError)
{
context.Result = null;
}
}
else
{
Expand Down

0 comments on commit fc3a65b

Please sign in to comment.