public void GetFormattedErrorMessage_ReturnsFormattedString()
        {
            //Create an Entity
            var emptyContact = new Contact();
            emptyContact.FirstName = null;
            emptyContact.LastName = null;

            //Create PropertyValidator
            var propertyValidator =
                new PropertyValidator<Contact, string>(contact => contact.LastName);

            //Create a rule
            RuleValidator<Contact, string> ruleValidator = new LengthBetween<Contact>(1, 5);

            //Create a context
            var context = new RuleValidatorContext<Contact, string>(emptyContact, propertyValidator, null);

            //create it like this? IOC? Factory?
            //IMessageStore messageStore = new ResourceMessageStore();

            //string errorMessage = messageStore.GetFormattedDefaultMessage(ruleValidator.GetType().Name, context, ruleValidator.Parameters);
            var messageService = new MessageService();
            var errorMessage = messageService.GetDefaultMessageAndFormat(new MessageContext(context, ruleValidator.GetType(), false, null, null), ruleValidator.Parameters);

            Assert.That(errorMessage, Is.Not.Null.Or.Empty);

            Assert.That(errorMessage, Is.StringContaining("Last Name"));
            Assert.That(errorMessage, Is.StringContaining("1"));
            Assert.That(errorMessage, Is.StringContaining("5"));
            //null: Search for Actual value but it's empty b/c the value is null
        }
Ejemplo n.º 2
0
        public RuleValidatorContext<CalendarEvent, System.DateTime> BuildContextForCalendarEventStartDate(DateTime startDate)
        {
            var calendarEvent = new CalendarEvent() { StartDate = startDate };
            var context = new RuleValidatorContext<CalendarEvent, DateTime>(calendarEvent, "StartDate", calendarEvent.StartDate, null, ValidationLevelType.Error, null);

            return context;
        }
Ejemplo n.º 3
0
 protected ValidationResult Evaluate(bool isValid, RuleValidatorContext context)
 {
     if (Negate)
     {
         if (!isValid)
         {
             return null;
         }
         else
         {
             return ValidationResultFactory.Create(this, context, Parameters, MessageKey);
         }
     }
     else
     {
         if (isValid)
         {
             return null;
         }
         else
         {
             return ValidationResultFactory.Create(this, context, Parameters,MessageKey);
         }
     }
 }
Ejemplo n.º 4
0
        public RuleValidatorContext<Contact, bool> BuildContextForContactActive(bool value)
        {
            var contact = new Contact { Active = value };
            var context = new RuleValidatorContext<Contact, bool>(contact, "Active", contact.Active, null, ValidationLevelType.Error, null);

            return context;
        }
Ejemplo n.º 5
0
        public static ValidationResult Create(RuleValidator validator, RuleValidatorContext context, IList<Object> parameterValues, object messageKey, IEnumerable<ValidationResult> nestedValidationResults = null)
        {
            string message = string.Empty;
            var messageService = new MessageService();

            if (String.IsNullOrEmpty(validator.Message))
            {
                var messageContext = new MessageContext(context, validator.GetType(), validator.Negate, validator.MessageStoreName, messageKey, validator.MessageFormatter);
                message = messageService.GetDefaultMessageAndFormat(messageContext, parameterValues);
            }
            else
            {
                //Since the message was supplied, don't get the default message from the store, just format it
                message = messageService.FormatMessage(validator.Message, context, parameterValues, validator.MessageFormatter);
            }

            //Override level if all the nested validation errors are Warnings

            if (nestedValidationResults != null && nestedValidationResults.All(vr => vr.Level == ValidationLevelType.Warn))
            {
                return new ValidationResult(context.PropertyInfo, message, ValidationLevelType.Warn, context.PropertyValue, nestedValidationResults);
            }
            else
            {
                return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults);
            }

            //return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults);
        }
Ejemplo n.º 6
0
        public string FormatMessage(string message, RuleValidatorContext context, object[] parameters, Func<object, string> propertyValueFormatter)
        {
            //Replace known keywords with actual values
            var formattedMessage = message.Replace("{PropertyName}", buildPropertyName(context));

            if (context.PropertyValue == null)
            {
                formattedMessage = formattedMessage.Replace("{PropertyValue}", context.PropertyValue as string);
            }
            else
            {
                string formattedPropertyValue;

                if (propertyValueFormatter == null)
                {
                    formattedPropertyValue = context.PropertyValue.ToString();
                }
                else
                {
                    formattedPropertyValue = propertyValueFormatter(context.PropertyValue);
                }
                formattedMessage = formattedMessage.Replace("{PropertyValue}", formattedPropertyValue);
            }

            //create param list for String.Format
            var errorMessageParams = new List<object>();
            if (parameters != null && parameters.Any())
            {
                errorMessageParams.AddRange(parameters);
            }

            return System.String.Format(formattedMessage, errorMessageParams.ToArray());
        }
