/// <summary>
        /// Performs a Equals validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <typeparam name="TProperty">Property to be compared, must be a Struct and Implements IComparable<T>, also needs to implement IFormattable (this will discard bool types)</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="value">Value to compare</param>
        /// <param name="errorCode">ErrorCode if number is NOT equal to value</param>
        /// <param name="errorDescription">Error description if number is NOT equal to value</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, TProperty> Equals <T, TProperty>(this ValidationContract <T, TProperty> contract, TProperty value, string errorCode = null, string errorDescription = null) where TProperty : struct, IComparable <TProperty>, IFormattable
        {
            errorCode        = errorCode ?? string.Format(contract.Options.Messages.EqualsErrorMessage.Key, contract.MemberName, value);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.EqualsErrorMessage.Value, contract.MemberName, value);

            if (contract.MemberValue.CompareTo(value) != 0)
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, TProperty>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
        /// <summary>
        /// Performs a Between validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <typeparam name="TProperty">Property to be compared, must be a Struct and Implements IComparable<T>, also needs to implement IFormattable (this will discard bool types)</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="initial">Initial range value to compare</param>
        /// <param name="final">Final range value to compare</param>
        /// <param name="errorCode">ErrorCode if number is not between the range</param>
        /// <param name="errorDescription">Error description if between the range</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, TProperty> Between <T, TProperty>(this ValidationContract <T, TProperty> contract, TProperty initial, TProperty final, string errorCode = null, string errorDescription = null) where TProperty : struct, IComparable <TProperty>, IFormattable
        {
            errorCode        = errorCode ?? string.Format(contract.Options.Messages.BetweenErrorMessage.Key, contract.MemberName, initial);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.BetweenErrorMessage.Value, contract.MemberName, initial, final);

            if (contract.MemberValue.CompareTo(initial) <= 0 || contract.MemberValue.CompareTo(final) >= 0)
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, TProperty>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
Example #3
0
        /// <summary>
        /// Performs a Empty validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="errorCode">ErrorCode if Guid is NOT equal to value</param>
        /// <param name="errorDescription">Error description if Guid is NOT equal to value</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, Guid> Empty <T>(this ValidationContract <T, Guid> contract, string errorCode = null, string errorDescription = null)
        {
            errorCode        = errorCode ?? string.Format(contract.Options.Messages.GuidEmptyErrorMessage.Key, contract.MemberName);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.GuidEmptyErrorMessage.Value, contract.MemberName);

            if (contract.MemberValue != Guid.Empty)
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, Guid>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
Example #4
0
        /// <summary>
        /// Performs a NotEquals validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="errorCode">ErrorCode if Guid is NOT equal to value</param>
        /// <param name="errorDescription">Error description if Guid is NOT equal to value</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, Guid> NotEqual <T>(this ValidationContract <T, Guid> contract, Guid comparer, string errorCode = null, string errorDescription = null)
        {
            errorCode        = errorCode ?? string.Format(contract.Options.Messages.GuidNotEqualErrorMessage.Key, contract.MemberValue, comparer);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.GuidNotEqualErrorMessage.Value, contract.MemberName, comparer);

            if (contract.MemberValue == comparer)
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, Guid>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
        /// <summary>
        /// Performs a Exactly Length Validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="minLength">Exactly required length</param>
        /// <param name="errorCode">ErrorCode if Length is not safistied</param>
        /// <param name="errorDescription">ErrorDescription if Length is not safistied</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, string> ExactlyLength <T>(this ValidationContract <T, string> contract, int length, string errorCode = null, string errorDescription = null)
        {
            string value = ConvertFromNull(contract.MemberValue);

            errorCode        = errorCode ?? string.Format(contract.Options.Messages.ExactlyLengthErrorMessage.Key, contract.MemberName);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.ExactlyLengthErrorMessage.Value, contract.MemberName, length);

            if (value.Length != length)
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, string>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
        /// <summary>
        /// Performs a empty validation, validation will fail if string IS empty
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="errorCode">ErrorCode if string is empty</param>
        /// <param name="errorDescription">Error description if string is empty</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, string> NotEmpty <T>(this ValidationContract <T, string> contract, string errorCode = null, string errorDescription = null)
        {
            string value = ConvertFromNull(contract.MemberValue);

            errorCode        = errorCode ?? string.Format(contract.Options.Messages.NotEmptyErrorMessage.Key, contract.MemberName);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.NotEmptyErrorMessage.Value, contract.MemberName);

            if (string.IsNullOrWhiteSpace(value))
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, string>(contract.MemberName, contract.MemberValue, contract.Errors));
        }
        /// <summary>
        /// Performs a Email Validation
        /// </summary>
        /// <typeparam name="T">T type</typeparam>
        /// <param name="contract">Current contract</param>
        /// <param name="errorCode">ErrorCode if Regex is not safistied</param>
        /// <param name="errorDescription">Error description if Regex is not satisfied</param>
        /// <returns>A new validation contract with this validation embedded</returns>
        public static ValidationContract <T, string> Email <T>(this ValidationContract <T, string> contract, string errorCode = null, string errorDescription = null)
        {
            string value = ConvertFromNull(contract.MemberValue);

            errorCode        = errorCode ?? string.Format(contract.Options.Messages.EmailErrorMessage.Key, contract.MemberName);
            errorDescription = errorDescription ?? string.Format(contract.Options.Messages.EmailErrorMessage.Value, contract.MemberName);

            Regex regex = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);

            if (!regex.IsMatch(contract.MemberValue))
            {
                contract.AddError(errorCode, errorDescription);
            }

            return(new ValidationContract <T, string>(contract.MemberName, contract.MemberValue, contract.Errors));
        }