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 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();
        }
        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 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();
        }
        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();
        }
예제 #6
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();
        }
예제 #7
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();
        }
예제 #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();
        }
        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();
        }
        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.
        }
예제 #11
0
        /// <summary>
        /// Get registrar for the given provider <br />
        /// 为给定的验证服务提供者程序提供一个注册器
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IValidationRegistrar ForProvider(IValidationProvider provider)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            ValidationMe.RegisterProvider(provider);

            return(new InternalValidationRegistrar(provider, RegisterMode.Hosted, ValidationProvider.MainName));
        }
        public virtual VerifyResult VerifyMany(IDictionary <string, object> keyValueCollections, bool withGlobalRules, string withGlobalProviderName = "")
        {
            if (ValidationHandler is null)
            {
                return(VerifyResult.Success);
            }
            var master = ValidationHandler.VerifyMany(_visitor.SourceType, keyValueCollections);
            var slave  = withGlobalRules
                ? ValidationMe.Resolve(_visitor.SourceType, withGlobalProviderName).VerifyMany(_visitor.SourceType, keyValueCollections)
                : VerifyResult.Success;

            return(VerifyResult.Merge(master, slave));
        }
        public virtual VerifyResult VerifyOne(string memberName, object value, bool withGlobalRules, string withGlobalProviderName = "")
        {
            if (ValidationHandler is null)
            {
                return(VerifyResult.Success);
            }
            var master = ValidationHandler.VerifyOne(_visitor.SourceType, value, memberName);
            var slave  = withGlobalRules
                ? ValidationMe.Resolve(_visitor.SourceType, withGlobalProviderName).VerifyOne(_visitor.SourceType, value, memberName)
                : VerifyResult.Success;

            return(VerifyResult.Merge(master, slave));
        }
        public VerifyResult Verify(bool withGlobalRules, string withGlobalProviderName = "")
        {
            if (ValidationHandler is null)
            {
                return(VerifyResult.Success);
            }
            var master = ValidationHandler.Verify(_visitor.SourceType, _visitor.Instance);
            var slave  = withGlobalRules
                ? ValidationMe.Resolve <T>(withGlobalProviderName).Verify(_visitor.Instance)
                : VerifyResult.Success;

            return(VerifyResult.Merge(master, slave));
        }
예제 #15
0
        /// <summary>
        /// Get registrar for the given provider and continue to register <br />
        /// 为给定的验证服务提供者程序提供一个注册器,并继续注册
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IValidationRegistrar Continue()
        {
            var provider = ValidationMe.ExposeValidationProvider();

            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var name = ((ICorrectProvider)provider).Name;
            var mode = ValidationProvider.IsDefault(name) ? RegisterMode.Direct : RegisterMode.Hosted;

            return(new InternalValidationRegistrar(provider, mode, name));
        }
        public VerifyResult VerifyOne(string memberName, bool withGlobalRules, string withGlobalProviderName = "")
        {
            if (ValidationHandler is null)
            {
                return(VerifyResult.Success);
            }
            var value  = _visitor.ExposeLazyMemberHandler().Value.GetValueObject(memberName);
            var master = ValidationHandler.VerifyOne(_visitor.SourceType, value, memberName);
            var slave  = withGlobalRules
                ? ValidationMe.Resolve <T>(withGlobalProviderName).VerifyOne(value, memberName)
                : VerifyResult.Success;

            return(VerifyResult.Merge(master, slave));
        }
예제 #17
0
        /// <summary>
        /// Get registrar for the given provider <br />
        /// 为给定的验证服务提供者程序提供一个注册器
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IValidationRegistrar ForProvider(IValidationProvider provider, string name)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = $"{provider.GetType().FullName}_{provider.GetHashCode()}";
            }

            ValidationMe.RegisterProvider(provider, name);

            return(new InternalValidationRegistrar(provider, RegisterMode.Hosted, name));
        }
예제 #18
0
        /// <summary>
        /// Get registrar for the given provider and continue to register <br />
        /// 为给定的验证服务提供者程序提供一个注册器,并继续注册
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static IValidationRegistrar Continue(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(Continue());
            }

            if (ValidationProvider.IsDefault(name))
            {
                return(DefaultRegistrar);
            }

            var provider = ValidationMe.ExposeValidationProvider(name);

            if (provider is null)
            {
                throw new InvalidOperationException($"There's no such name '{name}' for Validation Provider.");
            }

            name = ((ICorrectProvider)provider).Name;

            return(new InternalValidationRegistrar(provider, RegisterMode.Hosted, name));
        }
예제 #19
0
        internal static IValidationRegistrar ContinueOrDefault(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(Continue());
            }

            if (ValidationProvider.IsDefault(name))
            {
                return(DefaultRegistrar);
            }

            var provider = ValidationMe.ExposeValidationProvider(name);

            if (provider is null)
            {
                provider = ValidationMe.ExposeValidationProvider();
            }

            name = ((ICorrectProvider)provider).Name;

            return(new InternalValidationRegistrar(provider, RegisterMode.Hosted, name));
        }
예제 #20
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();
        }
예제 #21
0
 public void ReplaceOptions(ValidationOptions options) => ValidationMe.UpdateMainOptions(options.DeepCopy());
예제 #22
0
 public void UpdateOptions(Action <ValidationOptions> optionAct) => ValidationMe.UpdateMainOptions(optionAct);
예제 #23
0
 public ValidationOptions GetOptions(string providerName) => ((ICorrectProvider)ValidationMe.ExposeValidationProvider(providerName)).ExposeValidationOptions().DeepCopy();
예제 #24
0
 static ObjectVisitor()
 {
     CorrectnessVerifiableWallInitializer.InitializeAndPreheating();
     CorrectnessVerifiableWall.InitValidationProvider(ValidationMe.Use(CorrectnessVerifiableWall.GLOBAL_CORRECT_PROVIDER_KEY));
 }
예제 #25
0
 public void ReplaceOptions(ValidationOptions options, string providerName) => ValidationMe.UpdateOptions(providerName, options.DeepCopy());
예제 #26
0
 public void UpdateOptions(Action <ValidationOptions> optionAct, string providerName) => ValidationMe.UpdateOptions(providerName, optionAct);
예제 #27
0
 public ValidationOptions GetDefaultOptions() => ((ICorrectProvider)ValidationMe.ExposeDefaultProvider()).ExposeValidationOptions().DeepCopy();
예제 #28
0
 public void ReplaceDefaultOptions(ValidationOptions options) => ValidationMe.UpdateDefaultOptions(options.DeepCopy());