Beispiel #1
0
        private static void ValidateAgainstType(
            object model,
            IServiceProvider serviceProvider,
            IDictionary <object, object> items,
            ModelErrorDictionary modelErrors,
            PropertyInfo pi,
            IValidateAgainstType attr)
        {
            var propertyModelValue = pi.GetValue(model);
            var propertyModel      = propertyModelValue as IDictionary <string, object>;

            if (propertyModel == null)
            {
                var propertyEnumerableModel = propertyModelValue as IEnumerable <object>;
                if (propertyEnumerableModel == null)
                {
                    // Everything is fine. RequiredAttribute should handle validation here if not.
                    return;
                }
                else
                {
                    ValidateEnumerableElements(model, serviceProvider, items, modelErrors, pi);
                    return;
                }
            }

            #region Instantiate an object of the target type so we can validate it

            var propertyModelPropertiesByName = propertyModel.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value,
                attr.IgnoreCase ? StringComparer.InvariantCultureIgnoreCase : StringComparer.InvariantCulture);

            var retypedPropertyModel = Activator.CreateInstance(attr.Type);
            attr.Type.GetProperties().ForEach(retypedProperty =>
            {
                if (propertyModelPropertiesByName.TryGetValue(retypedProperty.Name, out object value))
                {
                    retypedProperty.SetValue(retypedPropertyModel, value);
                }
            });

            #endregion

            var retypedValidationResult = IsValid(retypedPropertyModel, serviceProvider, items);
            modelErrors.Add(retypedValidationResult.modelErrors, pi.Name);
        }
Beispiel #2
0
        private static void ValidateEnumerableElements(
            object model,
            IServiceProvider serviceProvider,
            IDictionary <object, object> items,
            ModelErrorDictionary modelErrors,
            PropertyInfo pi)
        {
            var enumerableModelErrors = new ModelErrorDictionary();
            var enumerableValue       = (IEnumerable)pi.GetValue(model);

            enumerableValue.Cast <object>().ForEach((elementModel, index) =>
            {
                var elementValidationResult = IsValid(elementModel, serviceProvider, items);
                enumerableModelErrors.Add(elementValidationResult.modelErrors, index);
            });

            modelErrors.Add(enumerableModelErrors, pi.Name);
        }
Beispiel #3
0
 public static void AddOutOfRange(this ModelErrorDictionary modelErrorDictionary, string memberName)
 => modelErrorDictionary.Add($"{memberName} was out of range of the allowed values", memberName);
Beispiel #4
0
 public static void AddTypeExpected(this ModelErrorDictionary modelErrorDictionary, string memberName, string typeDescriptor)
 => modelErrorDictionary.Add($"Expected {memberName} to be of type ${typeDescriptor}", memberName);
Beispiel #5
0
 public static void AddRequired(this ModelErrorDictionary modelErrorDictionary, string memberName)
 => modelErrorDictionary.Add($"{memberName} is required", memberName);