/// <summary> /// Validate a player instance /// </summary> /// <param name="controller">referring controller</param> /// <param name="player">player</param> /// <param name="result">out'ted result, may be null</param> /// <param name="options">any config options</param> /// <returns>bool indicating if player is valid</returns> public bool TryValidatePlayer(ControllerBase controller, ref Player player, out ActionResult result, ValidationOptions options = ValidationOptions.None) { if (options.HasFlag(ValidationOptions.IdIsEmpty)) { player.Id = 0; //enforce Id is empty } result = null; //validate if (string.IsNullOrWhiteSpace(player.FirstName)) { result = controller.UnprocessableEntity("Player first name cannot be empty or null"); return(false); } if (string.IsNullOrWhiteSpace(player.LastName)) { result = controller.UnprocessableEntity("Player last name cannot be empty or null"); return(false); } return(true); }
/// <summary> /// Validate a team instance /// </summary> /// <param name="controller">referring controller</param> /// <param name="team">team</param> /// <param name="result">out'ted result, may be null</param> /// <param name="options">any config options</param> /// <returns>bool indicating if team is valid</returns> public bool TryValidateTeam(ControllerBase controller, ref Team team, out ActionResult result, ValidationOptions options = ValidationOptions.None) { if (options.HasFlag(ValidationOptions.IdIsEmpty)) { team.Id = 0; //enforce Id is empty } result = null; //validate if (string.IsNullOrWhiteSpace(team.Name)) { result = controller.UnprocessableEntity("Location cannot be empty or null"); return(false); } if (string.IsNullOrWhiteSpace(team.Location)) { result = controller.UnprocessableEntity("Name cannot be empty or null"); return(false); } return(true); }
private static bool _TryValidateValueBase(string type, string subject, string objName, string val, List <string> invalidStartStrings, ValidationOptions options, out string msg) { #region "Analysis of System.Configuration Source:" /* * From http://www.w3.org/Addressing/ * * reserved = ';' | '/' | '?' | ':' | '@' | '&' | '=' | '+' | '$' | ',' * * From Platform SDK * * reserved = '\' | '/' | '|' | ':' | '"' | '<' | '>' * */ /* * Analysis of System.Configuration Source: * * NOTE: Element/section names themselves have no known reserved word restrictions. Restrictions are only present * in property name definitions. However, I will still present my findings below. We will need to revisit * the System.Configuration source to confirm. * * For section and element names, these are not allowed. * "configProtectedData", // Implicit is not allowed, and that is an "implicit" name. * "location", // Implicit is not allowed, and can only start with "location" for implicit names. * "config"}, // Implicit is not allowed, and can only start with "config" for implicit names. * * For property names, these are not allowed. * "lock", * "config"}, // Implicit is not allowed, and can only start with "config" for implicit names. * */ /* SECTION NAME CHECKS * * if (String.IsNullOrEmpty(name)) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_invalid), errorInfo); * } * * // must be a valid name in xml, so that it can be used as an element * // n.b. - it also excludes forward slash '/' * try { * XmlConvert.VerifyName(name); * } * // Do not let the exception propagate as an XML exception, * // for we want errors in the section name to be treated as local errors, * // not global ones. * catch (Exception e) { * throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_tag_name_invalid), e, errorInfo); * } * * // Checks if == "configProtectedData" * if (IsImplicitSection(name)) { * if (allowImplicit) { * // avoid test below for strings starting with "config" * return; * } * else { * throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_declare_or_remove_implicit_section, name), errorInfo); * } * } * * if (StringUtil.StartsWith(name, "config")) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_cannot_begin_with_config), errorInfo); * } * // Checks if == "location" * if (name == KEYWORD_LOCATION) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_cannot_be_location), errorInfo); * } * */ #endregion msg = ""; string messageBuilder = ""; if (options.HasFlag(ValidationOptions.IsXml)) { subject = "'Xml " + subject + "'"; } messageBuilder = "The " + type + " " + subject; // Fix redundant messaging. if (objName != val) { messageBuilder += " for " + objName + " '" + val + "'"; } else { messageBuilder += " '" + val + "'"; } //EXAMPLE: context.LogError("The Xml Section Name is required and cannot be an empty string.", "RequiredProperty", this); /** * Required Check. * */ if (options.HasFlag(ValidationOptions.IsRequired) && string.IsNullOrEmpty(val)) { msg = messageBuilder + " cannot be empty."; return(false); } /** * Reserved Word Check. * */ // + Cannot be named "configProtectedData", "location", "config" (Implicit is not allowed, and that is an "implicit" name). bool hasInvalidWord = false; if (invalidStartStrings != null && invalidStartStrings.Count > 0) { string adj = ""; string reservedWord = ""; foreach (string inv in invalidStartStrings) { if (inv.Substring(0, 1) == "^") { adj = "start with"; reservedWord = inv.Substring(1); if (val.ToLower().StartsWith(reservedWord.ToLower())) { hasInvalidWord = true; } } else { adj = "equal"; if (val.ToLower() == inv.ToLower()) { hasInvalidWord = true; reservedWord = inv.ToLower(); } } if (hasInvalidWord) { break; } } if (hasInvalidWord) { msg = messageBuilder + " cannot " + adj + " reserved word '" + reservedWord + "'"; // String.Join(", ", invalidStartStrings); return(false); } } /** * Invalid chars Check. * */ if (!string.IsNullOrEmpty(val)) { // We have a special validation method in System.Xml to handle XML names. if (!options.HasFlag(ValidationOptions.IsNamespace) && options.HasFlag(ValidationOptions.IsXml)) { try { XmlConvert.VerifyName(val); } catch (Exception e) { msg = messageBuilder + " must be a valid XML name: " + e.Message; return(false); } } else { string regexStrStart = "^[^0-9\\.]["; string regexStrAllowedChar = "a-zA-Z0-9_"; string regexStrEnd = "]+[\\`]?$"; string regexFriendly = "alphanumeric, start with a letter, cannot have special characters or spaces"; if (options.HasFlag(ValidationOptions.IsNamespace)) { // Allow dots. regexStrAllowedChar += "\\."; //if (((options | ValidationOptions.IsXml) == ValidationOptions.IsXml)) if (options.HasFlag(ValidationOptions.IsXml)) { // Allow URN: for XML namespace. regexStrAllowedChar += "\\:"; regexFriendly += ", must be valid XML namespace"; } else { regexFriendly += ", must be valid namespace"; } } Regex re = new Regex(regexStrStart + regexStrAllowedChar + regexStrEnd); if (!re.IsMatch(val)) { msg = messageBuilder + " must be " + regexFriendly + "."; return(false); } } } return(true); }
private static bool _TryValidateValueBase(string type, string subject, string objName, string val, List <string> invalidStartStrings, ValidationOptions options, out string msg) { #region "Analysis of System.Configuration Source:" /* * From http://www.w3.org/Addressing/ * * reserved = ';' | '/' | '?' | ':' | '@' | '&' | '=' | '+' | '$' | ',' * * From Platform SDK * * reserved = '\' | '/' | '|' | ':' | '"' | '<' | '>' * */ /* * Analysis of System.Configuration Source: * * NOTE: Element/section names themselves have no known reserved word restrictions. Restrictions are only present * in property name definitions. However, I will still present my findings below. We will need to revisit * the System.Configuration source to confirm. * * For section and element names, these are not allowed. * "configProtectedData", // Implicit is not allowed, and that is an "implicit" name. * "location", // Implicit is not allowed, and can only start with "location" for implicit names. * "config"}, // Implicit is not allowed, and can only start with "config" for implicit names. * * For property names, these are not allowed. * "lock", * "config"}, // Implicit is not allowed, and can only start with "config" for implicit names. * */ /* SECTION NAME CHECKS * * if (String.IsNullOrEmpty(name)) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_invalid), errorInfo); * } * * // must be a valid name in xml, so that it can be used as an element * // n.b. - it also excludes forward slash '/' * try { * XmlConvert.VerifyName(name); * } * // Do not let the exception propagate as an XML exception, * // for we want errors in the section name to be treated as local errors, * // not global ones. * catch (Exception e) { * throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_tag_name_invalid), e, errorInfo); * } * * // Checks if == "configProtectedData" * if (IsImplicitSection(name)) { * if (allowImplicit) { * // avoid test below for strings starting with "config" * return; * } * else { * throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_declare_or_remove_implicit_section, name), errorInfo); * } * } * * if (StringUtil.StartsWith(name, "config")) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_cannot_begin_with_config), errorInfo); * } * // Checks if == "location" * if (name == KEYWORD_LOCATION) { * throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_cannot_be_location), errorInfo); * } * */ #endregion msg = ""; string messageBuilder = ""; if (options.HasFlag(ValidationOptions.IsXml)) { subject = "'Xml " + subject + "'"; } messageBuilder = "The " + type + " " + subject; // Fix redundant messaging. if (objName != val) { messageBuilder += " for " + objName + " '" + val + "'"; } else { messageBuilder += " '" + val + "'"; } //EXAMPLE: context.LogError("The Xml Section Name is required and cannot be an empty string.", "RequiredProperty", this); /** * Required Check. * */ if (options.HasFlag(ValidationOptions.IsRequired) && string.IsNullOrEmpty(val)) { msg = messageBuilder + " cannot be empty."; return(false); } string errMsgPartial = ""; /** * Reserved Word Check. * */ // + Cannot be named "configProtectedData", "location", "config" (Implicit is not allowed, and that is an "implicit" name). if (!_ValidateForReservedWords(val, invalidStartStrings, out errMsgPartial)) { msg = messageBuilder + errMsgPartial; return(false); } /** * Invalid chars Check. **/ if (!string.IsNullOrEmpty(val)) { if (options.HasFlag(ValidationOptions.IsXml)) { // XML if (options.HasFlag(ValidationOptions.IsNamespace)) { if (!_ValidateXmlNamespace(val, out errMsgPartial)) { msg = messageBuilder + errMsgPartial; return(false); } } else { if (!_ValidateXmlName(val, out errMsgPartial)) { msg = messageBuilder + errMsgPartial; return(false); } } } else { // C# if (options.HasFlag(ValidationOptions.IsNamespace)) { if (!_ValidateCLRNamespace(val, out errMsgPartial)) { msg = messageBuilder + errMsgPartial; return(false); } } else { if (!_ValidateCLRName(val, out errMsgPartial)) { msg = messageBuilder + errMsgPartial; return(false); } } } } return(true); }