Example #1
0
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (validationContext == null)
        {
            return(null);
        }
        var property = validationContext.ObjectType.GetProperty(_nameOfBoolProp);

        if (property == null)
        {
            return(new ValidationResult($"{_nameOfBoolProp} not found"));
        }
        var boolVal = property.GetValue(validationContext.ObjectInstance, null);

        if (boolVal == null || boolVal.GetType() != typeof(bool))
        {
            return(new ValidationResult($"{_nameOfBoolProp} not boolean"));
        }
        if ((bool)boolVal)
        {
            var attribute = new EmailAddressAttribute {
                ErrorMessage = $"{value} is not a valid e-mail address."
            };
            return(attribute.GetValidationResult(value, validationContext));
        }
        return(null);
    }
Example #2
0
            public void ValidParisAddress_IsValid()
            {
                string value = "*****@*****.**", name = "Email";
                var    attr    = new EmailAddressAttribute();
                var    context = new ValidationContext(new object(), null, null);

                context.MemberName = name;
                var result = attr.GetValidationResult(value, context);

                Assert.IsNull(result);
            }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (ignoreEmpty && (value == null || value.ToString() == string.Empty))
            {
                return(ValidationResult.Success);
            }

            var result       = new EmailAddressAttribute();
            var errorMessage = string.IsNullOrEmpty(this.ErrorMessage) ? $"Invalid email address." : this.ErrorMessage;

            result.ErrorMessage = errorMessage;

            return(result.GetValidationResult(value, validationContext));
        }
Example #4
0
            public void InvalidAddress_IsInvalid()
            {
                string value = "test", name = "Email";
                var    attr    = new EmailAddressAttribute();
                var    context = new ValidationContext(new object(), null, null);

                context.MemberName = name;
                var result = attr.GetValidationResult(value, context);

                Assert.IsNotNull(result);
                Assert.AreEqual("A valid email address is required.", result.ErrorMessage);
                Assert.AreEqual(1, result.MemberNames.Count());
                Assert.AreEqual(name, result.MemberNames.Single());
            }