예제 #1
0
        /// <summary>
        /// Expands the JSON property names by adding intermediary values within a specified hierarchical name.
        /// </summary>
        private static string[] Expand(IEnumerable <string>?names)
        {
            var sb  = new StringBuilder();
            var kod = new KeyOnlyDictionary <string>();

            if (names != null)
            {
                foreach (var f in names)
                {
                    sb.Clear();
                    var parts = f.Split('.');
                    for (int i = 0; i < parts.Length; i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(".");
                        }

                        sb.Append(parts[i]);
                        kod.Add(sb.ToString());
                    }
                }
            }

            return(kod.Keys.ToArray());
        }
예제 #2
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);
        }