Beispiel #1
0
        public string FormatMessage(string message, RuleValidatorContext context, IList<Object> parameterValues, 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);
            }

            return parameterValues == null ? formattedMessage : String.Format(formattedMessage, parameterValues.ToStringEnum().ToArray());
        }
Beispiel #2
0
        public RuleValidatorContext <Contact, IEnumerable> BuildContextForAliases(PopulateListAction action)
        {
            Contact contact = new Contact();

            switch (action)
            {
            case PopulateListAction.Null:
                contact.Aliases = null;
                break;

            case PopulateListAction.Empty:
                contact.Aliases = new List <string>();
                break;

            case PopulateListAction.NonUnique:
                contact.Aliases = NonUniqueStrings();
                break;

            default:
                contact.Aliases = UniqueStrings();
                break;
            }

            var context = new RuleValidatorContext <Contact, IEnumerable>(contact, "Aliases", contact.Aliases, null, ValidationLevelType.Error, null);

            return(context);
        }
Beispiel #3
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            StringBuilder sb = new StringBuilder();

            if (context.PropertyValue.IsNullOrDefault())
            {
                return(true);
            }

            foreach (var value in context.PropertyValue)
            {
                if (!_forEachPredicate(value))
                {
                    sb.AppendLine(CreateErrorMessage(value));
                }
            }

            if (sb.Length > 0)
            {
                Message = sb.ToString();
                notification.Errors.Add(ValidationResultFactory.Create(this, context, null, null));
                return(false);
            }
            else
            {
                return(true);
            }
        }
Beispiel #4
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            SpecificationBase = specificationContainer.TryGetSpecification <TSpecification>() as Validates <TProperty> ??
                                new TSpecification();

            return(base.Validate(context, specificationContainer, notification));
        }
Beispiel #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);
        }
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            //Resolve the Specification
            Specification = specificationContainer.TryGetSpecification <TSpecification>() as Validates <TCollectionType> ??
                            new TSpecification();

            return(base.Validate(context, specificationContainer, notification));
        }
Beispiel #7
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var floor   = (TProperty)Params[0].GetParamValue(context);
            var ceiling = (TProperty)Params[1].GetParamValue(context);
            Comparer <TProperty> comparer = System.Collections.Generic.Comparer <TProperty> .Default;

            return(Evaluate(comparer.Compare(context.PropertyValue, ceiling) <= 0 && comparer.Compare(context.PropertyValue, floor) >= 0, context, notification));
        }
Beispiel #8
0
 public MessageContext(RuleValidatorContext ruleContext, Type validatorType, bool negate, string messageStoreName, object key)
 {
     RuleContext      = ruleContext;
     ValidatorType    = validatorType;
     Negate           = negate;
     MessageStoreName = messageStoreName;
     Key = key;
 }
Beispiel #9
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var lessThanEqualTo = (TProperty)Params[0].GetParamValue(context);

            Comparer <TProperty> comparer = System.Collections.Generic.Comparer <TProperty> .Default;

            return(Evaluate(comparer.Compare(context.PropertyValue, lessThanEqualTo) <= 0, context, notification));
        }
Beispiel #10
0
        public RuleValidatorContext <Contact, int> BuildContextForNumberOfDependents(int value)
        {
            var contact = new Contact {
                NumberOfDependents = value
            };
            var context = new RuleValidatorContext <Contact, int>(contact, "NumberOfDependents", contact.NumberOfDependents, null, ValidationLevelType.Error, null);

            return(context);
        }
Beispiel #11
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);
        }
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="specificationContainer">
        /// The specification container.
        /// </param>
        /// <param name="notification">
        /// The notification.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Validate(
            RuleValidatorContext <T, DateTime?> context,
            SpecificationContainer specificationContainer,
            ValidationNotification notification)
        {
            var isValid = context.PropertyValue.ToDateOnly() >= this.startDate && context.PropertyValue.ToDateOnly() <= DateTime.Today;

            return(this.Evaluate(isValid, context, notification));
        }
Beispiel #13
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;
 }
Beispiel #14
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, ValidationLevelType.Error, null);

            return(context);
        }
        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);
        }
