示例#1
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override async Task ValidateAsync(PropertyContext <TEntity, TProperty> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (_predicate != null)
            {
                if (!_predicate.Invoke(context.Parent.Value))
                {
                    context.CreateErrorMessage(ErrorText ?? ValidatorStrings.ImmutableFormat);
                }
            }
            else if (_immutable != null)
            {
                if (!_immutable.Invoke())
                {
                    context.CreateErrorMessage(ErrorText ?? ValidatorStrings.ImmutableFormat);
                }
            }
            else
            {
                if (!(await _immutableAsync !.Invoke().ConfigureAwait(false)))
                {
                    context.CreateErrorMessage(ErrorText ?? ValidatorStrings.ImmutableFormat);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, string> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (string.IsNullOrEmpty(context.Value))
            {
                return;
            }

            if (context.Value.Length < MinLength)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MinLengthFormat, MinLength);
                return;
            }

            if (MaxLength.HasValue && context.Value.Length > MaxLength.Value)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MaxLengthFormat, MaxLength);
                return;
            }

            if (Regex != null && !Regex.IsMatch(context.Value))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.RegexFormat);
                return;
            }
        }
示例#3
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Where the value is null, do nothing; i.e. Nullable<Type>.
            if (Comparer <object> .Default.Compare(context.Value, null) == 0)
            {
                return;
            }

            // Convert numeric to a decimal value.
            decimal value = Convert.ToDecimal(context.Value);

            // Check if negative.
            if (!AllowNegatives && value < 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.AllowNegativesFormat);
                return;
            }

            int il = MaxDigits.HasValue ? CalcIntegerPartLength(value) : 0;
            int dp = MaxDigits.HasValue || DecimalPlaces.HasValue ? CalcFractionalPartLength(value) : 0;

            // Check max digits.
            if (MaxDigits.HasValue && !CheckMaxDigits(MaxDigits.Value, DecimalPlaces, il, dp))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MaxDigitsFormat, MaxDigits);
                return;
            }

            // Check decimal places.
            if (DecimalPlaces.HasValue && !CheckDecimalPlaces(DecimalPlaces.Value, dp))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.DecimalPlacesFormat, DecimalPlaces);
                return;
            }
        }
示例#4
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Compare the value against its default.
            if (Comparer <TProperty> .Default.Compare(context.Value, default(TProperty)) == 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MandatoryFormat);
                return;
            }

            // Also check for empty strings.
            if (context.Value is string val && val.Length == 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MandatoryFormat);
            }
        }
示例#5
0
 /// <summary>
 /// Validate the property value.
 /// </summary>
 /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
 public override void Validate(PropertyContext <TEntity, TProperty> context)
 {
     if (_predicate != null)
     {
         if (!_predicate.Invoke(context.Parent.Value))
         {
             context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MustFormat);
         }
     }
     else
     {
         if (!_must.Invoke())
         {
             context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MustFormat);
         }
     }
 }
示例#6
0
 /// <summary>
 /// Validate the property value.
 /// </summary>
 /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
 public override void Validate(PropertyContext <TEntity, TProperty> context)
 {
     Beef.Check.NotNull(context, nameof(context));
     if (_predicate != null)
     {
         if (_predicate(context.Parent.Value))
         {
             context.CreateErrorMessage(ErrorText ?? ValidatorStrings.DuplicateFormat);
         }
     }
     else
     {
         if (_duplicate !())
         {
             context.CreateErrorMessage(ErrorText ?? ValidatorStrings.DuplicateFormat);
         }
     }
 }
示例#7
0
            protected override Task OnValidateAsync(PropertyContext <ValidationValue <int>, int> context)
            {
                if (context.Value == 11)
                {
                    context.CreateErrorMessage("{0} is not allowed to be eleven.");
                }

                return(Task.CompletedTask);
            }
示例#8
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, TProperty> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            // Compare the value against its default.
            if (Comparer <TProperty> .Default.Compare(context.Value, default !) == 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MandatoryFormat);
                return(Task.CompletedTask);
            }

            // Also check for empty strings.
            if (context.Value is string val && val.Length == 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MandatoryFormat);
            }

            return(Task.CompletedTask);
        }
示例#9
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, TProperty?> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (context.Value == null)
            {
                return(Task.CompletedTask);
            }

            if (context.Value.ContainsInvalidItems())
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.InvalidFormat);
                return(Task.CompletedTask);
            }

            // Check Min and Max counts.
            if (context.Value.Count < MinCount)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MinCountFormat, MinCount);
            }
            else if (MaxCount.HasValue && context.Value.Count > MaxCount.Value)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MaxCountFormat, MaxCount);
            }

            // Check duplicates.
            var dict = new KeyOnlyDictionary <string?>();

            foreach (var item in context.Value.ToRefDataList().Where(x => x.IsValid))
            {
                if (dict.ContainsKey(item.Code))
                {
                    context.CreateErrorMessage(ErrorText ?? ValidatorStrings.DuplicateValueFormat, context.Text, item.ToString());
                    return(Task.CompletedTask);
                }

                dict.Add(item.Code);
            }

            return(Task.CompletedTask);
        }
