예제 #1
0
 public static ValidationNotification EqualIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, object compare)
 {
     return(source.EqualIsValid(
                data.GetStructureToValidate(expression),
                compare));
 }
 public GetProductsResponseObject(int statusCode, ValidationNotification validationNotification)
 {
     this.StatusCode = statusCode;
     this.ValidationNotifications = new List <ValidationNotification> {
         validationNotification
     };
 }
예제 #3
0
        public static bool HasNotification <T, P>(
            this ValidationNotification source, T data, Expression <Func <T, P> > expression)
        {
            string reference = data.GetStructureToValidate(expression).Reference;

            return(source.Messages.Any(x => x.Reference == reference));
        }
예제 #4
0
 public DeleteProductResponseObject(int statusCode, ValidationNotification validationNotifications)
 {
     ValidationNotifications = new List <ValidationNotification> {
         validationNotifications
     };
     StatusCode = statusCode;
 }
예제 #5
0
        public static ValidationNotification CompareLessNumberIsValid(
            this ValidationNotification source,
            IStructureToValidate data,
            IStructureToValidate dataCompare)
        {
            string reference = data.Reference;

            object value   = data.Value;
            string display = data.Display;

            object valueCompare   = dataCompare.Value;
            string displayCompare = dataCompare.Display;

            source.CleanLastMessage();

            if (!(value is null) && !(valueCompare is null))
            {
                if (decimal.TryParse(Convert.ToString(value), out decimal newValue) &&
                    decimal.TryParse(Convert.ToString(valueCompare), out decimal newCompare) &&
                    newValue >= newCompare)
                {
                    string text = string.Format(
                        Resource.XCompareLessInvalid,
                        display, displayCompare);

                    var message = new ValidationMessage(text, reference);
                    source.SetLastMessage(message, data.Display);
                    source.Add(message);
                }
            }

            return(source);
        }
        public void CreateExpression_DateOutsideOfWindowAndIsMonday_ReverseExpression_BuildsExpression(int year, int month, int day, bool validates, int numberOfErrorMsg)
        {
            // Test: (DateTime d) => d.DayOfWeek == DayOfWeek.Monday & (d < floorDate | d > ceilingDate)
            var floorDate   = new DateTime(2010, 2, 1);
            var ceilingDate = new DateTime(2010, 3, 1);
            var testDate    = new DateTime(year, month, day);

            // build rules / rule nodes for "d < floorDate | d > ceilingDate" and put in a group
            var rangeRuleNode = new RuleNode(new LessThan <CalendarEvent, DateTime>(floorDate));

            rangeRuleNode.OrChild(new RuleNode(new GreaterThan <CalendarEvent, DateTime>(ceilingDate)));
            var groupNode = new GroupNode(rangeRuleNode);

            // build rule / rule node for "d.DayOfWeek == DayOfWeek.Monday
            var dateOneMondayRule = new CustomRule <CalendarEvent, DateTime>((c, d) => d.DayOfWeek == DayOfWeek.Monday);

            dateOneMondayRule.Message = "Date does not fall on a Monday.";
            var dateOneMondayRuleNode = new RuleNode(dateOneMondayRule);

            // add the rangeRuleNode as an And child of dateOneMondayRuleNode
            dateOneMondayRuleNode.AndChild(groupNode);

            var tree = new RuleTree <CalendarEvent, DateTime>(dateOneMondayRuleNode);

            Assert.That(tree.LambdaExpression, Is.Not.Null);

            var context      = BuildContextForCalendarEventStartDate(testDate);
            var notification = new ValidationNotification();
            var isValid      = tree.LambdaExpression(context, null, notification);

            Assert.That(isValid, Is.EqualTo(validates));
        }
예제 #7
0
 public static ValidationNotification BetweenDateTimeIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, IEnumerable <DateTime> options, CultureInfo cultureInfo = null)
 {
     return(source.BetweenDateTimeIsValid(
                data.GetStructureToValidate(expression),
                options, cultureInfo));
 }
