示例#1
0
        public void AddError(ValidationError validationError)
        {
            _innerReport.AddError(validationError);
            var visitor = new ProperyNameVisitor();

            visitor.Visit(validationError.Expression.Expression);

            if (visitor.PropertyName == null)
            {
                return;
            }

            var errorMessage    = _innerReport.GetErrorMessage(validationError.Value, validationError.Expression, _errorResolver);
            var modelDictionary = _modelState[visitor.PropertyName];

            if (modelDictionary == null)
            {
                return;
            }

            //NOTE: Add errorMessage even when errorMessage is null.
            var errorCollection = modelDictionary.Errors;

            //Clear errorCollection (in case of re-use).
            errorCollection.Clear();
            errorCollection.Add(errorMessage);
        }
示例#2
0
        public void ShouldSkipValidationWhenShortCircuiting()
        {
            var president     = new Member(null);
            var club          = new Club(president);
            var report        = new ValidationReport();
            var memberNameExp = new EquatableExpression(ExpressionHelper.New <Member, string>(mm => mm.Name));
            var presidentExp  = new EquatableExpression(ExpressionHelper.New <Club, Member>(cc => cc.President));

            //Report an error on the President expression...
            report.AddError(new ValidationError(new EqualRule <int>(2), presidentExp, new object[0], club));
            _en1.Validate(club, report, ValidationReportDepth.FieldShortCircuit);
            //because an error has already been reported on the club's president, the member's name rule would not have been invoked
            Assert.IsFalse(report.HasError(president, memberNameExp));

            //Now try this again without short-circuiting
            _en1.Validate(club, report, ValidationReportDepth.All);
            Assert.IsTrue(report.HasError(president, memberNameExp));
        }
        public void ShouldSkipValidationWhenShortCircuiting()
        {
            var president = new Member(null);
            var club = new Club(president);
            var report = new ValidationReport();
            var memberNameExp = new EquatableExpression(ExpressionHelper.New<Member, string>(mm => mm.Name));
            var presidentExp = new EquatableExpression(ExpressionHelper.New<Club, Member>(cc => cc.President));

            //Report an error on the President expression...
            report.AddError(new ValidationError(new EqualRule<int>(2), presidentExp, new object[0], club, club, presidentExp));
            _en1.Validate(club, report, ValidationReportDepth.FieldShortCircuit);
            //because an error has already been reported on the club's president, the member's name rule would not have been invoked
            Assert.IsFalse(report.HasError(president, memberNameExp));

            //Now try this again without short-circuiting
            _en1.Validate(club, report, ValidationReportDepth.All);
            Assert.IsTrue(report.HasError(president, memberNameExp));
        }