2022-01-15 11:25:12 -05:00
|
|
|
|
using TriInspector.Validators;
|
|
|
|
|
using TriInspector;
|
|
|
|
|
|
|
|
|
|
[assembly: RegisterTriAttributeValidator(typeof(RequiredValidator), ApplyOnArrayElement = true)]
|
|
|
|
|
|
|
|
|
|
namespace TriInspector.Validators
|
|
|
|
|
{
|
|
|
|
|
public class RequiredValidator : TriAttributeValidator<RequiredAttribute>
|
|
|
|
|
{
|
|
|
|
|
public override TriValidationResult Validate(TriProperty property)
|
|
|
|
|
{
|
|
|
|
|
if (property.FieldType == typeof(string))
|
|
|
|
|
{
|
|
|
|
|
var isNull = string.IsNullOrEmpty((string) property.Value);
|
|
|
|
|
if (isNull)
|
|
|
|
|
{
|
2022-05-14 03:02:47 -04:00
|
|
|
|
var message = Attribute.Message ?? $"{GetName(property)} is required";
|
2022-01-15 11:25:12 -05:00
|
|
|
|
return TriValidationResult.Error(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (typeof(UnityEngine.Object).IsAssignableFrom(property.FieldType))
|
|
|
|
|
{
|
|
|
|
|
var isNull = null == (UnityEngine.Object) property.Value;
|
|
|
|
|
if (isNull)
|
|
|
|
|
{
|
2022-05-14 03:02:47 -04:00
|
|
|
|
var message = Attribute.Message ?? $"{GetName(property)} is required";
|
2022-01-15 11:25:12 -05:00
|
|
|
|
return TriValidationResult.Error(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return TriValidationResult.Error("RequiredAttribute only valid on Object and String");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TriValidationResult.Valid;
|
|
|
|
|
}
|
2022-05-14 03:02:47 -04:00
|
|
|
|
|
|
|
|
|
private static string GetName(TriProperty property)
|
|
|
|
|
{
|
|
|
|
|
var name = property.DisplayName;
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
name = property.RawName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
2022-01-15 11:25:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|