Пример #1
0
 public static ValidationErrors If(
     this ValidationErrors source,
     bool predicate,
     ValidationError validationError)
 {
     return(predicate ?
            source.With(validationError) :
            source);
 }
Пример #2
0
        public static ValidationErrors Require <TValue>(
            this ValidationErrors source,
            TValue value,
            string name)
        {
            var valueType = typeof(TValue);

            var enumerableInterface = valueType
                                      .GetInterfaces()
                                      .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IReadOnlyCollection <>));

            var missing = false;

            if (value is string str)
            {
                missing = string.IsNullOrWhiteSpace(str);
            }
            else if (valueType == typeof(Guid?))
            {
                var g = (Guid?)(object)value;

                missing = !g.HasValue || g.Value == Guid.Empty;
            }
            else if (value == null)
            {
                missing = true;
            }
            else if (enumerableInterface != null)
            {
                missing = (int)valueType.GetProperty(nameof(IReadOnlyCollection <object> .Count)).GetValue(value) == 0;
            }
            else
            {
                missing = value.IsDefaultValue();
            }

            return(missing ?
                   source.With(ValidationError.Required(name)) :
                   source);
        }