feat: add support for validator resolution from dependency injection

This commit is contained in:
Louis Seubert 2026-05-28 20:12:00 +02:00
commit 22142882b5
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
19 changed files with 806 additions and 1 deletions

View 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;
}
}