Extending RequiredValidator class shows an error in Inspector

Issue #577 new
Fred created an issue

I am trying to extend the Required attribute so it can handle a specific type I created. Unfortunately, doing so gives me this rror in the Inspector above every field using the Required attribute.

This is the code that extends the Required Attribute:

using ScriptableObjectArchitecture;
using UnityEditor;

#if UNITY_EDITOR

[assembly: Sirenix.OdinInspector.Editor.Validation.RegisterValidator(typeof(Tangier.Core.TangierRequiredValidator))]

namespace Tangier.Core
{
    using System;
    using System.Reflection;
    using Sirenix.OdinInspector;
    using Sirenix.OdinInspector.Editor;
    using Sirenix.OdinInspector.Editor.Validation;

    public class TangierRequiredValidator : RequiredValidator
    {
        private StringMemberHelper stringHelper;

        public override void Initialize(MemberInfo member, Type memberValueType)
        {
            base.Initialize(member, memberValueType);

            if (this.Attribute.ErrorMessage != null)
            {
                this.stringHelper = new StringMemberHelper(member.ReflectedType, false, this.Attribute.ErrorMessage);
            }
        }

        protected override void Validate(object parentInstance, object memberValue, MemberInfo member, ValidationResult result)
        {
            if (!(memberValue is BaseReference basReferenceValue))
            {
                base.Validate(parentInstance, memberValue, member, result);
                return;
            }

            if (!basReferenceValue.IsValueDefined)
            {
                result.ResultType = this.Attribute.MessageType.ToValidationResultType();
                result.Message = this.stringHelper != null ? this.stringHelper.GetString(parentInstance) : (member.Name + " has to be constant or have a reference to a Variable");
            }
        }
    }
}
#endif

I was told this should work so I figured it is a bug that should be tracked.

Comments (0)

  1. Log in to comment