Add fix action in Required attribute

This commit is contained in:
VladV 2023-06-24 18:08:40 +04:00
parent fcc2c39447
commit 6a7036b3ac
5 changed files with 57 additions and 9 deletions

View File

@ -1,5 +1,7 @@
using TriInspector.Validators;
using JetBrains.Annotations;
using TriInspector.Validators;
using TriInspector;
using TriInspector.Resolvers;
[assembly: RegisterTriAttributeValidator(typeof(RequiredValidator), ApplyOnArrayElement = true)]
@ -7,6 +9,18 @@ namespace TriInspector.Validators
{
public class RequiredValidator : TriAttributeValidator<RequiredAttribute>
{
[CanBeNull] private ActionResolver _fixActionResolver;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (Attribute.FixAction != null)
{
_fixActionResolver = ActionResolver.Resolve(propertyDefinition, Attribute.FixAction);
}
return TriExtensionInitializationResult.Ok;
}
public override TriValidationResult Validate(TriProperty property)
{
if (property.FieldType == typeof(string))
@ -15,7 +29,7 @@ namespace TriInspector.Validators
if (isNull)
{
var message = Attribute.Message ?? $"{GetName(property)} is required";
return TriValidationResult.Error(message);
return MakeError(message, property);
}
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(property.FieldType))
@ -24,7 +38,7 @@ namespace TriInspector.Validators
if (isNull)
{
var message = Attribute.Message ?? $"{GetName(property)} is required";
return TriValidationResult.Error(message);
return MakeError(message, property);
}
}
else
@ -35,6 +49,23 @@ namespace TriInspector.Validators
return TriValidationResult.Valid;
}
private TriValidationResult MakeError(string error, TriProperty property)
{
var result = TriValidationResult.Error(error);
if (_fixActionResolver != null)
{
result = AddFix(result, property);
}
return result;
}
private TriValidationResult AddFix(TriValidationResult result, TriProperty property)
{
return result.WithFix(() => _fixActionResolver?.InvokeForAllTargets(property), Attribute.FixActionName);
}
private static string GetName(TriProperty property)
{
var name = property.DisplayName;

View File

@ -1,8 +1,14 @@
using TriInspector;
using System.Linq;
using TriInspector;
using UnityEngine;
public class Validators_RequiredSample : ScriptableObject
{
[Required]
public Material mat;
[Required(FixAction = nameof(FixMaterial), FixActionName = "Find in Resources")]
public Material material;
private void FixMaterial()
{
material = Resources.FindObjectsOfTypeAll<Material>().FirstOrDefault();
}
}

View File

@ -143,11 +143,19 @@ Tri Inspector has some builtin validators such as `missing reference` and `type
#### Required
![Required](https://user-images.githubusercontent.com/26966368/168233232-596535b4-bab8-462e-b5d8-7a1c090e5143.png)
![Required](https://github.com/codewriter-packages/Tri-Inspector/assets/26966368/56a8d0ef-c88b-4b4b-8121-388b94d47841)
```csharp
[Required]
public Material mat;
public Material material;
[Required(FixAction = nameof(FixTarget), FixActionName = "Assign self")]
public Transform target;
private void FixTarget()
{
target = GetComponent<Transform>();
}
```
#### ValidateInput

View File

@ -6,5 +6,8 @@ namespace TriInspector
public class RequiredAttribute : Attribute
{
public string Message { get; set; }
public string FixAction { get; set; }
public string FixActionName { get; set; }
}
}

View File

@ -25,7 +25,7 @@ namespace TriInspector
public TriValidationResult WithFix(Action action, string name = null)
{
return new TriValidationResult(IsValid, Message, MessageType, action, new GUIContent(name));
return new TriValidationResult(IsValid, Message, MessageType, action, new GUIContent(name ?? "Fix"));
}
public static TriValidationResult Info(string error)