Blog.Common.Web.Attributes.ValidateUsernameAttribute.IsValid C# (CSharp) Method

IsValid() public method

public IsValid ( object value ) : bool
value object
return bool
        public override bool IsValid(object value)
        {
            try
            {
                if (value == null)
                {
                    ErrorMessage = "Model property is null/empty.";
                    return false;
                }

                if (value.GetType() != typeof (string))
                {
                    ErrorMessage ="Model property is not a string.";
                    return false;
                }

                var username = value.ToString();
                if (!string.IsNullOrEmpty(username))
                {
                    var result = IsValidUsername(username);
                    if (result)
                    {
                        ErrorMessage = string.Empty;
                        return true;
                    }

                    ErrorMessage = "Username is already in use";
                    return false;
                }

                ErrorMessage = "Model property is null/empty.";
                return false;
            }
            catch (Exception ex)
            {
                throw new BlogException(ex.Message, ex.InnerException);
            }
        }
        

Usage Example

        public void ShouldReturnFalseWhenParameterIsNull()
        {
            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = attr.IsValid(null);

            Assert.IsFalse(result);
            Assert.AreEqual("Model property is null/empty.", attr.ErrorMessage);
        }
All Usage Examples Of Blog.Common.Web.Attributes.ValidateUsernameAttribute::IsValid
ValidateUsernameAttribute