| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | using FluentValidation; |
| | | 4 | | |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.FluentValidation; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Extension methods for registering FluentValidation validators as options validators. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class FluentValidationServiceCollectionExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Adds a FluentValidation validator as an options validator for the specified options type. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="TOptions">The options type to validate.</typeparam> |
| | | 20 | | /// <typeparam name="TValidator">The FluentValidation validator type.</typeparam> |
| | | 21 | | /// <param name="services">The service collection.</param> |
| | | 22 | | /// <returns>The service collection for chaining.</returns> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// <para> |
| | | 25 | | /// This method registers: |
| | | 26 | | /// <list type="bullet"> |
| | | 27 | | /// <item>The validator as a singleton</item> |
| | | 28 | | /// <item>An <see cref="IValidateOptions{TOptions}"/> adapter that uses the validator</item> |
| | | 29 | | /// </list> |
| | | 30 | | /// </para> |
| | | 31 | | /// <para> |
| | | 32 | | /// Example usage: |
| | | 33 | | /// <code> |
| | | 34 | | /// services.AddFluentValidationOptionsAdapter<DatabaseOptions, DatabaseOptionsValidator>(); |
| | | 35 | | /// </code> |
| | | 36 | | /// </para> |
| | | 37 | | /// </remarks> |
| | | 38 | | public static IServiceCollection AddFluentValidationOptionsAdapter<TOptions, [DynamicallyAccessedMembers(Dynamically |
| | | 39 | | this IServiceCollection services) |
| | | 40 | | where TOptions : class |
| | | 41 | | where TValidator : class, IValidator<TOptions> |
| | | 42 | | { |
| | 3 | 43 | | services.TryAddSingleton<TValidator>(); |
| | 6 | 44 | | services.TryAddSingleton<IValidator<TOptions>>(sp => sp.GetRequiredService<TValidator>()); |
| | 3 | 45 | | services.AddSingleton<IValidateOptions<TOptions>>(sp => |
| | 6 | 46 | | new FluentValidationOptionsAdapter<TOptions>(sp.GetRequiredService<IValidator<TOptions>>())); |
| | | 47 | | |
| | 3 | 48 | | return services; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Adds a FluentValidation validator as an options validator for named options. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <typeparam name="TOptions">The options type to validate.</typeparam> |
| | | 55 | | /// <typeparam name="TValidator">The FluentValidation validator type.</typeparam> |
| | | 56 | | /// <param name="services">The service collection.</param> |
| | | 57 | | /// <param name="name">The name of the options instance to validate.</param> |
| | | 58 | | /// <returns>The service collection for chaining.</returns> |
| | | 59 | | public static IServiceCollection AddFluentValidationOptionsAdapter<TOptions, [DynamicallyAccessedMembers(Dynamically |
| | | 60 | | this IServiceCollection services, |
| | | 61 | | string name) |
| | | 62 | | where TOptions : class |
| | | 63 | | where TValidator : class, IValidator<TOptions> |
| | | 64 | | { |
| | 0 | 65 | | services.TryAddSingleton<TValidator>(); |
| | 0 | 66 | | services.TryAddSingleton<IValidator<TOptions>>(sp => sp.GetRequiredService<TValidator>()); |
| | 0 | 67 | | services.AddSingleton<IValidateOptions<TOptions>>(sp => |
| | 0 | 68 | | new FluentValidationOptionsAdapter<TOptions>(sp.GetRequiredService<IValidator<TOptions>>(), name)); |
| | | 69 | | |
| | 0 | 70 | | return services; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Adds a FluentValidation validator instance as an options validator. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <typeparam name="TOptions">The options type to validate.</typeparam> |
| | | 77 | | /// <param name="services">The service collection.</param> |
| | | 78 | | /// <param name="validator">The validator instance.</param> |
| | | 79 | | /// <returns>The service collection for chaining.</returns> |
| | | 80 | | public static IServiceCollection AddFluentValidationOptionsAdapter<TOptions>( |
| | | 81 | | this IServiceCollection services, |
| | | 82 | | IValidator<TOptions> validator) |
| | | 83 | | where TOptions : class |
| | | 84 | | { |
| | 0 | 85 | | services.AddSingleton<IValidateOptions<TOptions>>( |
| | 0 | 86 | | new FluentValidationOptionsAdapter<TOptions>(validator)); |
| | | 87 | | |
| | 0 | 88 | | return services; |
| | | 89 | | } |
| | | 90 | | } |