public static IRequestExecutorBuilder AddFairyBread( this IRequestExecutorBuilder requestExecutorBuilder, Action <IFairyBreadOptions>?configureOptions = null) { // Services var services = requestExecutorBuilder.Services; var options = new DefaultFairyBreadOptions(); configureOptions?.Invoke(options); services.TryAddSingleton <IFairyBreadOptions>(options); services.TryAddSingleton <IValidatorProvider, DefaultValidatorProvider>(); services.TryAddSingleton <IValidationResultHandler, DefaultValidationResultHandler>(); // Execution requestExecutorBuilder.UseField <InputValidationMiddleware>(); return(requestExecutorBuilder); }
public static IRequestExecutorBuilder AddFairyBread( this IRequestExecutorBuilder requestExecutorBuilder, Action <IFairyBreadOptions>?configureOptions = null) { // Services var services = requestExecutorBuilder.Services; var options = new DefaultFairyBreadOptions(); configureOptions?.Invoke(options); services.TryAddSingleton <IFairyBreadOptions>(options); services.TryAddSingleton <IValidatorRegistry>(sp => new DefaultValidatorRegistry(services, sp.GetRequiredService <IFairyBreadOptions>())); services.TryAddSingleton <IValidatorProvider, DefaultValidatorProvider>(); services.TryAddSingleton <IValidationErrorsHandler, DefaultValidationErrorsHandler>(); // Executor builder requestExecutorBuilder.TryAddTypeInterceptor <ValidationMiddlewareInjector>(); return(requestExecutorBuilder); }
public void Should_Respect_ThrowIfNoValidatorsFound_Option(bool throwIfNoValidatorsFound) { // Arrange var services = new ServiceCollection(); var options = new DefaultFairyBreadOptions { ThrowIfNoValidatorsFound = throwIfNoValidatorsFound }; Func <DefaultValidatorRegistry> func = () => new DefaultValidatorRegistry(services, options); // Act if (throwIfNoValidatorsFound) { var ex = Should.Throw <Exception>(func); ex.Message.ShouldContain("No validators were found by FairyBread."); } else { Should.NotThrow(func); } }