public ValidationResult IsValid(object?value, ValidationContext validationContext, PasswordAttribute validationAttribute)
        {
            if (!(value is string stringValue))
            {
                return(ValidationResult.Success);
            }

            if (_passwordRequirements == null)
            {
                return
                    (validationAttribute.IgnoreIfServiceUnavailable ?
                     ValidationResult.Success :
                     throw new InvalidOperationException($"Password options {typeof(IOptions<PasswordOptions>)} has not been registered or configured."));
            }

            if (IsValidPassword(stringValue))
            {
                return(ValidationResult.Success);
            }

            var result = new ExtendedValidationResult(validationAttribute, validationContext, new[] { validationContext.MemberName });

            if (_passwordRequirements != null)
            {
                result.Properties[PasswordRequirementsPropertyKey] = _passwordRequirements;
            }

            return(result);
        }
示例#2
0
        ExtendedValidationResult ValidateBinding(BindingExpression be, DependencyObject targetObject, DependencyProperty targetProperty)
        {
            Binding b = be.ParentBinding;
            List <ExtendedValidationResult> results = new List <ExtendedValidationResult>();

            foreach (var validationRule in b.ValidationRules)
            {
                if (validationRule is ExtendedValidationRule)
                {
                    results.Add(EvaluateExtendedValidationRule(be, (ExtendedValidationRule)validationRule, targetObject, targetProperty));
                }
            }
            return(ExtendedValidationResult.MergeResults(results));
        }
        public override ExtendedValidationResult Validate(object dataItem, System.Windows.PropertyPath propertyPath, object newValue, System.Globalization.CultureInfo cultureInfo)
        {
            string strValue = newValue as string;

            if (strValue != null && strValue == "XXX")
            {
                return(ExtendedValidationResult.Error("The value may not be XXX"));
            }
            if (strValue != null && strValue == "YYY")
            {
                return(ExtendedValidationResult.Warning("The value should not be YYY, but if you insist..."));
            }
            return(ExtendedValidationResult.Valid);
        }