예제 #8
0
 public static ValidationNotification RangeTimeSpanIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, TimeSpan minimum, TimeSpan maximum)
 {
     return(source.RangeTimeSpanIsValid(
                data.GetStructureToValidate(expression),
                minimum, maximum));
 }
예제 #9
0
        //public void ValidateFromClient(SpecManagerServerValidationResult request)
        //{

        //}


        public void Notify(ValidationNotification notification)
        {
            //Bind the ValidationNotification to each Proxy Validator
            var specValidators = NamingContainer.Controls.All().OfType <Validator>();

            //Explicitly call Validate to trigger any validation messages
            specValidators.ToList().ForEach(x =>
            {
                x.ValidationNotification = notification;
                x.Validate();
            });

            //Get any Errors that aren't bound to a PropertyValidator and add it to the Validation Summary
            var ufoProperties = (from error in notification.Errors
                                 select error.Property.Name).Except(
                from validators in specValidators select validators.PropertyName).ToList();

            //Group ValidationResults by Property Name so a all results for a Propert can be passed to one
            //DummyValidator which will format the list of results for that Property
            var errorsByPropertyName =
                from error in notification.Errors
                group error by error.Property.Name
                into p
                select new { PropertyName = p.Key, Errors = p };

            foreach (var property in errorsByPropertyName)
            {
                //Check if this PropertyName is in the list of Properties with no Validator, and if so, add one
                if (ufoProperties.Exists(x => x == property.PropertyName))
                {
                    this.Page.Validators.Add(new SpecExpressDummyValidator(property.Errors.ToList()));
                }
            }
        }
예제 #10
0
 public static ValidationNotification RangeNumberIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, decimal minimum, decimal maximum)
 {
     return(source.RangeNumberIsValid(
                data.GetStructureToValidate(expression),
                minimum, maximum));
 }
예제 #11
0
 public static ValidationNotification RangeItemsIsValid <T>(
     this ValidationNotification source, T data, Expression <Func <T, IEnumerable> > expression, int minimum, int maximum)
 {
     return(source.RangeItemsIsValid(
                data.GetStructureToValidate(expression),
                minimum, maximum));
 }
예제 #12
0
        public static ValidationNotification CompareDifferentIsValid(
            this ValidationNotification source,
            IStructureToValidate data,
            IStructureToValidate dataCompare)
        {
            string reference = data.Reference;

            object value   = data.Value;
            string display = data.Display;

            object valueCompare   = dataCompare.Value;
            string displayCompare = dataCompare.Display;

            source.CleanLastMessage();

            if (!(value is null))
            {
                if (value.ToString() == valueCompare?.ToString())
                {
                    string text = string.Format(
                        Resource.XCompareDifferentInvalid,
                        display, displayCompare);

                    var message = new ValidationMessage(text, reference);
                    source.SetLastMessage(message, data.Display);
                    source.Add(message);
                }
            }

            return(source);
        }
예제 #13
0
 public static ValidationNotification EnumIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, Type type)
 {
     return(source.EnumIsValid(
                data.GetStructureToValidate(expression),
                type));
 }
예제 #14
0
        public void CreateExpression_DateOutsideOfWindowAndIsMonday_BuildsExpression(int year, int month, int day, bool validates, int numberOfErrorMsg)
        {
            // Test: (DateTime d) => (d < floorDate | d > ceilingDate) & d.DayOfWeek == DayOfWeek.Monday
            var floorDate = new DateTime(2010, 2, 1);
            var ceilingDate = new DateTime(2010, 3, 1);
            var testDate = new DateTime(year, month, day);

            // build rule / rule node for "d.DayOfWeek == DayOfWeek.Monday
            var dateOneMondayRule = new CustomRule<CalendarEvent, DateTime>((c, d) => d.DayOfWeek == DayOfWeek.Monday);
            dateOneMondayRule.Message = "Date does not fall on a Monday.";
            var dateOneMondayRuleNode = new RuleNode(dateOneMondayRule);

            // build rules / rule nodes for "d < floorDate | d > ceilingDate"
            var rangeRuleNode = new RuleNode(new LessThan<CalendarEvent, DateTime>(floorDate));
            rangeRuleNode.OrChild(new RuleNode(new GreaterThan<CalendarEvent, DateTime>(ceilingDate)));

            // put the rules / rule nodes together using a group to enforce the "or" precidence over the "and"
            var groupNode = new GroupNode(rangeRuleNode);
            groupNode.AndChild(dateOneMondayRuleNode);

            var tree = new RuleTree<CalendarEvent, DateTime>(groupNode);

            Assert.That(tree.LambdaExpression, Is.Not.Null);

            var context = BuildContextForCalendarEventStartDate(testDate);
            var notification = new ValidationNotification();
            var isValid = tree.LambdaExpression(context, null, notification);

            Assert.That(isValid, Is.EqualTo(validates));
        }
