Validate() public method

Validates the specified object.
public Validate ( object validationContext, object>.IDictionary contextParams, IValidationErrors errors ) : bool
validationContext object The object to validate.
contextParams object>.IDictionary Additional context parameters.
errors IValidationErrors instance to add error messages to.
return bool
コード例 #1
1
        public void ContextNarrowing()
        {
            Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
            
            ConditionValidator cv1 = new ConditionValidator("DOB.Year == 1856", null);
            ConditionValidator cv2 = new ConditionValidator("Year == 1856", null);
            
            StaticListableObjectFactory factory = new StaticListableObjectFactory();
            factory.AddObject("cv1", cv1);
            factory.AddObject("cv2", cv2);

            ValidatorReference v1 = new ValidatorReference();
            v1.ObjectFactory = factory;
            v1.Name = "cv1";

            IValidationErrors errors = new ValidationErrors();

            Assert.IsTrue(v1.Validate(context, null, errors));
            Assert.IsTrue(v1.Validate(context, errors));

            ValidatorReference v2 = new ValidatorReference();
            v2.ObjectFactory = factory;
            v2.Name = "cv2";
            v2.Context = Expression.Parse("DOB");

            Assert.IsTrue(v2.Validate(context, null, errors));
            Assert.IsTrue(v2.Validate(context, errors));

            ValidatorReference v3 = new ValidatorReference("false");
            v3.ObjectFactory = factory;
            v3.Name = "cv2";
            v3.Context = Expression.Parse("DOB");

            Assert.IsTrue(v3.Validate(null, errors));
        }
コード例 #2
0
        public void FalseValidatorReferenceNotEvaluatedBecauseWhenExpressionReturnsFalse()
        {
            StaticListableObjectFactory factory = new StaticListableObjectFactory();
            factory.AddObject("validator", new FalseValidator());

            ValidatorReference v = new ValidatorReference("false");
            v.ObjectFactory = factory;
            v.Name = "validator";

            IValidationErrors errors = new ValidationErrors();
            Assert.IsTrue(v.Validate(null, null, errors));
            Assert.IsTrue(v.Validate(null, errors));
        }
コード例 #3
0
        public void FalseValidatorReference()
        {
            StaticListableObjectFactory factory = new StaticListableObjectFactory();
            factory.AddObject("validator", new FalseValidator());

            ValidatorReference v = new ValidatorReference();
            v.ObjectFactory = factory;
            v.Name = "validator";

            IValidationErrors errors = new ValidationErrors();
            Assert.IsFalse(v.Validate(null, null, errors));
            Assert.IsFalse(v.Validate(null, errors));
        }
コード例 #4
0
        public void FalseValidatorReferenceNotEvaluatedBecauseWhenExpressionReturnsFalse()
        {
            StaticListableObjectFactory factory = new StaticListableObjectFactory();

            factory.AddObject("validator", new FalseValidator());

            ValidatorReference v = new ValidatorReference("false");

            v.ObjectFactory = factory;
            v.Name          = "validator";

            IValidationErrors errors = new ValidationErrors();

            Assert.IsTrue(v.Validate(null, null, errors));
            Assert.IsTrue(v.Validate(null, errors));
        }
コード例 #5
0
        public void FalseValidatorReference()
        {
            StaticListableObjectFactory factory = new StaticListableObjectFactory();

            factory.AddObject("validator", new FalseValidator());

            ValidatorReference v = new ValidatorReference();

            v.ObjectFactory = factory;
            v.Name          = "validator";

            IValidationErrors errors = new ValidationErrors();

            Assert.IsFalse(v.Validate(null, null, errors));
            Assert.IsFalse(v.Validate(null, errors));
        }
コード例 #6
0
        public void ContextNarrowing()
        {
            Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");

            ConditionValidator cv1 = new ConditionValidator("DOB.Year == 1856", null);
            ConditionValidator cv2 = new ConditionValidator("Year == 1856", null);

            StaticListableObjectFactory factory = new StaticListableObjectFactory();

            factory.AddObject("cv1", cv1);
            factory.AddObject("cv2", cv2);

            ValidatorReference v1 = new ValidatorReference();

            v1.ObjectFactory = factory;
            v1.Name          = "cv1";

            IValidationErrors errors = new ValidationErrors();

            Assert.IsTrue(v1.Validate(context, null, errors));
            Assert.IsTrue(v1.Validate(context, errors));

            ValidatorReference v2 = new ValidatorReference();

            v2.ObjectFactory = factory;
            v2.Name          = "cv2";
            v2.Context       = Expression.Parse("DOB");

            Assert.IsTrue(v2.Validate(context, null, errors));
            Assert.IsTrue(v2.Validate(context, errors));

            ValidatorReference v3 = new ValidatorReference("false");

            v3.ObjectFactory = factory;
            v3.Name          = "cv2";
            v3.Context       = Expression.Parse("DOB");

            Assert.IsTrue(v3.Validate(null, errors));
        }