Example #1
0
        public IEnumerable <string> GetValidationErrors(TRecord record, SaveAction action = SaveAction.NotSet, IDbConnection connection = null)
        {
            foreach (var prop in record.GetType().GetProperties().Where(pi => pi.HasSaveAction(action)))
            {
                if (RequiredDateNotSet(prop, record))
                {
                    yield return($"The {prop.Name} date field requires a value.");
                }

                var postulateAttr = prop.GetCustomAttributes <Validation.ValidationAttribute>();
                foreach (var attr in postulateAttr)
                {
                    object value = prop.GetValue(record);
                    if (!attr.IsValid(prop, value, connection))
                    {
                        yield return(attr.ErrorMessage);
                    }
                }

                var validationAttr = prop.GetCustomAttributes <System.ComponentModel.DataAnnotations.ValidationAttribute>();
                foreach (var attr in validationAttr)
                {
                    if (!IsRequiredWithInsertExpression(prop, attr))
                    {
                        object value = prop.GetValue(record);
                        if (!attr.IsValid(value))
                        {
                            yield return(attr.FormatErrorMessage(prop.Name));
                        }
                    }
                }
            }

            IValidateable validateable = record as IValidateable;

            if (validateable != null)
            {
                var errors = validateable.Validate(connection);
                foreach (var err in errors)
                {
                    yield return(err);
                }
            }
        }