Ejemplo n.º 1
0
        /// <summary>
        /// Validate an IValidateable object.
        /// </summary>
        /// <param name="entity">Object to validate</param>
        /// <param name="action">Action to execute: if not specified, use Any, this is normally used for custom validation routines</param>
        /// <param name="requirePopulatedEHValues">If this is set to true it will require that the entire object graphs, including traversing through EntityHeader<T>.Value will be required if the parameter is required.</param>
        /// <returns></returns>
        public static ValidationResult Validate(IValidateable entity, Actions action = Actions.Any, bool requirePopulatedEHValues = false)
        {
            var result = new ValidationResult();

            if (entity == null)
            {
                if (SLWIOC.Contains(typeof(ILogger)))
                {
                    var logger = SLWIOC.Get <ILogger>();
                    logger.AddException("Validator_Validate", new Exception("NULL Value Passed to Validate Method"));
                }

                result.AddSystemError("Null Value Provided for model on upload.");
                return(result);
            }

            ValidateAuditInfo(result, entity);
            ValidateId(result, entity);

            var properties = entity.GetType().GetTypeInfo().GetAllProperties();

            foreach (var prop in properties)
            {
                var attr = prop.GetCustomAttributes(typeof(FormFieldAttribute), true).OfType <FormFieldAttribute>().FirstOrDefault();
                if (attr != null)
                {
                    ValidateProperty(attr, result, entity, prop, action);
                }
            }

            var methods = entity.GetType().GetTypeInfo().DeclaredMethods;

            foreach (var method in methods)
            {
                var attr = method.GetCustomAttributes(typeof(CustomValidatorAttribute), true).OfType <CustomValidatorAttribute>().FirstOrDefault();
                if (attr != null)
                {
                    CallCustomValidationRoutine(attr, result, entity, method, action);
                }
            }

            foreach (var prop in properties)
            {
                var attr      = prop.GetCustomAttributes(typeof(FormFieldAttribute), true).OfType <FormFieldAttribute>().FirstOrDefault();
                var propValue = prop.GetValue(entity);
                if (propValue != null)
                {
                    if (propValue is IValidateable validateablePropValue)
                    {
                        var childResult = Validator.Validate(validateablePropValue, action);
                        result.Concat(childResult);
                    }

                    if (propValue.GetType().GetTypeInfo().IsGenericType&&
                        propValue.GetType().GetGenericTypeDefinition() == typeof(EntityHeader <>))
                    {
                        var valueProperty = propValue.GetType().GetTypeInfo().GetDeclaredProperty("Value");
                        if (valueProperty.GetValue(propValue) is IValidateable validatableChild)
                        {
                            var childResult = Validator.Validate(validatableChild, action);
                            result.Concat(childResult);
                        }
                        else
                        {
                            if (attr != null && attr.IsRequired && requirePopulatedEHValues)
                            {
                                AddRequiredFieldMissingMessage(result, attr, prop.Name);
                            }
                        }
                    }

                    if (prop.GetValue(entity) is System.Collections.IEnumerable listValues)
                    {
                        foreach (var listValue in listValues)
                        {
                            if (listValue is IValidateable validatableListValue)
                            {
                                var childResult = Validator.Validate(validatableListValue, action);
                                result.Concat(childResult);
                            }
                        }
                    }
                }
            }

            return(result);
        }