Tri-Inspector/Editor.Extras/Validators/ValidateInputValidator.cs

55 lines
1.7 KiB
C#
Raw Normal View History

2022-05-07 12:04:09 -04:00
using System;
using System.Reflection;
using TriInspector;
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>
{
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)
{
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
{
result = _resolver.InvokeForTarget(property, targetIndex);
2022-05-07 12:04:09 -04:00
}
catch (Exception e)
{
if (e is TargetInvocationException targetInvocationException)
{
e = targetInvocationException.InnerException;
}
result = TriValidationResult.Error($"Exception was thrown: {e}");
2022-05-07 12:04:09 -04:00
}
if (!result.IsValid)
{
return result;
}
}
return TriValidationResult.Valid;
}
}
}