Ejemplo n.º 7
0
        private RuleValidatorContext<Contact, string> BuildContextForName(string propertyValue)
        {
            var contact = new Contact { FirstName = propertyValue };
            var context = new RuleValidatorContext<Contact, string>(contact, "FirstName", contact.FirstName, null, null);

            return context;
        }
 public RuleValidatorContext(object instance, string propertyName, object propertyValue, MemberInfo propertyInfo, RuleValidatorContext parentContext)
 {
     Instance = instance;
     PropertyName = propertyName;
     PropertyValue = propertyValue;
     PropertyInfo = propertyInfo;
     Parent = parentContext;
 }
Ejemplo n.º 9
0
 public MessageContext(RuleValidatorContext ruleContext, Type validatorType, bool negate, string messageStoreName, object key)
 {
     RuleContext = ruleContext;
     ValidatorType = validatorType;
     Negate = negate;
     MessageStoreName = messageStoreName;
     Key = key;
 }
Ejemplo n.º 10
0
 public RuleValidatorContext(object instance, string propertyName, object propertyValue, ValidationLevelType level, MemberInfo propertyInfo, RuleValidatorContext parentContext)
 {
     Instance      = instance;
     PropertyName  = propertyName;
     PropertyValue = propertyValue;
     PropertyInfo  = propertyInfo;
     Parent        = parentContext;
     Level         = level;
 }
Ejemplo n.º 11
0
 public MessageContext(RuleValidatorContext ruleContext, Type validatorType, bool negate, string messageStoreName, object key, Func<object, string> propertyValueFormatter)
 {
     RuleContext = ruleContext;
     ValidatorType = validatorType;
     Negate = negate;
     MessageStoreName = messageStoreName;
     Key = key;
     PropertyValueFormatter = propertyValueFormatter;
 }
 public RuleValidatorContext(object instance, PropertyValidator validator, RuleValidatorContext parentContext)
 {
     PropertyName = String.IsNullOrEmpty(validator.PropertyNameOverride)
                       ? validator.PropertyName.SplitPascalCase()
                       : validator.PropertyNameOverride;
     PropertyValue = validator.GetValueForProperty(instance);
     PropertyInfo = validator.PropertyInfo;
     Parent = parentContext;
     Instance = instance;
 }
Ejemplo n.º 13
0
        public RuleValidatorContext<Contact, string> BuildContextForLength(string firstName, string lastName)
        {
            if (string.IsNullOrEmpty(lastName))
            {
                lastName = "Default";
            }

            var contact = new Contact { FirstName = firstName, LastName = lastName };
            var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, ValidationLevelType.Error, null);
            return context;
        }
Ejemplo n.º 14
0
        public void When_Required_And_CollectionValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, IEnumerable>();
            var context = new RuleValidatorContext<Customer, IEnumerable>(customer, "Contacts", customer.Contacts, null, null);

            //Validate the validator only, return true of no error returned
            var result = validator.Validate(context, null);

            Assert.IsNotEmpty(result.Message);
        }
Ejemplo n.º 15
0
        public void When_Required_And_StringValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, string>();
            var context = new RuleValidatorContext<Customer, string>(customer, "Name", customer.Name, null, null);

            //Validate the validator only, return true of no error returned
            var result = validator.Validate(context, null);

            Assert.IsNotEmpty(result.Message);
        }
Ejemplo n.º 16
0
        public void When_Required_And_StringValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, string>();
            var context = new RuleValidatorContext<Customer, string>(customer, "Name", customer.Name, null, ValidationLevelType.Error, null);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            validator.Validate(context, null, notification);

            Assert.IsNotEmpty(notification.Errors[0].Message);
        }
Ejemplo n.º 17
0
        public void When_Required_And_CollectionValue_Is_Empty_IsInvalid()
        {
            var customer = new Customer() {Contacts = new List<Contact>()};

            var validator = new Required<Customer, IEnumerable>();
            var context = new RuleValidatorContext<Customer, IEnumerable>(customer, "Contacts", customer.Contacts, null, ValidationLevelType.Error, null);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            validator.Validate(context, null, notification);

            Assert.IsNotEmpty(notification.Errors[0].Message);
        }
