public void OnAuthorizationThrowsIfFilterContextIsNull()
        {
            // Arrange
            ValidateInputAttribute attr = new ValidateInputAttribute(true);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { attr.OnAuthorization(null); }, "filterContext");
        }
Example #2
0
        public void OnAuthorizationThrowsIfFilterContextIsNull()
        {
            // Arrange
            ValidateInputAttribute attr = new ValidateInputAttribute(true);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { attr.OnAuthorization(null); }, "filterContext");
        }
        public void EnableValidationProperty()
        {
            // Act
            ValidateInputAttribute attrTrue = new ValidateInputAttribute(true);
            ValidateInputAttribute attrFalse = new ValidateInputAttribute(false);

            // Assert
            Assert.True(attrTrue.EnableValidation);
            Assert.False(attrFalse.EnableValidation);
        }
Example #4
0
        public void EnableValidationProperty()
        {
            // Act
            ValidateInputAttribute attrTrue  = new ValidateInputAttribute(true);
            ValidateInputAttribute attrFalse = new ValidateInputAttribute(false);

            // Assert
            Assert.True(attrTrue.EnableValidation);
            Assert.False(attrFalse.EnableValidation);
        }
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(IPropertyValueEntry <T> entry, ValidateInputAttribute attribute, GUIContent label)
        {
            PropertyContext <InnerDrawer> context;

            if (entry.Context.Get(this, "inner_drawer", out context))
            {
                context.Value = (InnerDrawer)Activator.CreateInstance(typeof(InnerDrawer <>).MakeGenericType(typeof(T), entry.ParentType), this);
            }

            context.Value.DrawPropertyLayout(entry, attribute, label);
        }
        public void OnAuthorizationSetsControllerValidateRequestToTrue()
        {
            // Arrange
            Controller controller = new EmptyController() { ValidateRequest = false };
            ValidateInputAttribute attr = new ValidateInputAttribute(enableValidation: true);
            AuthorizationContext authContext = GetAuthorizationContext(controller);

            // Act
            attr.OnAuthorization(authContext);

            // Assert
            Assert.True(controller.ValidateRequest);
        }
Example #7
0
        public override void ValidateProperty(SerializedProperty property, bool drawField)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property);

            if (validateInputAttribute.HideWithField && !drawField)
            {
                return;
            }

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName);

            if (validationCallback != null &&
                validationCallback.ReturnType == typeof(bool) &&
                validationCallback.GetParameters().Length == 1)
            {
                FieldInfo fieldInfo     = ReflectionUtility.GetField(target, property.name);
                Type      fieldType     = fieldInfo.FieldType;
                Type      parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }))
                    {
                        if (string.IsNullOrEmpty(validateInputAttribute.Message))
                        {
                            EditorDrawUtility.DrawHelpBox(property.name + " is not valid", MessageType.Error, logToConsole: true, context: target);
                        }
                        else
                        {
                            EditorDrawUtility.DrawHelpBox(validateInputAttribute.Message, MessageType.Error, logToConsole: true, context: target);
                        }
                    }
                }
                else
                {
                    string warning = "The field type is not the same as the callback's parameter type";
                    EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
                }
            }
            else
            {
                string warning =
                    validateInputAttribute.GetType().Name +
                    " needs a callback with boolean return type and a single parameter of the same type as the field";

                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
            }
        }
Example #8
0
        public void OnAuthorizationSetsControllerValidateRequestToTrue()
        {
            // Arrange
            Controller controller = new EmptyController()
            {
                ValidateRequest = false
            };
            ValidateInputAttribute attr        = new ValidateInputAttribute(enableValidation: true);
            AuthorizationContext   authContext = GetAuthorizationContext(controller);

            // Act
            attr.OnAuthorization(authContext);

            // Assert
            Assert.True(controller.ValidateRequest);
        }