Beispiel #16
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            var regexPattern = (string)Params[0].GetParamValue(context);

            Regex regex   = new Regex(regexPattern);
            bool  isMatch = regex.IsMatch(context.PropertyValue);

            return(Evaluate(isMatch, context, notification));
        }
        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);
        }
        public RuleValidatorContext <CalendarEvent, System.DateTime> BuildContextForCalendarEventEndDate(string subject, DateTime startDate, DateTime endDate)
        {
            var calendarEvent = new CalendarEvent()
            {
                Subject = subject, StartDate = startDate, EndDate = endDate
            };
            var context = new RuleValidatorContext <CalendarEvent, DateTime>(calendarEvent, "EndDate", calendarEvent.EndDate, null, ValidationLevelType.Error, null);

            return(context);
        }
Beispiel #19
0
        public bool Between_IsValid(int propertyValue, int floor, int ceiling)
        {
            //Create Validator
            var validator = new Between <Contact, int>(floor, ceiling);
            RuleValidatorContext <Contact, int> context = BuildContextForNumberOfDependents(propertyValue);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #20
0
        public bool IsFalse_IsValid(bool propertyValue)
        {
            //Create Validator
            var validator = new IsFalse <Contact>();
            RuleValidatorContext <Contact, bool> context = BuildContextForContactActive(propertyValue);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #21
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            int length = String.IsNullOrEmpty(context.PropertyValue) ? 0 : context.PropertyValue.Length;

            var contextWithLength = new RuleValidatorContext <T, string>(context.Instance, context.PropertyName, length.ToString(),
                                                                         context.PropertyInfo, context.Level, null);

            var max = (int)Params[0].GetParamValue(context);

            return(Evaluate(length <= max, contextWithLength, notification));
        }
Beispiel #22
0
        public bool Contains_IsValid(string lookingFor)
        {
            //Create Validator
            var validator = new Contains <Contact, IEnumerable>(lookingFor);
            RuleValidatorContext <Contact, IEnumerable> context = BuildContextForAliases(PopulateListAction.Populate);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #23
0
        public bool LessThanEqualTo_IsValid(int propertyValue, int lessThanEqualTo)
        {
            //Create Validator
            var validator = new LessThanEqualTo <Contact, int>(lessThanEqualTo);
            RuleValidatorContext <Contact, int> context = BuildContextForNumberOfDependents(propertyValue);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #24
0
        public bool CountLessThan_Expression_IsValid(PopulateListAction populateListAction, int val)
        {
            //Create Validator
            var validator = new CountLessThan <Contact, IEnumerable>(val);
            RuleValidatorContext <Contact, IEnumerable> context = BuildContextForAliases(populateListAction);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
        public bool Matches_IsValid(string firstName, string regexPattern)
        {
            //Create Validator
            var validator = new Matches <Contact>(regexPattern);
            RuleValidatorContext <Contact, string> context = BuildContextForLength(firstName);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
        public bool MaxLength_IsValid(string propertyValue, int maxLength)
        {
            //Create Validator
            var validator = new MaxLength <Contact>(maxLength);
            RuleValidatorContext <Contact, string> context = BuildContextForLength(propertyValue);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #27
0
        public override bool Validate(RuleValidatorContext <T, string> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            //Guard against null in Regex Match
            if (context.PropertyValue == null)
            {
                return(Evaluate(false, context, notification));
            }

            Match m = new Regex(@"^[a-zA-Z\s]+$").Match(context.PropertyValue);

            return(Evaluate(m.Success, context, notification));
        }
        public bool MaxLength_Expression_IsValid(string firstName, string lastName)
        {
            //Create Validator
            //FirstName Length must be at least the same length as the LastName
            var validator = new MaxLength <Contact>(c => (int)(c.LastName.Length));
            RuleValidatorContext <Contact, string> context = BuildContextForLength(firstName, lastName);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #29
0
        public bool CustomRule_WithDelegate_IsValid(string propertyValue)
        {
            //Create Validator
            var validator = new CustomRule <Contact, string>(IsValidName);

            validator.Message = "Invalid Name";
            RuleValidatorContext <Contact, string> context = BuildContextForName(propertyValue);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            return(validator.Validate(context, null, notification));
        }
Beispiel #30
0
        public bool GreaterThan_Expression_IsValid(int propertyValue, int greaterThan)
        {
            //Create Validator
            var validator = new GreaterThan <Contact, int>(c => c.NumberOfDependents);
            RuleValidatorContext <Contact, int> context = BuildContextForNumberOfDependents(propertyValue);

            context.Instance.NumberOfDependents = greaterThan;

            var notification = new ValidationNotification();

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