示例#10
0
        /// <summary>
        /// Creates the error message passing the <paramref name="compareToText"/> text as the third format parameter (i.e. String.Format("{2}")).
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        /// <param name="compareToText">The compare text <see cref="LText"/> to be passed for the error message.</param>
        protected void CreateErrorMessage(PropertyContext <TEntity, TProperty> context, LText compareToText)
        {
            switch (Operator)
            {
            case CompareOperator.Equal:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareEqualFormat, (string)compareToText ?? null);
                break;

            case CompareOperator.NotEqual:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareNotEqualFormat, (string)compareToText ?? null);
                break;

            case CompareOperator.LessThan:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareLessThanFormat, (string)compareToText ?? null);
                break;

            case CompareOperator.LessThanEqual:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareLessThanEqualFormat, (string)compareToText ?? null);
                break;

            case CompareOperator.GreaterThan:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareGreaterThanFormat, (string)compareToText ?? null);
                break;

            case CompareOperator.GreaterThanEqual:
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.CompareGreaterThanEqualFormat, (string)compareToText ?? null);
                break;
            }
        }
示例#11
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            if (context.Value == null)
            {
                return;
            }

            if (!context.Value.IsValid)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.InvalidFormat);
                return;
            }
        }
示例#12
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, TProperty> context)
        {
            // Where the value is null, do nothing; i.e. Nullable<Type>.
            Beef.Check.NotNull(context, nameof(context));
            if (Comparer <object> .Default.Compare(context.Value !, null !) == 0)
            {
                return(Task.CompletedTask);
            }

            // Convert numeric to a decimal value.
            decimal value = Convert.ToDecimal(context.Value, System.Globalization.CultureInfo.CurrentCulture);

            // Check if negative.
            if (!AllowNegatives && value < 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.AllowNegativesFormat);
                return(Task.CompletedTask);
            }

            int il = MaxDigits.HasValue ? DecimalRuleHelper.CalcIntegerPartLength(value) : 0;
            int dp = MaxDigits.HasValue || DecimalPlaces.HasValue ? DecimalRuleHelper.CalcFractionalPartLength(value) : 0;

            // Check max digits.
            if (MaxDigits.HasValue && !DecimalRuleHelper.CheckMaxDigits(MaxDigits.Value, DecimalPlaces, il, dp))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.MaxDigitsFormat, MaxDigits);
                return(Task.CompletedTask);
            }

            // Check decimal places.
            if (DecimalPlaces.HasValue && !DecimalRuleHelper.CheckDecimalPlaces(DecimalPlaces.Value, dp))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.DecimalPlacesFormat, DecimalPlaces);
            }

            return(Task.CompletedTask);
        }
示例#13
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, string> context)
        {
            if (string.IsNullOrEmpty(context.Value))
            {
                return;
            }

            var wildcard = Wildcard ?? Wildcard.Default ?? Wildcard.MultiAll;

            if (wildcard != null && !wildcard.Validate(context.Value))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.WildcardFormat);
                return;
            }
        }
示例#14
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, TProperty> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (context.Value == null)
            {
                return(Task.CompletedTask);
            }

            if (!context.Value.IsValid)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.InvalidFormat);
            }

            return(Task.CompletedTask);
        }
示例#15
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Where allowing negatives or the value is null, do nothing; i.e. Nullable<Type>.
            if (AllowNegatives || Comparer <object> .Default.Compare(context.Value, null) == 0)
            {
                return;
            }

            // Convert numeric to a double value.
            double value = Convert.ToDouble(context.Value);

            // Determine if the value is negative and is/isn't allowed.
            if (!AllowNegatives && value < 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.AllowNegativesFormat);
            }
        }
示例#16
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, string?> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (string.IsNullOrEmpty(context.Value))
            {
                return(Task.CompletedTask);
            }

            var rd = ReferenceDataManager.Current[typeof(TRefData)].GetByCode(context.Value);

            if (rd == null || !rd.IsValid)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.InvalidFormat);
            }

            return(Task.CompletedTask);
        }
示例#17
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, string?> context)
        {
            Beef.Check.NotNull(context, nameof(context));

            if (string.IsNullOrEmpty(context.Value))
            {
                return(Task.CompletedTask);
            }

            var wildcard = Wildcard ?? Wildcard.Default ?? Wildcard.MultiAll;

            if (wildcard != null && !wildcard.Validate(context.Value))
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.WildcardFormat);
            }

            return(Task.CompletedTask);
        }
示例#18
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Where allowing negatives or the value is null, do nothing; i.e. Nullable<Type>.
            Beef.Check.NotNull(context, nameof(context));

            if (AllowNegatives || Comparer <object> .Default.Compare(context.Value !, null !) == 0)
            {
                return;
            }

            // Convert numeric to a double value.
            double value = Convert.ToDouble(context.Value, System.Globalization.CultureInfo.InvariantCulture);

            // Determine if the value is negative and is/isn't allowed.
            if (!AllowNegatives && value < 0)
            {
                context.CreateErrorMessage(ErrorText ?? ValidatorStrings.AllowNegativesFormat);
            }
        }
示例#19
0
文件: ExistsRule.cs 项目: ostat/Beef
 /// <summary>
 /// Create the error message.
 /// </summary>
 private void CreateErrorMessage(PropertyContext <TEntity, TProperty> context)
 {
     context.CreateErrorMessage(ErrorText ?? ValidatorStrings.ExistsFormat);
 }