2026-05-28 20:12:00 +02:00
|
|
|
// Copyright (c) The Geekeey Authors
|
|
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Reflection.Metadata;
|
|
|
|
|
|
|
|
|
|
[assembly: MetadataUpdateHandler(typeof(Geekeey.Request.Validation.DispatchingValidator))]
|
|
|
|
|
|
|
|
|
|
namespace Geekeey.Request.Validation;
|
|
|
|
|
|
2026-05-30 19:36:28 +02:00
|
|
|
internal sealed partial class DispatchingValidator : IValidator
|
2026-05-28 20:12:00 +02:00
|
|
|
{
|
|
|
|
|
private static readonly ConcurrentDictionary<Type, ValidatorInvoker> ValidatorsHandlers = new();
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
|
|
|
|
public DispatchingValidator(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 20:39:51 +02:00
|
|
|
public static void ClearCache(Type[]? _)
|
|
|
|
|
{
|
|
|
|
|
ValidatorsHandlers.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 20:12:00 +02:00
|
|
|
public Validation Validate(ValidationContext context)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(context);
|
|
|
|
|
|
|
|
|
|
if (context.Instance is null)
|
|
|
|
|
{
|
|
|
|
|
return new Validation([]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var handler = ValidatorsHandlers.GetOrAdd(context.Instance.GetType(), static key =>
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(ValidatorInvoker<>).MakeGenericType(key);
|
|
|
|
|
return (ValidatorInvoker)Activator.CreateInstance(type)!;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return handler.Validate(context, _serviceProvider);
|
|
|
|
|
}
|
|
|
|
|
}
|