コード例 #1
0
        public void ValidateProperty(T value, [CallerMemberName] string propertyName = null)
        {
            if (_validationAttributes == null)
            {
                return;
            }

            var context = new ValidationContext(this)
            {
                MemberName = propertyName
            };
            var validationErrors = new List <ValidationResult>();

            if (!DataValidator.TryValidateValue(value, context, validationErrors, _validationAttributes))
            {
                var errors = validationErrors.Select(error => error.ErrorMessage);
                _erros = errors.ToList();
            }
            else
            {
                _erros.Clear();
            }


            RaisePropertyChanged(nameof(ErrorMessage));
            RaisePropertyChanged(nameof(Errors));
            RaisePropertyChanged(nameof(HasErrors));
        }
コード例 #2
0
        public static void TryValidateValueThrowsIf_ValidationAttributeEnumerable_is_null()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = null;
            Assert.Throws <ArgumentNullException>(
                () => Validator.TryValidateValue(new object(), validationContext, validationResults: null, validationAttributes: null));
        }
コード例 #3
0
        public static void TryValidateValue_returns_true_if_no_attributes_to_validate_regardless_of_value()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "NoAttributesProperty";
            Assert.True(Validator.TryValidateValue(null, validationContext,
                                                   validationResults: null, validationAttributes: Enumerable.Empty <ValidationAttribute>()));
            Assert.True(Validator.TryValidateValue(new object(), validationContext,
                                                   validationResults: null, validationAttributes: Enumerable.Empty <ValidationAttribute>()));
        }
コード例 #4
0
        public static void TryValidateValue_returns_true_if_Property_has_no_RequiredAttribute_and_value_is_valid()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyToBeTested";
            var attributesToValidate = new ValidationAttribute[] { new ValidValueStringPropertyAttribute() };

            Assert.True(Validator.TryValidateValue("Valid Value", validationContext, null, attributesToValidate));

            var validationResults = new List <ValidationResult>();

            Assert.True(Validator.TryValidateValue("Valid Value", validationContext, validationResults, attributesToValidate));
            Assert.Equal(0, validationResults.Count);
        }
コード例 #5
0
        public static void TryValidateValue_returns_false_if_Property_has_no_RequiredAttribute_and_value_is_invalid()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var attributesToValidate = new ValidationAttribute[] { new ValidValueStringPropertyAttribute() };

            Assert.False(Validator.TryValidateValue("Invalid Value", validationContext, null, attributesToValidate));

            var validationResults = new List <ValidationResult>();

            Assert.False(Validator.TryValidateValue("Invalid Value", validationContext, validationResults, attributesToValidate));
            Assert.Equal(1, validationResults.Count);
            Assert.Equal("ValidValueStringPropertyAttribute.IsValid failed for value Invalid Value", validationResults[0].ErrorMessage);
        }
コード例 #6
0
        public static void TryValidateValue_returns_false_if_Property_has_RequiredAttribute_and_value_is_null()
        {
            var validationContext = new ValidationContext(new ToBeValidated());

            validationContext.MemberName = "PropertyWithRequiredAttribute";
            var attributesToValidate = new ValidationAttribute[] { new RequiredAttribute(), new ValidValueStringPropertyAttribute() };

            Assert.False(Validator.TryValidateValue(null, validationContext, null, attributesToValidate));

            var validationResults = new List <ValidationResult>();

            Assert.False(Validator.TryValidateValue(null, validationContext, validationResults, attributesToValidate));
            Assert.Equal(1, validationResults.Count);
            // cannot check error message - not defined on ret builds
        }
コード例 #7
0
 public static void TryValidateValueThrowsIf_ValidationContext_is_null()
 {
     Assert.Throws <ArgumentNullException>(
         () => Validator.TryValidateValue(new object(),
                                          validationContext: null, validationResults: null, validationAttributes: Enumerable.Empty <ValidationAttribute>()));
 }