Example #9
0
        public override void ValidateProperty(SerializedProperty property)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property);
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName);

            if (validationCallback != null &&
                validationCallback.ReturnType == typeof(bool) &&
                validationCallback.GetParameters().Length == 1)
            {
                FieldInfo fieldInfo     = ReflectionUtility.GetField(target, property.name);
                Type      fieldType     = fieldInfo.FieldType;
                Type      parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }))
                    {
                        if (string.IsNullOrEmpty(validateInputAttribute.Message))
                        {
                            NaughtyEditorGUI.HelpBox_Layout(
                                property.name + " is not valid", MessageType.Error, context: property.serializedObject.targetObject);
                        }
                        else
                        {
                            NaughtyEditorGUI.HelpBox_Layout(
                                validateInputAttribute.Message, MessageType.Error, context: property.serializedObject.targetObject);
                        }
                    }
                }
                else
                {
                    string warning = "The field type is not the same as the callback's parameter type";
                    NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
                }
            }
            else
            {
                string warning =
                    validateInputAttribute.GetType().Name +
                    " needs a callback with boolean return type and a single parameter of the same type as the field";

                NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
            }
        }
        public override void ValidateProperty(SerializedProperty property)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName);

            if (validationCallback != null &&
                validationCallback.ReturnType == typeof(bool) &&
                validationCallback.GetParameters().Length == 1)
            {
                FieldInfo fieldInfo     = ReflectionUtility.GetField(target, property.name);
                Type      fieldType     = fieldInfo.FieldType;
                Type      parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }))
                    {
                        if (string.IsNullOrEmpty(validateInputAttribute.Message))
                        {
                            EditorDrawUtility.DrawHelpBox(property.name + " 不是有效的", MessageType.Error, context: target, logToConsole: false);
                        }
                        else
                        {
                            EditorDrawUtility.DrawHelpBox(validateInputAttribute.Message, MessageType.Error, context: target, logToConsole: false);
                        }
                    }
                }
                else
                {
                    string warning = "这个字段类型跟回调函数参数类型不同啊!";
                    EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target, logToConsole: false);
                }
            }
            else
            {
                string warning =
                    validateInputAttribute.GetType().Name +
                    " 需要一个返回布尔类型的回调函数和一个与字段类型相同的单参!";


                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target, logToConsole: false);
            }
        }
        public static void RegisterAvoidValidate()
        {
            ValidateInputAttribute doNotValidateInputAttribute = new ValidateInputAttribute(false);

            Controller <ValidatorController>()
            .Action(c => c.Validate(null, null))
            .AddFilters(ctx =>
            {
                var form   = ctx.ControllerContext.HttpContext.Request.Form;
                var prefix = form["prefix"];

                RuntimeInfo ri = RuntimeInfo.FromFormValue(form[TypeContextUtilities.Compose(prefix, EntityBaseKeys.RuntimeInfo)]);

                if (AvoidValidate(ri.EntityType))
                {
                    return(doNotValidateInputAttribute);
                }

                if (form.AllKeys.Contains(EntityBaseKeys.RuntimeInfo))
                {
                    RuntimeInfo riBase = RuntimeInfo.FromFormValue(form[EntityBaseKeys.RuntimeInfo]);
                    if (AvoidValidate(riBase.EntityType))
                    {
                        return(doNotValidateInputAttribute);
                    }
                }

                return(null);
            });

            Controller <OperationController>()
            .Action(c => c.Execute())
            .AddFilters(ctx =>
            {
                var form   = ctx.ControllerContext.HttpContext.Request;
                var prefix = form["prefix"];

                RuntimeInfo ri = RuntimeInfo.FromFormValue(form[TypeContextUtilities.Compose(prefix, EntityBaseKeys.RuntimeInfo)]);
                if (AvoidValidate(ri.EntityType))
                {
                    return(doNotValidateInputAttribute);
                }
                return(null);
            });
        }
Example #12
0
        public override void ValidateProperty(SerializedProperty property)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttributes <ValidateInputAttribute>(property)[0];

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            MethodInfo validationCallback =
                target.GetType().GetMethod(validateInputAttribute.CallbackName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (validationCallback != null &&
                validationCallback.ReturnType == typeof(bool) &&
                validationCallback.GetParameters().Length == 1)
            {
                FieldInfo fieldInfo     = target.GetType().GetField(property.name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                Type      fieldType     = fieldInfo.FieldType;
                Type      parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }))
                    {
                        if (string.IsNullOrEmpty(validateInputAttribute.Message))
                        {
                            this.DrawHelpBox(property.name + " is not valid", target, MessageType.Error);
                        }
                        else
                        {
                            this.DrawHelpBox(validateInputAttribute.Message, target, MessageType.Error);
                        }
                    }
                }
                else
                {
                    this.DrawHelpBox("The field type is not the same as the callback's parameter type", target, MessageType.Warning);
                }
            }
            else
            {
                this.DrawHelpBox(validateInputAttribute.GetType().Name +
                                 " needs a callback with boolean return type and a single parameter of the same type as the field", target, MessageType.Warning);
            }
        }
 public abstract void DrawPropertyLayout(IPropertyValueEntry <T> entry, ValidateInputAttribute attribute, GUIContent label);