예제 #15
0
 public static ValidationNotification MinCharactersIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, int minimum)
 {
     return(source.MinCharactersIsValid(
                data.GetStructureToValidate(expression),
                minimum));
 }
예제 #16
0
 public static ValidationNotification NotNullOrEmptyIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, bool ignoreWithSpace = false)
 {
     return(source.NotNullOrEmptyIsValid(
                data.GetStructureToValidate(expression),
                ignoreWithSpace));
 }
예제 #17
0
 public static ValidationNotification DateTimeIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, CultureInfo cultureInfo = null)
 {
     return(source.DateTimeIsValid(
                data.GetStructureToValidate(expression),
                cultureInfo));
 }
예제 #18
0
        public static ValidationNotification CompareLessDateTimeIsValid(
            this ValidationNotification source,
            IStructureToValidate data,
            IStructureToValidate dataCompare,
            CultureInfo cultureInfo = null)
        {
            string reference = data.Reference;

            object value   = data.Value;
            string display = data.Display;

            object valueCompare   = dataCompare.Value;
            string displayCompare = dataCompare.Display;

            cultureInfo = cultureInfo ?? CultureInfo.CurrentCulture;
            source.CleanLastMessage();

            if (!(value is null) && !(valueCompare is null))
            {
                if (DateTime.TryParse(Convert.ToString(value), cultureInfo, DateTimeStyles.None, out DateTime newValue) &&
                    DateTime.TryParse(Convert.ToString(valueCompare), cultureInfo, DateTimeStyles.None, out DateTime newCompare) &&
                    newValue >= newCompare)
                {
                    string text = string.Format(
                        Resource.XCompareLessInvalid,
                        display, displayCompare);

                    ValidationMessage message = new ValidationMessage(text, reference);
                    source.SetLastMessage(message, data.Display);
                    source.Add(message);
                }
            }

            return(source);
        }
예제 #19
0
 public static ValidationNotification ExactItemsIsValid <T>(
     this ValidationNotification source, T data, Expression <Func <T, IEnumerable> > expression, int exact)
 {
     return(source.ExactItemsIsValid(
                data.GetStructureToValidate(expression),
                exact));
 }
예제 #20
0
 public static ValidationNotification ExactCharactersIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, int exact)
 {
     return(source.ExactCharactersIsValid(
                data.GetStructureToValidate(expression),
                exact));
 }
예제 #21
0
 public static ValidationNotification BetweenTimeSpanIsValid <T, P>(
     this ValidationNotification source, T data, Expression <Func <T, P> > expression, IEnumerable <TimeSpan> options)
 {
     return(source.BetweenTimeSpanIsValid(
                data.GetStructureToValidate(expression),
                options));
 }
예제 #22
0
    public static ValidationNotification ToNotification(this IEnumerable <ValidationResult> validationResults)
    {
        var notification = new ValidationNotification()
        {
            Errors = validationResults.ToList()
        };

        return(notification);
    }
        /// <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));
        }
예제 #24
0
 public static ValidationNotification RegexIsValid <T, P>(
     this ValidationNotification source, T data,
     Expression <Func <T, P> > expression,
     string pattern, RegexOptions options = RegexOptions.None)
 {
     return(source.RegexIsValid(
                data.GetStructureToValidate(expression),
                pattern, options));
 }
