public void VerifyCustomValidatorAndShouldBeFailureTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = true; //important

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_VerifyCustomValidatorAndShouldBeFailure32Test")
            .ForCustomValidator(new LengthShould32Validator())
            .Build();

            var validator1 = ValidationMe.Use("UT_VerifyCustomValidatorAndShouldBeFailure32Test").Resolve <Length32Model>();
            var validator2 = new LengthShould32Validator();

            var model = new Length32Model {
                Name = "12345678901234"
            };

            validator1.Verify(model).IsValid.ShouldBeFalse();
            validator2.Verify(model).IsValid.ShouldBeFalse();
        }
        public void VerifyAnByInstanceShouldBeFailureTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_VerifyAnByInstanceShouldBeFailureTest")
            .ForType <NiceBoat>()
            .ForMember("Name").NotEmpty()
            .AndForMember("Length").GreaterThanOrEqual(0)
            .AndForMember("Width").GreaterThanOrEqual(0)
            .Build();

            var validator = ValidationMe.Use("UT_VerifyAnByInstanceShouldBeFailureTest").Resolve <NiceBoat>();

            var instance = new NiceBoat
            {
                Name       = "",
                Length     = -1000,
                Width      = -30,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "nice@@boat.com"
            };

            var result1 = validator.Verify(instance);

            result1.ShouldNotBeNull();
            result1.IsValid.ShouldBeFalse();
            result1.Errors.Should().HaveCount(3);
        }
        public void VerifyOneShouldBeSuccessTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_VerifyOneShouldBeSuccessTest")
            .ForType <NiceBoat>()
            .ForMember("Name").NotEmpty()
            .AndForMember("Length").GreaterThanOrEqual(0)
            .AndForMember("Width").GreaterThanOrEqual(0)
            .Build();

            var validator = ValidationMe.Use("UT_VerifyOneShouldBeSuccessTest").Resolve <NiceBoat>();

            validator.VerifyOne(typeof(string), "Nice", "Name").IsValid.ShouldBeTrue();
            validator.VerifyOne(typeof(long), 1000, "Length").IsValid.ShouldBeTrue();
            validator.VerifyOne(typeof(long), 20, "Width").IsValid.ShouldBeTrue();
            validator.VerifyOne(typeof(DateTime), DateTimeFactory.Create(2020, 12, 21), "CreateTime").IsValid.ShouldBeTrue();
            validator.VerifyOne(typeof(string), "*****@*****.**", "Email").IsValid.ShouldBeTrue();
        }
        public void VerifyOneWithPropertySelectorAndShouldBeFailureTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_VerifyOneWithPropertySelectorAndShouldBeFailureTest")
            .ForType <NiceBoat>()
            .ForMember("Name").NotEmpty()
            .AndForMember("Length").GreaterThanOrEqual(0)
            .AndForMember("Width").GreaterThanOrEqual(0)
            .Build();

            var validator = ValidationMe.Use("UT_VerifyOneWithPropertySelectorAndShouldBeFailureTest").Resolve <NiceBoat>();

            validator.VerifyOne(x => x.Name, "").IsValid.ShouldBeFalse();
            validator.VerifyOne(x => x.Length, -1000).IsValid.ShouldBeFalse();
            validator.VerifyOne(x => x.Width, -30).IsValid.ShouldBeFalse();
            validator.VerifyOne(x => x.CreateTime, DateTimeFactory.Create(2020, 12, 21)).IsValid.ShouldBeTrue();
            validator.VerifyOne(x => x.Email, "nice@@boat.com").IsValid.ShouldBeTrue(); // Because Annotation and CustomValidation are all disable.
        }
        public void ConditionPriorityTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_ConditionPriorityTest")
            .ForType <NiceBoat>()
            .ForMember(x => x.Name).MaxLength(10).And().MinLength(8).Or().MaxLength(20).And().MinLength(18)
            .Build();

            var validator = ValidationMe.Use("UT_ConditionPriorityTest").Resolve <NiceBoat>();

            var instance = new NiceBoat
            {
                Name       = "NiceBoat1234567890Z", //19
                Length     = 1000,
                Width      = 30,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "*****@*****.**"
            };

            var result1 = validator.Verify(instance);

            result1.ShouldNotBeNull();

            result1.IsValid.ShouldBeTrue();
        }
Ejemplo n.º 6
0
        public void SimpleOrOps_ShouldBeFailure_Test()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_SimpleOrOps_ShouldBeFailure_Test")
            .ForType <NiceBoat>()
            .ForMember(x => x.Width).GreaterThan(12).Or().LessThan(8)
            .Build();

            var validator = ValidationMe.Use("UT_SimpleOrOps_ShouldBeFailure_Test").Resolve <NiceBoat>();

            var instance = new NiceBoat
            {
                Name       = "NiceBoat1",
                Length     = 1000,
                Width      = 10,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "*****@*****.**"
            };

            var result1 = validator.Verify(instance);

            result1.ShouldNotBeNull();

            result1.IsValid.ShouldBeFalse();
        }
        public void UseGenericStrategyAndVerifyInstanceAndReturnSuccessTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_UseGenericStrategyAndVerifyInstanceAndReturnSuccessTest")
            .ForStrategy <GenericNiceBoatStrategy, NiceBoat>()
            .Build();

            var validator = ValidationMe.Use("UT_UseGenericStrategyAndVerifyInstanceAndReturnSuccessTest").Resolve <NiceBoat>();

            var instance = new NiceBoat
            {
                Name       = "NiceBoat1000",
                Length     = 1000,
                Width      = 30,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "*****@*****.**"
            };

            var result1 = validator.Verify(instance);

            result1.ShouldNotBeNull();

            result1.IsValid.ShouldBeTrue();
        }
Ejemplo n.º 8
0
        public void ValidationOneWithDataAnnotationAndShouldBeSuccessTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = true;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_ValidationOneWithDataAnnotationAndShouldBeSuccessTest")
            .ForDataAnnotationSupport()
            .Build();

            var validator = ValidationMe.Use("UT_ValidationOneWithDataAnnotationAndShouldBeSuccessTest").Resolve <GoodJob>();

            var model = new GoodJob()
            {
                Name = "Good", Cost = 11
            };

            validator.VerifyOne(x => x.Name, model.Name).IsValid.Should().BeTrue();
            validator.VerifyOne(model.Name, "Name").IsValid.Should().BeTrue();
            validator.VerifyOne(typeof(GoodJob), model.Name, "Name").IsValid.Should().BeTrue();
        }
Ejemplo n.º 9
0
        public void ValidationModelWorkWithFluentValidationAndShouldBeSuccessTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = true;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar
            .ForProvider(provider, "UTF_ValidationModelWorkWithFluentValidationAndShouldBeSuccessTest")
            .ForFluentValidator <GanglvToniValidator>()
            .Build();

            var validator = ValidationMe.Use("UTF_ValidationModelWorkWithFluentValidationAndShouldBeSuccessTest")
                            .Resolve <GanglvToni>();

            var model = new GanglvToni()
            {
                Name = "Good", Age = 11
            };

            validator.Verify(typeof(GanglvToni), model).IsValid.Should().BeTrue();
        }
Ejemplo n.º 10
0
        public void SimpleAndOps_WithAC_ShouldBeSuccess_Test()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_SimpleAndOps_WithAC_ShouldBeSuccess_Test")
            .ForType <NiceBoat>()
            .ForMember(x => x.Name).MaxLength(8).When(x => x.StartsWith("M")).And().MinLength(7).Unless(x => x.StartsWith("N"))
            .Build();

            var validator = ValidationMe.Use("UT_SimpleAndOps_WithAC_ShouldBeSuccess_Test").Resolve <NiceBoat>();

            var instance = new NiceBoat
            {
                Name       = "NiceBoat1", //9
                Length     = 1000,
                Width      = 30,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "*****@*****.**"
            };

            var result1 = validator.Verify(instance);

            result1.ShouldNotBeNull();

            result1.IsValid.ShouldBeTrue();
        }
        public void VerifyManyByDictionaryShouldBeSuccessTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = false;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UT_VerifyAnByDictionaryShouldBeSuccessTest")
            .ForType <NiceBoat>()
            .ForMember("Name").NotEmpty()
            .AndForMember("Length").GreaterThanOrEqual(0)
            .AndForMember("Width").GreaterThanOrEqual(0)
            .Build();

            var validator = ValidationMe.Use("UT_VerifyAnByDictionaryShouldBeSuccessTest").Resolve <NiceBoat>();

            var d = new Dictionary <string, object>
            {
                ["Name"]       = "NiceBoat1000",
                ["Length"]     = 1000,
                ["Width"]      = 30,
                ["CreateTime"] = DateTimeFactory.Create(2020, 12, 21),
                ["Email"]      = "*****@*****.**"
            };

            var result1 = validator.VerifyMany(d);

            result1.ShouldNotBeNull();

            result1.IsValid.ShouldBeTrue();
        }
Ejemplo n.º 12
0
        public void ValidationModelWorkWithFluentValidationAndShouldBeFailureTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = true;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar
            .ForProvider(provider, "UTF_ValidationModelWorkWithFluentValidationAndShouldBeFailureTest")
            .ForFluentValidator <GanglvToniValidator, GanglvToni>()
            .Build();

            var validator = ValidationMe.Use("UTF_ValidationModelWorkWithFluentValidationAndShouldBeFailureTest")
                            .Resolve <GanglvToni>();

            var model1 = new GanglvToni()
            {
                Name = "", Age = 11
            };
            var model2 = new GanglvToni()
            {
                Name = "11111111111", Age = 11
            };
            var model3 = new GanglvToni()
            {
                Name = "Good", Age = 9
            };
            var model4 = new GanglvToni()
            {
                Name = "", Age = -9
            };

            var r1 = validator.Verify(typeof(GanglvToni), model1);
            var r2 = validator.Verify(typeof(GanglvToni), model2);
            var r3 = validator.Verify(typeof(GanglvToni), model3);
            var r4 = validator.Verify(typeof(GanglvToni), model4);

            r1.IsValid.ShouldBeFalse();
            r2.IsValid.ShouldBeFalse();
            r3.IsValid.ShouldBeFalse();
            r4.IsValid.ShouldBeFalse();

            r1.MemberNames.Should().HaveCount(1);
            r2.MemberNames.Should().HaveCount(1);
            r3.MemberNames.Should().HaveCount(1);
            r4.MemberNames.Should().HaveCount(2);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Resolve a validator based on a given type.
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public Func <string, IValidator> RequiredResolveScoped(Type type) => scopeName => Required(ValidationMe.Use(scopeName).Resolve(type));
Ejemplo n.º 14
0
 /// <summary>
 /// Resolve a validator based on a given type and name.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 public Func <string, IValidator> ResolveScoped(Type type, string name) => scopeName => ValidationMe.Use(scopeName).Resolve(type, name);
Ejemplo n.º 15
0
        public void ValidationOneWorkWithFluentValidationAndShouldBeFailureTest()
        {
            var options = new ValidationOptions();

            options.AnnotationEnabled        = false;
            options.FailureIfInstanceIsNull  = true;
            options.FailureIfProjectNotMatch = false;
            options.CustomValidatorEnabled   = true;

            var provider = new ValidationProvider(ValidationProjectManager, VerifiableObjectResolver, options);

            ValidationRegistrar.ForProvider(provider, "UTF_ValidationOneWorkWithFluentValidationAndShouldBeFailureTest")
            .ForFluentValidator <GanglvToniValidator>()
            .Build();

            var validator = ValidationMe.Use("UTF_ValidationOneWorkWithFluentValidationAndShouldBeFailureTest")
                            .Resolve <GanglvToni>();

            var model1 = new GanglvToni()
            {
                Name = "", Age = 11
            };
            var model2 = new GanglvToni()
            {
                Name = "11111111111", Age = 11
            };
            var model3 = new GanglvToni()
            {
                Name = "Good", Age = 9
            };
            var model4 = new GanglvToni()
            {
                Name = "", Age = -9
            };

            validator.VerifyOne(x => x.Name, model1.Name).IsValid.Should().BeFalse();
            validator.VerifyOne(model1.Name, "Name").IsValid.Should().BeFalse();
            validator.VerifyOne(typeof(GanglvToni), model1.Name, "Name").IsValid.Should().BeFalse();

            validator.VerifyOne(x => x.Age, model1.Age).IsValid.Should().BeTrue();
            validator.VerifyOne(model1.Age, "Age").IsValid.Should().BeTrue();
            validator.VerifyOne(typeof(GanglvToni), model1.Age, "Age").IsValid.Should().BeTrue();

            validator.VerifyOne(x => x.Name, model2.Name).IsValid.Should().BeFalse();
            validator.VerifyOne(model2.Name, "Name").IsValid.Should().BeFalse();
            validator.VerifyOne(typeof(GanglvToni), model2.Name, "Name").IsValid.Should().BeFalse();

            validator.VerifyOne(x => x.Age, model2.Age).IsValid.Should().BeTrue();
            validator.VerifyOne(model2.Age, "Age").IsValid.Should().BeTrue();
            validator.VerifyOne(typeof(GanglvToni), model2.Age, "Age").IsValid.Should().BeTrue();


            validator.VerifyOne(x => x.Name, model3.Name).IsValid.Should().BeTrue();
            validator.VerifyOne(model3.Name, "Name").IsValid.Should().BeTrue();
            validator.VerifyOne(typeof(GanglvToni), model3.Name, "Name").IsValid.Should().BeTrue();

            validator.VerifyOne(x => x.Age, model3.Age).IsValid.Should().BeFalse();
            validator.VerifyOne(model3.Age, "Age").IsValid.Should().BeFalse();
            validator.VerifyOne(typeof(GanglvToni), model3.Age, "Age").IsValid.Should().BeFalse();


            validator.VerifyOne(x => x.Name, model4.Name).IsValid.Should().BeFalse();
            validator.VerifyOne(model4.Name, "Name").IsValid.Should().BeFalse();
            validator.VerifyOne(typeof(GanglvToni), model4.Name, "Name").IsValid.Should().BeFalse();

            validator.VerifyOne(x => x.Age, model4.Age).IsValid.Should().BeFalse();
            validator.VerifyOne(model4.Age, "Age").IsValid.Should().BeFalse();
            validator.VerifyOne(typeof(GanglvToni), model4.Age, "Age").IsValid.Should().BeFalse();
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Resolve a validator based on a given type.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public Func <string, IValidator <T> > ResolveScoped <T>() => scopeName => ValidationMe.Use(scopeName).Resolve <T>();
Ejemplo n.º 17
0
 /// <summary>
 /// Resolve a validator based on a given type and name.
 /// </summary>
 /// <param name="name"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public Func <string, IValidator <T> > RequiredResolveScoped <T>(string name) => scopeName => Required(ValidationMe.Use(scopeName).Resolve <T>(name));
Ejemplo n.º 18
0
 static ObjectVisitor()
 {
     CorrectnessVerifiableWallInitializer.InitializeAndPreheating();
     CorrectnessVerifiableWall.InitValidationProvider(ValidationMe.Use(CorrectnessVerifiableWall.GLOBAL_CORRECT_PROVIDER_KEY));
 }