Ejemplo n.º 18
0
        public static ValidationResult Create(RuleValidator validator, RuleValidatorContext context, IList<Object> parameterValues, object messageKey)
        {
            string message = string.Empty;
            var messageService = new MessageService();

            if (String.IsNullOrEmpty(validator.Message))
            {
                var messageContext = new MessageContext(context, validator.GetType(), validator.Negate, validator.MessageStoreName, messageKey, validator.MessageFormatter);
                message = messageService.GetDefaultMessageAndFormat(messageContext, parameterValues);
            }
            else
            {
                //Since the message was supplied, don't get the default message from the store, just format it
                message = messageService.FormatMessage(validator.Message, context, parameterValues, validator.MessageFormatter);
            }

            return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue);
        }
Ejemplo n.º 19
0
        public RuleValidatorContext(object instance, PropertyValidator validator, RuleValidatorContext parentContext)
        {
            PropertyValue = validator.GetValueForProperty(instance);

            if (validator.PropertyNameOverride == null)
            {
                SelectPropertyName(instance, validator);
            }
            else
            {
                PropertyName = validator.PropertyNameOverride;
            }

            PropertyInfo = validator.PropertyInfo;
            Parent       = parentContext;
            Instance     = instance;
            Level        = validator.Level;
        }
Ejemplo n.º 20
0
        public RuleValidatorContext(object instance, PropertyValidator validator, RuleValidatorContext parentContext)
        {
            PropertyValue = validator.GetValueForProperty(instance);

            if (validator.PropertyNameOverride == null)
            {
                SelectPropertyName(instance, validator);
            }
            else
            {
                PropertyName = validator.PropertyNameOverride;
            }

            PropertyInfo = validator.PropertyInfo;
            Parent = parentContext;
            Instance = instance;
            Level = validator.Level;
        }
Ejemplo n.º 21
0
        public object GetParamValue <T, TProperty>(RuleValidatorContext <T, TProperty> context = null)
        {
            if (IsExpressionParam)
            {
                if (context == null)
                {
                    throw new System.ArgumentException(
                              "Cannot get Param Value for an Expression Param without context.");
                }

                return(CompiledExpression.Invoke(context.Instance));
            }
            else
            {
                if (IsDelegate)
                {
                    return(Delegate.DynamicInvoke(new object[] { context.Instance }));
                }
                else
                {
                    return(paramValue);
                }
            }
        }
Ejemplo n.º 22
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var result = _expression(context.Instance, context.PropertyValue);

            return(Evaluate(result, context, notification));
        }
Ejemplo n.º 23
0
 public abstract bool Validate(object instance, RuleValidatorContext parentRuleContexts, SpecificationContainer specificationContainer, ValidationNotification notification);
Ejemplo n.º 24
0
 public abstract List<ValidationResult> Validate(object instance, RuleValidatorContext parentRuleContexts, SpecificationContainer specificationContainer);
Ejemplo n.º 25
0
 public string FormatMessage(string message, RuleValidatorContext context, object[] parameters)
 {
     return FormatMessage(message, context, parameters, null);
 }
Ejemplo n.º 26
0
 public RuleValidatorContext<Contact, string> BuildContextForLength(string value)
 {
     var contact = new Contact {FirstName = value};
     var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, ValidationLevelType.Error, null);
     return context;
 }
Ejemplo n.º 27
0
 public RuleValidatorContext<Contact, string> BuildContext(string value)
 {
     var contact = new Contact { FirstName = value };
     var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, null);
     return context;
 }
Ejemplo n.º 28
0
        private string buildPropertyName(RuleValidatorContext context)
        {
            //Build a string with the graph of property names
            var propertyNameNodes = new List<string>();

            RuleValidatorContext currentContext = context;
            do
            {
                propertyNameNodes.Add(currentContext.PropertyName.SplitPascalCase());
                currentContext = currentContext.Parent;
            } while (currentContext != null);

            //Reverse by putting the top level first
            propertyNameNodes.Reverse();

            //create a string containing the heirarchy flattened out
            var propertyNameForNestedProperty = new StringBuilder();
            //add a space between nodes
            propertyNameNodes.ForEach(p => propertyNameForNestedProperty.AppendFormat(" {0}", p));

            return propertyNameForNestedProperty.ToString().Trim();
        }