/// <summary> /// Validates the name of the user. /// </summary> /// <param name="value">Name of the user.</param> /// <param name="idValue">id value of the entity. It will be used in update mode to check duplicate</param> /// <param name="fnName">Business rule function if it is insert or update</param> /// <param name="errors">The errors.</param> /// <param name="throwIfErrors">Throw BRException if an error happened</param> public bool ValidateUserName(string value, long?idValue, BusinessRuleErrorList errors, RuleFunctionSEnum fnName, bool throwIfErrors) { int errCount = errors.Count; if (CheckUtils.CheckStringShouldNotBeNullOrEmpty(vUser.ColumnNames.UserName, value, errors) == false) { if (throwIfErrors && errors.Count > errCount) { throw new BRException(errors); } else { return(false); } } string colName = vUser.ColumnNames.UserName; //Must consist at least two characters that are alpha characters a-zA-Z //Must consist only ONE underscore or dash allowed anywhere AFTER the first check, //the dash/underscore cannot be at the end as the same rule to apply as the first step //Must be alpha-numeric characters. //var colInfo = this.Entity.EntityColumns[User.ColumnNames.UserName]; if (fnName == RuleFunctionSEnum.Delete) { return(true); } // DEVELOPER NOTE: Change this pattern with pattern specified in UI in FWHtml.cs file for editor // format check // http://stackoverflow.com/questions/3588623/c-sharp-regex-for-a-username-with-a-few-restrictions string valuePattern = @"^(?=.{5,50}$)([A-Za-z0-9][._]?)*$"; //(?=.{5,50}$) Must be 5-50 characters in the string //([A-Za-z0-9][._()\[\]-]?)* The string is a sequence of alphanumerics, // each of which may be followed by a symbol if (System.Text.RegularExpressions.Regex.IsMatch( value, valuePattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase) == false) { errors.Add(colName, BusinessErrorStrings.User.UserName_RegularExpressionCheck ); } // duplicate check if (errors.Count == 0) // Perfomance: We check database only if no error is there. { value = value.ToLower(); // we store all user names in lower case CheckUtils.CheckDuplicateValueNotToBeExists(colName, value, idValue, errors, null, fnName == RuleFunctionSEnum.Insert, BusinessErrorStrings.User.UserName_DuplicateUserName); } if (errors.Count > 0 && throwIfErrors) { throw new BRException(errors); } return(errors.Count == 0); }
/// <summary> /// Validates the phone number /// </summary> /// <param name="value">Phone Number</param> /// <param name="idValue">id value of the entity. It will be used in update mode to check duplicate</param> /// <param name="fnName">Business rule function if it is insert or update</param> /// <param name="canBeNull">See if Phone number can be null or empty</param> /// <param name="errors">The errors.</param> /// <param name="throwIfErrors">Throw BRException if an error happened</param> public bool ValidatePhoneNumber(string value, long?idValue, bool canBeNull, BusinessRuleErrorList errors, RuleFunctionSEnum fnName, bool throwIfErrors) { int errCount = errors.Count; // To simplify Signup, we removed Phone number as mandatory // in addition, RegisterAndLogin option doesn't need to have a phone number // However, if a phone number is provided, we need to check its format if (string.IsNullOrEmpty(value) && canBeNull) { return(true); } if (CheckUtils.CheckStringShouldNotBeNullOrEmpty(vUser.ColumnNames.PhoneNumber, value, errors) == false) { if (throwIfErrors && errors.Count > errCount) { throw new BRException(errors); } else { return(false); } } string colName = vUser.ColumnNames.PhoneNumber; if (fnName == RuleFunctionSEnum.Delete) { return(true); } if (IsValidPhoneNumberE164(value) == false) { errors.Add(colName, BusinessErrorStrings.User.PhoneNumber_NotE164); } // duplicate check if (errors.Count == 0) // Perfomance: We check database only if no error is there. { value = value.ToLower(); // we store all user names in lower case CheckUtils.CheckDuplicateValueNotToBeExists(colName, value, idValue, errors, null, fnName == RuleFunctionSEnum.Insert, BusinessErrorStrings.User.PhoneNumber_DuplicatePhoneNumber); } if (errors.Count > 0 && throwIfErrors) { throw new BRException(errors); } return(errors.Count == 0); }
public bool ValidateEmail(string value, long?idValue, BusinessRuleErrorList errors, RuleFunctionSEnum fnName, bool throwIfErrors) { if (string.IsNullOrEmpty(value)) { return(true); } //if (CheckUtils.CheckStringShouldNotBeNullOrEmpty(vUser.ColumnNames.Email, value, errors) == false) // if (throwIfErrors) // throw new BRException(errors); // else // return false; if (fnName == RuleFunctionSEnum.Delete) { return(true); } string colName = vUser.ColumnNames.Email; // format check // we try to create a mail address. If format was incorrect then its an error //http://stackoverflow.com/questions/5342375/c-sharp-regex-email-validation try { System.Net.Mail.MailAddress m = new System.Net.Mail.MailAddress(value); } catch (Exception) { errors.Add(colName, BusinessErrorStrings.User.Email_InvalidEmailAddress); } // check duplicate if (errors.Count == 0) // Perfomance: We check database only if no error is there. { value = value.ToLower(); // we store all emails in lower case CheckUtils.CheckDuplicateValueNotToBeExists(colName, value, idValue, errors, null, fnName == RuleFunctionSEnum.Insert, BusinessErrorStrings.User.Email_Duplicate); } if (errors.Count > 0 && throwIfErrors) { throw new BRException(errors); } return(errors.Count == 0); }
public void UpdatePaykeyInDatabase(string payKey) { BusinessRuleErrorList errors = new BusinessRuleErrorList(); if (CheckUtils.CheckStringShouldNotBeNullOrEmpty(vPayment.ColumnNames.PayKey, payKey, errors) == false) { throw new BRException(errors); } // check if pay key is not duplicated in database FilterExpression filter = new FilterExpression(); filter.AddFilter(new Filter(vPayment.ColumnNames.PaymentStatusID, (int)EntityEnums.PaymentStatusEnum.PendingWithPayKey)); if (CheckUtils.CheckDuplicateValueNotToBeExists(vPayment.ColumnNames.PayKey, payKey, null, errors, null, true, null) == false) { throw new BRException(errors[0].ErrorDescription); } }
protected virtual void CheckAutomatedRules(object entitySet, RuleFunctionSEnum fnName, BusinessRuleErrorList errors) { foreach (var colInfo in this.Entity.EntityColumns) { ColumnInfo col = colInfo.Value; if ( (col.IsUpdatable && fnName == RuleFunctionSEnum.Update) || (col.IsInsertable && fnName == RuleFunctionSEnum.Insert) ) { object val = FWUtils.EntityUtils.GetObjectFieldValue(entitySet, col.Name); // check allow blank for all types if (col.ValidationIsNullable == false) { if (val == null) { errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.NotNull, col.Caption)); continue; } // TODO: fix foreign key integer value checking for int //// for integer values if the column is id column, we check not to enter -1 value (that is the default for not having a value) //if (col.Name.EndsWith("ID") && (val is Int16 || val is Int32 || val is Int64)) // if (Convert.ToInt64(val) == -1) // { // errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.NotNull, col.Caption)); // continue; // } } // checking string length if (col.DataTypeDotNet == typeof(string)) { if (col.ValidationIsNullable == false && ((string)val) == "") { errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.StringNotNullOrEmpty, col.Caption)); } CheckUtils.CheckStringLenBetweenMinAndMax(col.Name, (string)val, errors, col.ValidationMinLength, col.ValidationMaxLength); } // checking minimum maximum value if existed if (col.ValidationMaxValue != null || col.ValidationMinValue != null) { if (col.DataTypeDotNet == typeof(int)) { CheckUtils.CheckIntBetweenMinMax(col.Name, (int)val, errors, (int?)col.ValidationMinValue, (int?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(long)) { CheckUtils.CheckLongBetweenMinMax(col.Name, (long)val, errors, (long?)col.ValidationMinValue, (long?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(double)) { CheckUtils.CheckDoubleBetweenMinMax(col.Name, (double)val, errors, (double?)col.ValidationMinValue, (double?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(float)) { CheckUtils.CheckFloatBetweenMinMax(col.Name, (float)val, errors, (float?)col.ValidationMinValue, (float?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(decimal)) { CheckUtils.CheckDecimalBetweenMinMax(col.Name, (decimal)val, errors, (decimal?)col.ValidationMinValue, (decimal?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(DateTime)) { CheckUtils.CheckDateTimeBetweenMinMax(col.Name, (DateTime)val, errors, (DateTime?)col.ValidationMinValue, (DateTime?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(short)) { CheckUtils.CheckShortBetweenMinMax(col.Name, (short)val, errors, (short?)col.ValidationMinValue, (short?)col.ValidationMaxValue); } else if (col.DataTypeDotNet == typeof(byte)) { CheckUtils.CheckByteBetweenMinMax(col.Name, (byte)val, errors, (byte?)col.ValidationMinValue, (byte?)col.ValidationMaxValue); } } // duplicate check if (errors.Count == 0 && col.ValidationNoDuplicate) // Perfomance: We check database only if no error is there. { object idValue = ((EntityObjectBase)entitySet).GetPrimaryKeyValue(); CheckUtils.CheckDuplicateValueNotToBeExists(col.Name, val, idValue, errors, null, fnName == RuleFunctionSEnum.Insert, null); } } // end of if column should be validated or not } // end foreach column }