49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
|
|
// 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;
|
||
|
|
}
|
||
|
|
}
|