public void ErrorDescriptorsTest() { ErrorDescriptors eds = new ErrorDescriptors() { new ErrorDescriptor(ErrorSeverity.Default, "My default error descriptor"), new ErrorDescriptor(ErrorSeverity.Info, "My info error descriptor"), new ErrorDescriptor(ErrorSeverity.Warning, "My warning error descriptor"), new ErrorDescriptor(ErrorSeverity.Error, "My error descriptor") }; // Constructor tests Assert.Equal("My info error descriptor", eds[1].Message); Assert.Equal(ErrorSeverity.Info, eds[1].Severity); // Serialize and de-serialize test var serialized = JsonConvert.SerializeObject(eds); var converter = new ErrorDescriptorsJsonConverter(); var deserialized = (ErrorDescriptors)converter.Convert(new[] { new Exception(serialized) }, eds.GetType(), null, CultureInfo.InvariantCulture); Assert.Equal(4, deserialized.Count()); for (int i = 0; i < eds.Count; i++) { Assert.Equal(eds[i], deserialized[i]); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is IEnumerable <object> rawObj) { var descriptors = new ErrorDescriptors(); foreach (var obj in rawObj) { switch (obj) { case ErrorDescriptor ed: descriptors.Add(ed); break; case Exception ex: Logger.LogError(ex); break; } } return(GetColorFromDescriptors(descriptors)); } return(null); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var descriptors = new ErrorDescriptors(); if (value is IEnumerable <object> rawObj) { foreach (var error in rawObj.OfType <ErrorDescriptor>()) { descriptors.Add(error); } foreach (var ex in rawObj.OfType <Exception>()) { descriptors.Add(UndefinedExceptionToErrorDescriptor(ex)); } } else if (value is Exception ex) { descriptors.Add(UndefinedExceptionToErrorDescriptor(ex)); } else { return(null); } return(GetColorFromDescriptors(descriptors)); }
void IRegisterValidationMethod.RegisterValidationMethod(string propertyName, ValidateMethod validateMethod) { if (string.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentException("PropertyName must be valid.", nameof(propertyName)); } ValidationMethods[propertyName] = validateMethod; ErrorsByPropertyName[propertyName] = ErrorDescriptors.Create(); }
public static ErrorDescriptors ValidatePassword(string password) { var errors = new ErrorDescriptors(); if (IsTrimable(password, out _)) { errors.Add(new ErrorDescriptor(ErrorSeverity.Warning, TrimWarnMessage)); } if (IsTooLong(password, out _)) { errors.Add(new ErrorDescriptor(ErrorSeverity.Error, PasswordTooLongMessage)); } return(errors); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is IEnumerable <object> rawObj) { var descriptors = new ErrorDescriptors(); foreach (var error in rawObj.OfType <ErrorDescriptor>()) { descriptors.Add(error); } return(GetColorFromDescriptors(descriptors)); } return(null); }
private SolidColorBrush GetColorFromDescriptors(ErrorDescriptors descriptors) { if (!descriptors.Any()) { return(null); } var highPrioError = descriptors.OrderByDescending(p => p.Severity).FirstOrDefault(); if (ErrorSeverityColorConverter.ErrorSeverityColors.TryGetValue(highPrioError.Severity, out var brush)) { return(brush); } else { return(null); } }
public ErrorDescriptors ValidatePassword() { string password = Password; var errors = new ErrorDescriptors(); if (PasswordHelper.IsTrimable(password, out _)) { errors.Add(new ErrorDescriptor(ErrorSeverity.Error, "Leading and trailing white spaces are not allowed!")); } if (PasswordHelper.IsTooLong(password, out _)) { errors.Add(new ErrorDescriptor(ErrorSeverity.Error, PasswordHelper.PasswordTooLongMessage)); } return(errors); }
internal static ErrorDescriptors ExceptionListToErrorDescriptor(IEnumerable <Exception> exList) { var errors = new ErrorDescriptors(); foreach (var exMsg in exList.Select(p => p.Message).Where(p => p != null)) { try { errors.AddRange(JsonConvert.DeserializeObject <ErrorDescriptors>(exMsg)); } catch (Exception e) { errors.Add(new ErrorDescriptor(ErrorSeverity.Error, e.Message)); errors.Add(new ErrorDescriptor(ErrorSeverity.Error, exMsg)); } } return(errors); }