using System; using System.Collections; using System.Globalization; using BS.Shared.Extensions; namespace BeWo.Validation { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class ValidationAttribute : Attribute { public ValidationAttribute() { this.ValidationRule = ValidationRules.AlwaysValid; } public ValidationRules ValidationRule { get; set; } public bool Validate(object pValue) { bool lResult = true; switch (ValidationRule) { case ValidationRules.NotZero: double lNumber; var lIsNumber = Double.TryParse(pValue.ToStringNullCheck(), out lNumber); if (lIsNumber) { lResult = Math.Abs(lNumber) > 0d; } break; case ValidationRules.GreaterThanZero: if (pValue == null) { lResult = false; } else { double lNumber2; var lIsNumber2 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber2); if (lIsNumber2) { lResult = lNumber2 > 0d; } } break; case ValidationRules.GreaterThanOrEqualZero: if (pValue == null) { lResult = false; } else { double lNumber3; var lIsNumber3 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber3); if (lIsNumber3) { lResult = lNumber3 >= 0d; } } break; case ValidationRules.NotNullOrStringEmpty: if (pValue is string) { lResult = ((string)pValue).Trim() != string.Empty; } else { lResult = pValue != null; } break; case ValidationRules.EmptyNoticeInNewServiceRecordsAllowed: if (MainSession.AppSettings.IsServiceRecordNoticeMandatory) { if (pValue is string) { lResult = ((string)pValue).Trim() != string.Empty; } else { lResult = pValue != null; } } break; case ValidationRules.CollectionNotEmpty: if (pValue is ICollection) { lResult = (pValue as ICollection).Count > 0; } break; } return lResult; } } }