Skip to content

Commit

Permalink
feat: v5
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccallum committed Feb 20, 2021
1 parent cb6a21d commit 776435f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 91 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>4.1.1</Version>
<Version>4.1.2</Version>

<HotChocolateVersion>11.0.9</HotChocolateVersion>

Expand Down
47 changes: 0 additions & 47 deletions src/FairyBread.Tests/InputValidationMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using FluentValidation;
using HotChocolate;
using HotChocolate.Configuration;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Types;
Expand Down Expand Up @@ -85,25 +84,6 @@ public async Task Query_Doesnt_Validate_By_Default()
await Verifier.Verify(result);
}

[Fact]
public async Task Auto_Validator_Scanning_Works()
{
// Arrange
var executor = await GetRequestExecutorAsync(options =>
{
options.ShouldValidate = (ctx, arg) => ctx.Operation.Operation == OperationType.Query;
options.AssembliesToScanForValidators = null;
});

var query = @"query { read(foo: { someInteger: -1, someString: ""hello"" }) }";

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

// Assert
await Verifier.Verify(result);
}

[Theory]
[MemberData(nameof(Cases))]
public async Task Mutation_Works(CaseData caseData)
Expand Down Expand Up @@ -228,33 +208,6 @@ public async Task Should_Respect_ThrowIfNoValidatorsFound_Option(bool throwIfNoV
await Verifier.Verify(result, verifySettings);
}

[Fact]
public async Task Should_Respect_AssembliesToScanForValidators_Option()
{
// Arrange
var executor = await GetRequestExecutorAsync(
options =>
{
options.ShouldValidate = (ctx, arg) => ctx.Operation.Operation == OperationType.Query;
options.AssembliesToScanForValidators = new[] { typeof(FooInputDtoValidator).Assembly };
},
services =>
{
services.AddSingleton<FooInputDtoValidator>();
services.AddSingleton<BarInputDtoValidator>();
services.AddSingleton<BarInputDtoAsyncValidator>();
},
registerValidatorFromAssembly: false);

var query = @"query { read(foo: { someInteger: -1, someString: ""hello"" }) }";

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

// Assert
await Verifier.Verify(result);
}

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

Expand Down
2 changes: 0 additions & 2 deletions src/FairyBread/DefaultFairyBreadOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace FairyBread
{
public class DefaultFairyBreadOptions : IFairyBreadOptions
{
public virtual IEnumerable<Assembly>? AssembliesToScanForValidators { get; set; }

public virtual bool ThrowIfNoValidatorsFound { get; set; } = true;

/// <inheritdoc/>
Expand Down
49 changes: 16 additions & 33 deletions src/FairyBread/DefaultValidatorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,30 @@ public DefaultValidatorProvider(IServiceProvider serviceProvider, IServiceCollec
{
ServiceProvider = serviceProvider;

List<AssemblyScanner.AssemblyScanResult> validatorResults;
var validatorResults = new List<AssemblyScanner.AssemblyScanResult>();
var validatorInterface = typeof(IValidator);
var objectValidatorInterface = typeof(IValidator<object>);
var underlyingValidatorType = objectValidatorInterface.GetGenericTypeDefinition().UnderlyingSystemType;

var useExplicitAssemblySearch = options.AssembliesToScanForValidators != null;
if (useExplicitAssemblySearch)
foreach (var service in services)
{
validatorResults = AssemblyScanner.FindValidatorsInAssemblies(options.AssembliesToScanForValidators).ToList();
}
else
{
validatorResults = new List<AssemblyScanner.AssemblyScanResult>();

var validatorInterface = typeof(IValidator);
var objectValidatorInterface = typeof(IValidator<object>);
var underlyingValidatorType = objectValidatorInterface.GetGenericTypeDefinition().UnderlyingSystemType;

foreach (var service in services)
if (!service.ServiceType.IsGenericType ||
service.ServiceType.Name != objectValidatorInterface.Name ||
service.ServiceType.GetGenericTypeDefinition() != underlyingValidatorType)
{
if (!service.ServiceType.IsGenericType ||
service.ServiceType.Name != objectValidatorInterface.Name ||
service.ServiceType.GetGenericTypeDefinition() != underlyingValidatorType)
{
continue;
}

validatorResults.Add(
new AssemblyScanner.AssemblyScanResult(
service.ServiceType,
service.ImplementationType));
continue;
}

validatorResults.Add(
new AssemblyScanner.AssemblyScanResult(
service.ServiceType,
service.ImplementationType));
}

if (!validatorResults.Any() && options.ThrowIfNoValidatorsFound)
{
throw new Exception($"No validators were found by FairyBread." +
$"{nameof(IFairyBreadOptions)}.{nameof(IFairyBreadOptions.AssembliesToScanForValidators)} " +
(useExplicitAssemblySearch
? $"was provided and no validators could be found in the provided assemblies: " +
$"{string.Join(",", options.AssembliesToScanForValidators!)}."
: $"was not provided and no validators could be found in your service registrations." +
$"Ensure you're registering your FluentValidation validators.")
);
throw new Exception($"No validators were found by FairyBread. " +
$"Ensure you're registering your FluentValidation validators for DI.");
}

foreach (var validatorResult in validatorResults)
Expand Down
8 changes: 0 additions & 8 deletions src/FairyBread/IFairyBreadOptions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using HotChocolate.Resolvers;
using HotChocolate.Types;

namespace FairyBread
{
public interface IFairyBreadOptions
{
/// <summary>
/// If set, FairyBread will look for validators only in these assemblies.
/// If not set, FairyBread will look for validators registered with DI.
/// </summary>
IEnumerable<Assembly>? AssembliesToScanForValidators { get; set; }

/// <summary>
/// If true, FairyBread will throw on startup if no validators are found.
/// This is on by default to avoid an accidental release with no validators
Expand Down

0 comments on commit 776435f

Please sign in to comment.