예제 #25
0
        public static ValidationNotification RemoveAtReference <T, P>(
            this ValidationNotification source, T data, Expression <Func <T, P> > expression)
        {
            var validate = data.GetStructureToValidate(expression);

            source.RemoveAtReference(validate.Reference);

            return(source);
        }
예제 #26
0
 public static ValidationNotification CompareDifferentIsValid <T, P, PCompare>(
     this ValidationNotification source, T data,
     Expression <Func <T, P> > expression,
     Expression <Func <T, PCompare> > expressionCompare)
 {
     return(source.CompareDifferentIsValid(
                data.GetStructureToValidate(expression),
                data.GetStructureToValidate(expressionCompare)));
 }
예제 #27
0
 public static ValidationNotification RangeNumberIsValid(
     this ValidationNotification source, object value, decimal minimum, decimal maximum)
 {
     return(source.RangeNumberIsValid(new StructureToValidate
     {
         Value = value,
         Display = Resource.DisplayValue,
         Reference = null
     }, minimum, maximum));
 }
예제 #28
0
 private static ValidationNotification ExactCharactersIsValid(
     this ValidationNotification source, object value, string display, string reference, int exact)
 {
     return(source.ExactCharactersIsValid(new StructureToValidate
     {
         Value = value,
         Display = display,
         Reference = reference
     }, exact));
 }
예제 #29
0
 public static ValidationNotification TimeSpanIsValid(
     this ValidationNotification source, object value)
 {
     return(source.TimeSpanIsValid(new StructureToValidate
     {
         Value = value,
         Display = Resource.DisplayValue,
         Reference = null
     }));
 }
예제 #30
0
 public static ValidationNotification ExactCharactersIsValid(
     this ValidationNotification source, object value, int exact)
 {
     return(source.ExactCharactersIsValid(new StructureToValidate
     {
         Value = value,
         Display = Resource.DisplayValue,
         Reference = null
     }, exact));
 }
예제 #31
0
 public static ValidationNotification ExactItemsIsValid(
     this ValidationNotification source, IEnumerable value, int exact)
 {
     return(source.ExactItemsIsValid(new StructureToValidate
     {
         Value = value,
         Display = Resource.DisplayValue,
         Reference = null
     }, exact));
 }
예제 #32
0
        public bool IsTrue_IsValid(bool propertyValue)
        {
            //Create Validator
            var validator = new IsTrue<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);
        }
예제 #33
0
        public bool IsAlpha_IsValid(string propertyValue)
        {
            //Create Validator
            var validator = new Alpha<Contact>();
            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);
        }
예제 #34
0
        public bool IsInSet_GenericList_IsValid(string propertyValue)
        {
            //List
            var list = new List<string> {"US", "GB", "AU", "CA"};
            //Create Validator
            var validator = new Rules.GeneralValidators.IsInSet<Contact,string>(list);
            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);
        }
예제 #35
0
        public bool IsValid()
        {
            ValidationCatalog.AddSpecification<Contact>(x =>
                                                              {
                                                                  x.Check(contact => contact.LastName).Required();
                                                                  x.Check(contact => contact.FirstName).Required();
                                                                  x.Check(contact => contact.DateOfBirth).Optional().
                                                                      GreaterThan(new DateTime(1950, 1, 1));
                                                              });

            //Validate
            Errors = ValidationCatalog.Validate(this);
            return Errors.IsValid;
        }
예제 #36
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);
        }
예제 #37
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);
        }
예제 #38
0
        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 ValidationNotificationEventArgs(ValidationNotification vn)
 {
     _ve = vn;
 }
예제 #40
0
        public bool LengthBetween_IsValid(string propertyValue, int low, int high)
        {
            //Create Validator
            var validator = new LengthBetween<Contact>(low, high);
            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);
        }
