feat: rename validation builder for parity
Rename extensions functions and `IValidatorBuilder` for parity with dispatcher options and builder.
This commit is contained in:
parent
4d33d5ab4c
commit
36f1a9eb1b
7 changed files with 52 additions and 51 deletions
|
|
@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- **request.dispatcher:** Hide pipeline internals in stack frames
|
- **request.dispatcher:** Hide pipeline internals in stack frames
|
||||||
|
- **request.validation:** Rename `IValidatorBuilder` to `IRequestValidatorBuilder` incl. extensions methods
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_single_validator()
|
public async Task I_can_resolve_single_validator()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<OrderedFailuresValidator>(ServiceLifetime.Transient);
|
builder.Add<OrderedFailuresValidator>(ServiceLifetime.Transient);
|
||||||
});
|
});
|
||||||
|
|
@ -27,7 +27,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_multiple_validators()
|
public async Task I_can_resolve_multiple_validators()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<PersonValidator>(ServiceLifetime.Transient);
|
builder.Add<PersonValidator>(ServiceLifetime.Transient);
|
||||||
builder.Add<AnotherPersonValidator>(ServiceLifetime.Transient);
|
builder.Add<AnotherPersonValidator>(ServiceLifetime.Transient);
|
||||||
|
|
@ -46,7 +46,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_open_generic_validator()
|
public async Task I_can_resolve_open_generic_validator()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add(typeof(GenericValidator<>), ServiceLifetime.Transient);
|
builder.Add(typeof(GenericValidator<>), ServiceLifetime.Transient);
|
||||||
});
|
});
|
||||||
|
|
@ -63,7 +63,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_multi_interface_validator()
|
public async Task I_can_resolve_multi_interface_validator()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<MultiInterfaceValidator>(ServiceLifetime.Transient);
|
builder.Add<MultiInterfaceValidator>(ServiceLifetime.Transient);
|
||||||
});
|
});
|
||||||
|
|
@ -84,7 +84,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_generic_validator_with_constraints()
|
public async Task I_can_resolve_generic_validator_with_constraints()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add(typeof(ConstrainedValidator<>), ServiceLifetime.Transient);
|
builder.Add(typeof(ConstrainedValidator<>), ServiceLifetime.Transient);
|
||||||
});
|
});
|
||||||
|
|
@ -104,7 +104,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_generic_wrapper_validator_with_constraints()
|
public async Task I_can_resolve_generic_wrapper_validator_with_constraints()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add(typeof(ConstrainedWrapperValidator<>), ServiceLifetime.Transient);
|
builder.Add(typeof(ConstrainedWrapperValidator<>), ServiceLifetime.Transient);
|
||||||
});
|
});
|
||||||
|
|
@ -124,7 +124,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_resolve_aggregate_validator_directly()
|
public async Task I_can_resolve_aggregate_validator_directly()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<PersonValidator>(ServiceLifetime.Transient);
|
builder.Add<PersonValidator>(ServiceLifetime.Transient);
|
||||||
builder.Add<AnotherPersonValidator>(ServiceLifetime.Transient);
|
builder.Add<AnotherPersonValidator>(ServiceLifetime.Transient);
|
||||||
|
|
@ -142,7 +142,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_select_validators_from_base_classes_and_interfaces()
|
public async Task I_can_select_validators_from_base_classes_and_interfaces()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<EntityValidator>(ServiceLifetime.Transient);
|
builder.Add<EntityValidator>(ServiceLifetime.Transient);
|
||||||
builder.Add<AnimalValidator>(ServiceLifetime.Transient);
|
builder.Add<AnimalValidator>(ServiceLifetime.Transient);
|
||||||
|
|
@ -161,7 +161,7 @@ internal sealed class DependencyInjectionTests
|
||||||
public async Task I_can_select_validators_polymorphically_based_on_the_instance_type()
|
public async Task I_can_select_validators_polymorphically_based_on_the_instance_type()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddValidation(builder =>
|
services.AddRequestValidation(builder =>
|
||||||
{
|
{
|
||||||
builder.Add<AnimalValidator>(ServiceLifetime.Transient);
|
builder.Add<AnimalValidator>(ServiceLifetime.Transient);
|
||||||
builder.Add<DogValidator>(ServiceLifetime.Transient);
|
builder.Add<DogValidator>(ServiceLifetime.Transient);
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ internal sealed class DispatchingValidator : IValidator
|
||||||
{
|
{
|
||||||
public override Validation Validate(ValidationContext context, IServiceProvider serviceProvider)
|
public override Validation Validate(ValidationContext context, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
var options = serviceProvider.GetRequiredService<IOptions<ValidationOptions>>().Value;
|
var options = serviceProvider.GetRequiredService<IOptions<RequestValidatorOptions>>().Value;
|
||||||
|
|
||||||
var validators = options.GetValidators<T>(serviceProvider);
|
var validators = options.GetValidators<T>(serviceProvider);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ namespace Geekeey.Request.Validation;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines a builder for configuring validator registrations.
|
/// Defines a builder for configuring validator registrations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IValidatorBuilder
|
public interface IRequestValidatorBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the service collection where the validators are registered.
|
/// Gets the service collection where the validators are registered.
|
||||||
|
|
@ -6,24 +6,24 @@ using System.Reflection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
|
||||||
using static Geekeey.Request.Validation.ValidationOptions;
|
using static Geekeey.Request.Validation.RequestValidatorOptions;
|
||||||
|
|
||||||
namespace Geekeey.Request.Validation;
|
namespace Geekeey.Request.Validation;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides extension methods for configuring <see cref="IValidatorBuilder"/>
|
/// Provides extension methods for configuring <see cref="IRequestValidatorBuilder"/>
|
||||||
/// with additional capabilities such as searching and registering validators in assemblies or adding types directly.
|
/// with additional capabilities such as searching and registering validators in assemblies or adding types directly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ValidatorBuilderExtensions
|
public static class RequestValidatorBuilderExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Searches for validator types within the specified assembly and adds them to the validator
|
/// Searches for validator types within the specified assembly and adds them to the validator
|
||||||
/// configuration.
|
/// configuration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="assembly">The assembly to search for validator types.</param>
|
/// <param name="assembly">The assembly to search for validator types.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder SearchInAssembly(this IValidatorBuilder builder, Assembly assembly)
|
public static IRequestValidatorBuilder SearchInAssembly(this IRequestValidatorBuilder builder, Assembly assembly)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
|
|
@ -40,11 +40,11 @@ public static class ValidatorBuilderExtensions
|
||||||
/// Searches for validator types within the specified assembly and adds them to the validator
|
/// Searches for validator types within the specified assembly and adds them to the validator
|
||||||
/// configuration with the given service lifetime.
|
/// configuration with the given service lifetime.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="assembly">The assembly to search for validator types.</param>
|
/// <param name="assembly">The assembly to search for validator types.</param>
|
||||||
/// <param name="lifetime">The lifetime with which the validators are registered in the dependency injection container.</param>
|
/// <param name="lifetime">The lifetime with which the validators are registered in the dependency injection container.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder SearchInAssembly(this IValidatorBuilder builder, Assembly assembly, ServiceLifetime lifetime)
|
public static IRequestValidatorBuilder SearchInAssembly(this IRequestValidatorBuilder builder, Assembly assembly, ServiceLifetime lifetime)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
|
|
@ -60,14 +60,14 @@ public static class ValidatorBuilderExtensions
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified type to the validator configuration for inspection.
|
/// Adds the specified type to the validator configuration for inspection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="type">The type to be added to the validator configuration.</param>
|
/// <param name="type">The type to be added to the validator configuration.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add(this IValidatorBuilder builder, Type type)
|
public static IRequestValidatorBuilder Add(this IRequestValidatorBuilder builder, Type type)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
builder.Services.AddOptions<ValidationOptions>()
|
builder.Services.AddOptions<RequestValidatorOptions>()
|
||||||
.Configure(options => options.Inspect([type]));
|
.Configure(options => options.Inspect([type]));
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|
@ -78,15 +78,15 @@ public static class ValidatorBuilderExtensions
|
||||||
/// This also adds the type to the service collection with the specified lifetime,
|
/// This also adds the type to the service collection with the specified lifetime,
|
||||||
/// allowing it to be resolved as a dependency.
|
/// allowing it to be resolved as a dependency.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> used to configure the validators.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> used to configure the validators.</param>
|
||||||
/// <param name="type">The type to be added to the validator configuration.</param>
|
/// <param name="type">The type to be added to the validator configuration.</param>
|
||||||
/// <param name="lifetime">The lifetime scope of the type in the service container.</param>
|
/// <param name="lifetime">The lifetime scope of the type in the service container.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add(this IValidatorBuilder builder, Type type, ServiceLifetime lifetime)
|
public static IRequestValidatorBuilder Add(this IRequestValidatorBuilder builder, Type type, ServiceLifetime lifetime)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
builder.Services.AddOptions<ValidationOptions>()
|
builder.Services.AddOptions<RequestValidatorOptions>()
|
||||||
.Configure(options => options.Inspect([type]));
|
.Configure(options => options.Inspect([type]));
|
||||||
|
|
||||||
builder.Services.Add(new ServiceDescriptor(type, type, lifetime));
|
builder.Services.Add(new ServiceDescriptor(type, type, lifetime));
|
||||||
|
|
@ -97,16 +97,16 @@ public static class ValidatorBuilderExtensions
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified collection of types to the validator configuration for inspection.
|
/// Adds the specified collection of types to the validator configuration for inspection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="types">The collection of types to be added to the validator configuration.</param>
|
/// <param name="types">The collection of types to be added to the validator configuration.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add(this IValidatorBuilder builder, IEnumerable<Type> types)
|
public static IRequestValidatorBuilder Add(this IRequestValidatorBuilder builder, IEnumerable<Type> types)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
var typeList = types.ToList();
|
var typeList = types.ToList();
|
||||||
|
|
||||||
builder.Services.AddOptions<ValidationOptions>()
|
builder.Services.AddOptions<RequestValidatorOptions>()
|
||||||
.Configure(options => options.Inspect(typeList));
|
.Configure(options => options.Inspect(typeList));
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|
@ -117,17 +117,17 @@ public static class ValidatorBuilderExtensions
|
||||||
/// This also adds the specified collection of types to the service collection with the specified lifetime,
|
/// This also adds the specified collection of types to the service collection with the specified lifetime,
|
||||||
/// allowing it to be resolved as a dependency.
|
/// allowing it to be resolved as a dependency.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="types">The collection of types to be added to the validator configuration.</param>
|
/// <param name="types">The collection of types to be added to the validator configuration.</param>
|
||||||
/// <param name="lifetime">The lifetime scope of the types in the service container.</param>
|
/// <param name="lifetime">The lifetime scope of the types in the service container.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add(this IValidatorBuilder builder, IEnumerable<Type> types, ServiceLifetime lifetime)
|
public static IRequestValidatorBuilder Add(this IRequestValidatorBuilder builder, IEnumerable<Type> types, ServiceLifetime lifetime)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(builder);
|
ArgumentNullException.ThrowIfNull(builder);
|
||||||
|
|
||||||
var typeList = types.ToList();
|
var typeList = types.ToList();
|
||||||
|
|
||||||
builder.Services.AddOptions<ValidationOptions>()
|
builder.Services.AddOptions<RequestValidatorOptions>()
|
||||||
.Configure(options => options.Inspect(typeList));
|
.Configure(options => options.Inspect(typeList));
|
||||||
|
|
||||||
builder.Services.Add(typeList.Select(export => new ServiceDescriptor(export, export, lifetime)));
|
builder.Services.Add(typeList.Select(export => new ServiceDescriptor(export, export, lifetime)));
|
||||||
|
|
@ -139,9 +139,9 @@ public static class ValidatorBuilderExtensions
|
||||||
/// Adds the specified validator type to the validator configuration.
|
/// Adds the specified validator type to the validator configuration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TValidator">The type of the validator to add.</typeparam>
|
/// <typeparam name="TValidator">The type of the validator to add.</typeparam>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add<TValidator>(this IValidatorBuilder builder)
|
public static IRequestValidatorBuilder Add<TValidator>(this IRequestValidatorBuilder builder)
|
||||||
where TValidator : class, IValidator
|
where TValidator : class, IValidator
|
||||||
{
|
{
|
||||||
return builder.Add(typeof(TValidator));
|
return builder.Add(typeof(TValidator));
|
||||||
|
|
@ -151,10 +151,10 @@ public static class ValidatorBuilderExtensions
|
||||||
/// Adds the specified validator type to the validator configuration with the specified lifetime.
|
/// Adds the specified validator type to the validator configuration with the specified lifetime.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TValidator">The type of the validator to add.</typeparam>
|
/// <typeparam name="TValidator">The type of the validator to add.</typeparam>
|
||||||
/// <param name="builder">The <see cref="IValidatorBuilder"/> to configure.</param>
|
/// <param name="builder">The <see cref="IRequestValidatorBuilder"/> to configure.</param>
|
||||||
/// <param name="lifetime">The lifetime scope of the validator in the service container.</param>
|
/// <param name="lifetime">The lifetime scope of the validator in the service container.</param>
|
||||||
/// <returns>The <see cref="IValidatorBuilder"/> instance for further configuration.</returns>
|
/// <returns>The <see cref="IRequestValidatorBuilder"/> instance for further configuration.</returns>
|
||||||
public static IValidatorBuilder Add<TValidator>(this IValidatorBuilder builder, ServiceLifetime lifetime)
|
public static IRequestValidatorBuilder Add<TValidator>(this IRequestValidatorBuilder builder, ServiceLifetime lifetime)
|
||||||
where TValidator : class, IValidator
|
where TValidator : class, IValidator
|
||||||
{
|
{
|
||||||
return builder.Add(typeof(TValidator), lifetime);
|
return builder.Add(typeof(TValidator), lifetime);
|
||||||
|
|
@ -8,12 +8,12 @@ using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Geekeey.Request.Validation;
|
namespace Geekeey.Request.Validation;
|
||||||
|
|
||||||
internal sealed class ValidationOptions
|
internal sealed class RequestValidatorOptions
|
||||||
{
|
{
|
||||||
private readonly List<Type> _search = [];
|
private readonly List<Type> _search = [];
|
||||||
private readonly Lazy<TypeIndex> _validatorsTypeIndex;
|
private readonly Lazy<TypeIndex> _validatorsTypeIndex;
|
||||||
|
|
||||||
public ValidationOptions()
|
public RequestValidatorOptions()
|
||||||
{
|
{
|
||||||
_validatorsTypeIndex = new Lazy<TypeIndex>(() => new ValidatorTypeIndex(_search.Distinct()));
|
_validatorsTypeIndex = new Lazy<TypeIndex>(() => new ValidatorTypeIndex(_search.Distinct()));
|
||||||
}
|
}
|
||||||
|
|
@ -14,15 +14,15 @@ public static class ServiceCollectionExtensions
|
||||||
/// Adds validator services to the specified <see cref="IServiceCollection"/>.
|
/// Adds validator services to the specified <see cref="IServiceCollection"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">The service collection to which the validator services will be added.</param>
|
/// <param name="services">The service collection to which the validator services will be added.</param>
|
||||||
/// <returns>An instance of <see cref="IValidatorBuilder"/> to configure the validator registrations.</returns>
|
/// <returns>An instance of <see cref="IRequestValidatorBuilder"/> to configure the validator registrations.</returns>
|
||||||
public static IValidatorBuilder AddValidation(this IServiceCollection services)
|
public static IRequestValidatorBuilder AddRequestValidation(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(services);
|
ArgumentNullException.ThrowIfNull(services);
|
||||||
|
|
||||||
services.AddOptions<ValidationOptions>();
|
services.AddOptions<RequestValidatorOptions>();
|
||||||
services.AddTransient<IValidator, DispatchingValidator>();
|
services.AddTransient<IValidator, DispatchingValidator>();
|
||||||
|
|
||||||
return new ValidatorBuilder(services);
|
return new RequestValidatorBuilder(services);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -32,17 +32,17 @@ public static class ServiceCollectionExtensions
|
||||||
/// <param name="services">The service collection to which the validator services will be added.</param>
|
/// <param name="services">The service collection to which the validator services will be added.</param>
|
||||||
/// <param name="configure">A delegate to configure the validator builder.</param>
|
/// <param name="configure">A delegate to configure the validator builder.</param>
|
||||||
/// <returns>The service collection with the validator services added.</returns>
|
/// <returns>The service collection with the validator services added.</returns>
|
||||||
public static IServiceCollection AddValidation(this IServiceCollection services, Action<IValidatorBuilder> configure)
|
public static IServiceCollection AddRequestValidation(this IServiceCollection services, Action<IRequestValidatorBuilder> configure)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(services);
|
ArgumentNullException.ThrowIfNull(services);
|
||||||
ArgumentNullException.ThrowIfNull(configure);
|
ArgumentNullException.ThrowIfNull(configure);
|
||||||
|
|
||||||
configure(services.AddValidation());
|
configure(services.AddRequestValidation());
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class ValidatorBuilder(IServiceCollection services) : IValidatorBuilder
|
private sealed class RequestValidatorBuilder(IServiceCollection services) : IRequestValidatorBuilder
|
||||||
{
|
{
|
||||||
public IServiceCollection Services { get; } = services;
|
public IServiceCollection Services { get; } = services;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue