Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccallum committed Jan 24, 2021
2 parents a265376 + fa130f7 commit 720d1f3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 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>3.0.0</Version>
<Version>3.0.1</Version>

<HotChocolateVersion>11.0.0</HotChocolateVersion>

Expand Down
1 change: 1 addition & 0 deletions src/FairyBread.Tests/FairyBread.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>$(TestTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
Data: {
read: 'SomeInteger: 1, SomeString: hello; '
}
}
67 changes: 45 additions & 22 deletions src/FairyBread.Tests/InputValidationMiddlewareTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using HotChocolate;
Expand Down Expand Up @@ -59,6 +60,49 @@ public async Task Query_Works(CaseData caseData)
await Verifier.Verify(result, verifySettings);
}


[Theory]
[MemberData(nameof(Cases))]
public async Task Mutation_Works(CaseData caseData)
{
// Arrange
var executor = await GetRequestExecutorAsync();

var query = "mutation { write(foo: " + caseData.FooInput + ", bar: " + caseData.BarInput + ") }";

// Act
var result = await executor.ExecuteAsync(query);

// Assert
var verifySettings = new VerifySettings();
verifySettings.UseParameters(caseData);
await Verifier.Verify(result, verifySettings);
}

[Fact]
public async Task Ignores_Null_Argument_Value()
{
// Arrange
var caseData = (CaseData)Cases().First()[0];
var executor = await GetRequestExecutorAsync(options =>
{
options.ShouldValidate = (ctx, arg) => ctx.Operation.Operation == OperationType.Query;
});

var query = "query { read(foo: " + caseData.FooInput + ") }";

// Act
var result = await executor.ExecuteAsync(query);

// Assert
var verifySettings = new VerifySettings();
await Verifier.Verify(result, verifySettings);
}


// TODO: Unit tests for:
// - cancellation

public static IEnumerable<object[]> Cases()
{
var caseId = 1;
Expand Down Expand Up @@ -98,30 +142,9 @@ public CaseData(int caseId, string fooInput, string barInput)
}
}

[Theory]
[MemberData(nameof(Cases))]
public async Task Mutation_Works(CaseData caseData)
{
// Arrange
var executor = await GetRequestExecutorAsync();

var query = "mutation { write(foo: " + caseData.FooInput + ", bar: " + caseData.BarInput + ") }";

// Act
var result = await executor.ExecuteAsync(query);

// Assert
var verifySettings = new VerifySettings();
verifySettings.UseParameters(caseData);
await Verifier.Verify(result, verifySettings);
}

// TODO: Unit tests for:
// - cancellation

public class QueryType
{
public string Read(FooInputDto foo, BarInputDto bar) => $"{foo}; {bar}";
public string Read(FooInputDto foo, BarInputDto? bar) => $"{foo}; {bar}";
}

public class MutationType
Expand Down
1 change: 1 addition & 0 deletions src/FairyBread/FairyBread.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(LibraryTargetFrameworks)</TargetFrameworks>
<Description>Input validation for HotChocolate</Description>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/FairyBread/IFairyBreadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface IFairyBreadOptions
/// <summary>
/// Function used to determine if an argument should be validated by
/// FairyBread's <see cref="InputValidationMiddleware"/>.
/// The default implementation is <see cref="DefaultImplementations.ShouldValidate(IMiddlewareContext, Argument)"/>
/// The default implementation is
/// <see cref="DefaultImplementations.ShouldValidate(IMiddlewareContext, Argument)"/>
/// </summary>
Func<IMiddlewareContext, IInputField, bool> ShouldValidate { get; set; }
}
Expand Down
7 changes: 6 additions & 1 deletion src/FairyBread/InputValidationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public async Task InvokeAsync(IMiddlewareContext context)
var resolvedValidators = _validatorProvider.GetValidators(context, argument);
try
{
var value = context.ArgumentValue<object>(argument.Name);
var value = context.ArgumentValue<object?>(argument.Name);
if (value == null)
{
continue;
}

foreach (var resolvedValidator in resolvedValidators)
{
var validationContext = new ValidationContext<object>(value);
Expand Down

0 comments on commit 720d1f3

Please sign in to comment.