예제 #41
0
        public void Validate_OptionalProperty_WithNoValue_IsValid()
        {
            var emptyContact = new Contact();
            emptyContact.FirstName = string.Empty;
            emptyContact.LastName = string.Empty;

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

            //add a single rule
            var lengthValidator = new LengthBetween<Contact>(1, 5);
            propertyValidator.AndRule(lengthValidator); //.Rules.Add(lengthValidator);

            var notification = new ValidationNotification();

            //Validate
            var result = propertyValidator.Validate(emptyContact, null, notification);

            Assert.That(result, Is.True);
            Assert.That(notification.Errors, Is.Empty);
        }
예제 #42
0
        public void Validate_Property_With_PropertyNameOverrideExpression_IsValid()
        {
            var emptyContact = new Contact();
            emptyContact.FirstName = "George's last name";
            emptyContact.LastName = string.Empty;

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

            propertyValidator.PropertyNameOverrideExpression = new Func<Contact, string>( o => o.FirstName);

            //add a single rule
            var lengthValidator = new LengthBetween<Contact>(1, 5);
            propertyValidator.AndRule(lengthValidator);

            //Validate
            ValidationNotification notification = new ValidationNotification();
            propertyValidator.Validate(emptyContact, null, notification);

            Assert.That(notification.Errors, Is.Not.Empty);
        }
        //public void ValidateFromClient(SpecManagerServerValidationResult request)
        //{
        //}
        public void Notify(ValidationNotification notification)
        {
            //Bind the ValidationNotification to each Proxy Validator
            var specValidators = NamingContainer.Controls.All().OfType<Validator>();
            //Explicitly call Validate to trigger any validation messages
            specValidators.ToList().ForEach(x =>
            {
                x.ValidationNotification = notification;
                x.Validate();
            });

            //Get any Errors that aren't bound to a PropertyValidator and add it to the Validation Summary
            var ufoProperties = (from error in notification.Errors
                                 select error.Property.Name).Except(
               from validators in specValidators select validators.PropertyName).ToList();

            //Group ValidationResults by Property Name so a all results for a Propert can be passed to one
            //DummyValidator which will format the list of results for that Property
            var errorsByPropertyName =
            from error in notification.Errors
            group error by error.Property.Name
                into p
                select new { PropertyName = p.Key, Errors = p };

            foreach (var property in errorsByPropertyName)
            {
                //Check if this PropertyName is in the list of Properties with no Validator, and if so, add one
                if (ufoProperties.Exists(x => x == property.PropertyName))
                {
                    this.Page.Validators.Add(new SpecExpressDummyValidator(property.Errors.ToList()));
                }
            }
        }
예제 #44
0
        public bool MinLength_Expression_IsValid(string firstName, string lastName)
        {
            //Create Validator
            //FirstName Length must be at least the same length as the LastName
            var validator = new MinLength<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);
        }
예제 #45
0
        public void CreateExpression_TwoRules_OrRelation_BuildsExpression(int year, int month, int day, bool validates, int numberOfErrorMsg)
        {
            var floorDate = new DateTime(2010, 2, 1);
            var ceilingDate = new DateTime(2010, 3, 1);
            var testDate = new DateTime(year, month, day);

            var tree = new RuleTree.RuleTree<CalendarEvent, DateTime>(
                new RuleTree.RuleNode(new LessThan<CalendarEvent, DateTime>(floorDate)));

            tree.Root.OrChild(new RuleNode(new GreaterThan<CalendarEvent, DateTime>(ceilingDate)));

            Assert.That(tree.LambdaExpression, Is.Not.Null);

            var context = BuildContextForCalendarEventStartDate(testDate);
            var notification = new ValidationNotification();
            var isValid = tree.LambdaExpression(context, null, notification);

            Assert.That(isValid, Is.EqualTo(validates));
        }
예제 #46
0
        public bool CreateExpression_SingleRule_BuildsExpression(bool propertyValue)
        {
            var tree = new RuleTree.RuleTree<Contact, bool>(
                new RuleTree.RuleNode(new IsTrue<Contact>()));

            Assert.That(tree.LambdaExpression, Is.Not.Null);

            RuleValidatorContext<Contact, bool> context = BuildContextForContactActive(propertyValue);

            var notification = new ValidationNotification();

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

            return isValid;
        }