/// <summary> /// Verifica se uma string não é vazia /// </summary> /// <param name="validationResult"></param> /// <param name="stringValue"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentNotEmpty(this IValidationResult validationResult, string stringValue, string errorMessage) { if (string.IsNullOrEmpty(stringValue?.Trim())) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="value"/> está entre <see cref="minimum"/> e <see cref="maximum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="value"></param> /// <param name="minimum"></param> /// <param name="maximum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentRange(this IValidationResult validationResult, decimal value, decimal minimum, decimal maximum, string errorMessage) { if (value < minimum || value > maximum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="boolValue"/> é verdadeiro /// </summary> /// <param name="validationResult"></param> /// <param name="boolValue"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertStateTrue(this IValidationResult validationResult, bool boolValue, string errorMessage) { if (!boolValue) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Executa o método <see cref="object.Equals(object)"/> entre o <see cref="object1"/> e <see cref="object2"/> /// </summary> /// <param name="validationResult"></param> /// <param name="object1"></param> /// <param name="object2"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentNotEquals(this IValidationResult validationResult, object object1, object object2, string errorMessage) { if (object1.Equals(object2)) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="object1"/> não é nulo /// </summary> /// <param name="validationResult"></param> /// <param name="object1"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentNotNull(this IValidationResult validationResult, object object1, string errorMessage) { if (object1 == null) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="value"/> é maior que <see cref="minimum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="value"></param> /// <param name="minimum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentIsGreaterThan(this IValidationResult validationResult, decimal value, decimal minimum, string errorMessage) { if (value <= minimum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="value"/> é maior ou igual que <see cref="minimum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="value"></param> /// <param name="minimum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentIsGreaterOrEqualThan(this IValidationResult validationResult, long value, long minimum, string errorMessage) { if (value < minimum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="value"/> é menor que <see cref="minimum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="value"></param> /// <param name="minimum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentIsLessThan(this IValidationResult validationResult, int value, int minimum, string errorMessage) { if (value >= minimum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="value"/> é menor ou igual que <see cref="minimum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="value"></param> /// <param name="minimum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentIsLessOrEqualThan(this IValidationResult validationResult, float value, float minimum, string errorMessage) { if (value > minimum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se <see cref="boolValue"/> é falso /// </summary> /// <param name="validationResult"></param> /// <param name="boolValue"></param> /// <param name="errorMessage">A mensagem de erro à ser retornada em caso de erro.</param> /// <returns></returns> public static IValidationResult AssertArgumentFalse(this IValidationResult validationResult, bool boolValue, string errorMessage) { if (boolValue) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Verifica se o uma string possui tamanho menor que <see cref="maximum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="stringValue"></param> /// <param name="maximum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentLength(this IValidationResult validationResult, string stringValue, int maximum, string errorMessage) { int length = stringValue.Trim().Length; if (length > maximum) { validationResult.Add(errorMessage); } return(validationResult); }
/// <summary> /// Dado que <see cref="pattern"/> é um pattern Regex, verifica se <see cref="stringValue"/> é compatível com este pattern /// </summary> /// <param name="validationResult"></param> /// <param name="pattern"></param> /// <param name="stringValue"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentMatches(this IValidationResult validationResult, string pattern, string stringValue, string errorMessage) { Regex regex = new Regex(pattern); if (!regex.IsMatch(stringValue)) { validationResult.Add(errorMessage); } return(validationResult); }
public static IValidationResult AssertIsEmail(this IValidationResult validationResult, string email, string message) { if ( !Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)) { validationResult.Add(message); } return(validationResult); }
/// <summary> /// 添加错误 /// </summary> /// <param name="validationResult">验证结果</param> /// <param name="modelState">实体状态字典</param> public virtual void AddErrors(IValidationResult validationResult, ModelStateDictionary modelState) { if (modelState.IsValid) { return; } foreach (var state in modelState) { foreach (var error in state.Value.Errors) { validationResult.Add(new ValidationResult(error.ErrorMessage, new[] { state.Key })); } } }
/// <summary> /// Verifica se <see cref="stringValue"/> possui tamanho entre <see cref="minimum"/> e <see cref="maximum"/> /// </summary> /// <param name="validationResult"></param> /// <param name="stringValue"></param> /// <param name="minimum"></param> /// <param name="maximum"></param> /// <param name="errorMessage"></param> /// <returns></returns> public static IValidationResult AssertArgumentLength(this IValidationResult validationResult, string stringValue, int minimum, int maximum, string errorMessage) { if (string.IsNullOrEmpty(stringValue)) { stringValue = string.Empty; } int length = stringValue.Trim().Length; if (length < minimum || length > maximum) { validationResult.Add(errorMessage); } return(validationResult); }