mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add fix action in Required attribute
This commit is contained in:
parent
fcc2c39447
commit
6a7036b3ac
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
12
README.md
12
README.md
@ -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
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user