feat: add support for validator resolution from dependency injection
This commit is contained in:
parent
21ca8a3757
commit
22142882b5
19 changed files with 806 additions and 1 deletions
49
src/request.validation/ServiceCollectionExtensions.cs
Normal file
49
src/request.validation/ServiceCollectionExtensions.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Geekeey.Request.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for configuring and registering validator services in the <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds validator services to the specified <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public static IValidatorBuilder AddValidation(this IServiceCollection services)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
|
||||
services.AddOptions<ValidationOptions>();
|
||||
services.AddTransient<IValidator, DispatchingValidator>();
|
||||
|
||||
return new ValidatorBuilder(services);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds validator services to the specified <see cref="IServiceCollection"/>
|
||||
/// and configures them using the provided <see cref="Action{T}"/>.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// <returns>The service collection with the validator services added.</returns>
|
||||
public static IServiceCollection AddValidation(this IServiceCollection services, Action<IValidatorBuilder> configure)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
ArgumentNullException.ThrowIfNull(configure);
|
||||
|
||||
configure(services.AddValidation());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private sealed class ValidatorBuilder(IServiceCollection services) : IValidatorBuilder
|
||||
{
|
||||
public IServiceCollection Services { get; } = services;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue