2022-05-07 12:04:09 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using TriInspector;
|
2022-05-09 08:12:30 -04:00
|
|
|
|
using TriInspector.Resolvers;
|
2022-05-07 12:04:09 -04:00
|
|
|
|
using TriInspector.Validators;
|
|
|
|
|
|
2022-05-07 12:30:05 -04:00
|
|
|
|
[assembly: RegisterTriAttributeValidator(typeof(ValidateInputValidator))]
|
2022-05-07 12:04:09 -04:00
|
|
|
|
|
|
|
|
|
namespace TriInspector.Validators
|
|
|
|
|
{
|
|
|
|
|
public class ValidateInputValidator : TriAttributeValidator<ValidateInputAttribute>
|
|
|
|
|
{
|
2022-05-09 08:12:30 -04:00
|
|
|
|
private MethodResolver<TriValidationResult> _resolver;
|
|
|
|
|
|
|
|
|
|
public override void Initialize(TriPropertyDefinition propertyDefinition)
|
|
|
|
|
{
|
|
|
|
|
base.Initialize(propertyDefinition);
|
|
|
|
|
|
|
|
|
|
_resolver = MethodResolver.Resolve<TriValidationResult>(propertyDefinition, Attribute.Method);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 12:04:09 -04:00
|
|
|
|
public override TriValidationResult Validate(TriProperty property)
|
|
|
|
|
{
|
2022-05-09 08:12:30 -04:00
|
|
|
|
if (_resolver.TryGetErrorString(out var error))
|
|
|
|
|
{
|
|
|
|
|
return TriValidationResult.Error(error);
|
|
|
|
|
}
|
2022-05-07 12:04:09 -04:00
|
|
|
|
|
|
|
|
|
for (var targetIndex = 0; targetIndex < property.PropertyTree.TargetObjects.Length; targetIndex++)
|
|
|
|
|
{
|
|
|
|
|
TriValidationResult result;
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-05-09 08:12:30 -04:00
|
|
|
|
result = _resolver.InvokeForTarget(property, targetIndex);
|
2022-05-07 12:04:09 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (e is TargetInvocationException targetInvocationException)
|
|
|
|
|
{
|
|
|
|
|
e = targetInvocationException.InnerException;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-09 08:12:30 -04:00
|
|
|
|
result = TriValidationResult.Error($"Exception was thrown: {e}");
|
2022-05-07 12:04:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TriValidationResult.Valid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|