コード例 #1
0
        public void GenericTypeCreateObjectContractTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceBoat>();

            contract.ShouldNotBeNull();
            contract.Type.ShouldBe(typeof(NiceBoat));
            contract.ObjectKind.ShouldBe(VerifiableObjectKind.StructureType);
            contract.IsBasicType.ShouldBeFalse();

            //annotations/attributes - class level
            contract.Attributes.Count.ShouldBe(0);
            contract.IncludeAnnotations.ShouldBeTrue();

            //value-contract
            contract.GetMemberContracts().Count().ShouldBe(5);

            contract.GetMemberContract("Name").MemberName.ShouldBe("Name");
            contract.GetMemberContract("Length").MemberName.ShouldBe("Length");
            contract.GetMemberContract("Width").MemberName.ShouldBe("Width");
            contract.GetMemberContract("CreateTime").MemberName.ShouldBe("CreateTime");
            contract.GetMemberContract("Email").MemberName.ShouldBe("Email");

            contract.GetMemberContract(0).MemberName.ShouldBe("Name");
            contract.GetMemberContract(1).MemberName.ShouldBe("Length");
            contract.GetMemberContract(2).MemberName.ShouldBe("Width");
            contract.GetMemberContract(3).MemberName.ShouldBe("Email"); //Property first
            contract.GetMemberContract(4).MemberName.ShouldBe("CreateTime");
        }
コード例 #2
0
        public void GetValueFromInstanceTest()
        {
            var instance = new NiceBoat
            {
                Name       = "NiceBoat1000",
                Length     = 1000,
                Width      = 30,
                CreateTime = DateTimeFactory.Create(2020, 12, 21),
                Email      = "*****@*****.**"
            };

            var contract = VerifiableObjectContractManager.Resolve(typeof(NiceBoat));

            var value1 = contract.GetMemberContract("Name");
            var value2 = contract.GetMemberContract("Length");
            var value3 = contract.GetMemberContract("Width");
            var value4 = contract.GetMemberContract("CreateTime");
            var value5 = contract.GetMemberContract("Email");

            value1.GetValue(instance).ShouldBe("NiceBoat1000");
            value2.GetValue(instance).ShouldBe(1000);
            value3.GetValue(instance).ShouldBe(30);
            value4.GetValue(instance).ShouldBe(DateTimeFactory.Create(2020, 12, 21));
            value5.GetValue(instance).ShouldBe("*****@*****.**");
        }
コード例 #3
0
        public void GetValueFromDictionaryTest()
        {
            var d = new Dictionary <string, object>
            {
                ["Name"]       = "NiceBoat1000",
                ["Length"]     = 1000,
                ["Width"]      = 30,
                ["CreateTime"] = DateTimeFactory.Create(2020, 12, 21),
                ["Email"]      = "*****@*****.**"
            };

            var contract = VerifiableObjectContractManager.Resolve(typeof(NiceBoat));

            var value1 = contract.GetMemberContract("Name");
            var value2 = contract.GetMemberContract("Length");
            var value3 = contract.GetMemberContract("Width");
            var value4 = contract.GetMemberContract("CreateTime");
            var value5 = contract.GetMemberContract("Email");

            value1.GetValue(d).ShouldBe("NiceBoat1000");
            value2.GetValue(d).ShouldBe(1000);
            value3.GetValue(d).ShouldBe(30);
            value4.GetValue(d).ShouldBe(DateTimeFactory.Create(2020, 12, 21));
            value5.GetValue(d).ShouldBe("*****@*****.**");
        }
コード例 #4
0
        protected FakeValidationStrategy()
        {
            SourceType = typeof(T);

            _memberValueRuleBuilders = new List <FakeValueRuleBuilder <T> >();
            _contract = VerifiableObjectContractManager.Resolve <T>();
        }
コード例 #5
0
        protected FakeValidationStrategy(Type type)
        {
            SourceType = type ?? throw new ArgumentNullException(nameof(type));

            _memberValueRuleBuilders = new List <FakeValueRuleBuilder>();
            _contract = VerifiableObjectContractManager.Resolve(type);
        }
コード例 #6
0
        public void AunnCoo_RequireGuidToken_ShouldBeValid()
        {
            var model = new AunnCoo {
                ThisGuid = Guid.NewGuid(), ThatGuid = Guid.NewGuid(), OtherInfo = Guid.NewGuid()
            };

            var context   = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member1   = context.GetValue("ThisGuid");
            var member2   = context.GetValue("ThatGuid");
            var member3   = context.GetValue("OtherInfo");
            var contract1 = member1.ExposeContract();
            var contract2 = member2.ExposeContract();
            var contract3 = member3.ExposeContract();

            var token1 = new ValueRequiredGuidToken(contract1);
            var token2 = new ValueRequiredGuidToken(contract2);
            var token3 = new ValueRequiredGuidToken(contract3);

            token1.Valid(context).IsSuccess.ShouldBeTrue();
            token1.Valid(member1).IsSuccess.ShouldBeTrue();

            token2.Valid(context).IsSuccess.ShouldBeTrue();
            token2.Valid(member2).IsSuccess.ShouldBeTrue();

            token3.Valid(context).IsSuccess.ShouldBeTrue();
            token3.Valid(member3).IsSuccess.ShouldBeTrue();
        }
コード例 #7
0
        public void StructureTypeWithValueToObjectContextTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceStruct>();
            var @struct  = new NiceStruct("NiceBoat1000", 100);
            var context1 = contract.WithInstance(@struct);
            var context2 = contract.WithInstance(@struct, "NiceStruct");

            context1.InstanceName.ShouldBe("Instance");
            context2.InstanceName.ShouldBe("NiceStruct");

            context1.GetValues().Count().ShouldBe(2);
            context2.GetValues().Count().ShouldBe(2);

            context1.GetValue(0).MemberName.ShouldBe("Name");
            context2.GetValue(0).MemberName.ShouldBe("Name");

            context1.GetValue(1).MemberName.ShouldBe("Age");
            context2.GetValue(1).MemberName.ShouldBe("Age");

            context1.GetValue("Name").Value.ShouldBe("NiceBoat1000");
            context2.GetValue("Name").Value.ShouldBe("NiceBoat1000");

            context1.GetValue("Age").Value.ShouldBe(100);
            context2.GetValue("Age").Value.ShouldBe(100);
        }
コード例 #8
0
        public void AunnCoo_Discount_Nullable_ScalePrecisionToken_ShouldBeInvalid()
        {
            var model = new AunnCoo {
                NullableDiscount = 123.456778M
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("NullableDiscount");
            var contract = member.ExposeContract();

            var token = new ValueScalePrecisionToken(contract, 2, 4);

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();

            model = new AunnCoo {
                NullableDiscount = 12.3414M
            };
            context = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member  = context.GetValue("NullableDiscount");

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();

            model = new AunnCoo {
                NullableDiscount = 12.344M
            };
            context = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member  = context.GetValue("NullableDiscount");

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();

            model = new AunnCoo {
                NullableDiscount = 1.344M
            };
            context = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member  = context.GetValue("NullableDiscount");

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();

            model = new AunnCoo {
                NullableDiscount = 156.3M
            };
            context = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member  = context.GetValue("NullableDiscount");

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();

            model = new AunnCoo {
                NullableDiscount = 65.430M
            };
            context = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member  = context.GetValue("NullableDiscount");

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();
        }
コード例 #9
0
        public void RecordTypeWithValueToObjectContextTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceRecord>();
            var record   = new NiceRecord {
                Name = "NiceBoat1000", Age = 100
            };
            var context1 = contract.WithInstance(record);
            var context2 = contract.WithInstance(record, "NiceRecord");

            context1.InstanceName.ShouldBe("Instance");
            context2.InstanceName.ShouldBe("NiceRecord");

            context1.GetValues().Count().ShouldBe(2);
            context2.GetValues().Count().ShouldBe(2);

            context1.GetValue(0).MemberName.ShouldBe("Name");
            context2.GetValue(0).MemberName.ShouldBe("Name");

            context1.GetValue(1).MemberName.ShouldBe("Age");
            context2.GetValue(1).MemberName.ShouldBe("Age");

            context1.GetValue("Name").Value.ShouldBe("NiceBoat1000");
            context2.GetValue("Name").Value.ShouldBe("NiceBoat1000");

            context1.GetValue("Age").Value.ShouldBe(100);
            context2.GetValue("Age").Value.ShouldBe(100);
        }
コード例 #10
0
        public void AunnCoo_Func_For_GenericFuncToken_And_ShouldBeValid()
        {
            var model = new AunnCoo {
                AunnClass = AunnEnum.One
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("AunnClass");
            var contract = member.ExposeContract();
            Func <AunnEnum, CustomVerifyResult> condition = obj =>
            {
                if (obj is AunnEnum a && a == AunnEnum.One)
                {
                    return new CustomVerifyResult {
                               VerifyResult = true
                    }
                }
                ;
                return(new CustomVerifyResult {
                    VerifyResult = false
                });
            };

            var token = new ValueFuncToken <AunnEnum>(contract, condition);

            token.Valid(context).IsSuccess.ShouldBeTrue();
            token.Valid(member).IsSuccess.ShouldBeTrue();
        }
コード例 #11
0
        public void AunnCoo_String_GenericStringEnumToken_Test()
        {
            var model = new AunnCoo {
                Name = "One"
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("Name");
            var contract = member.ExposeContract();

            var token1 = new ValueStringEnumToken <AunnEnum>(contract, false);
            var token2 = new ValueStringEnumToken <AunnEnum>(contract, true);

            token1.Valid(context).IsSuccess.ShouldBeTrue();
            token1.Valid(member).IsSuccess.ShouldBeTrue();

            token2.Valid(context).IsSuccess.ShouldBeTrue();
            token2.Valid(member).IsSuccess.ShouldBeTrue();

            model.Name = "one";
            context    = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            member     = context.GetValue("Name");

            token1.Valid(context).IsSuccess.ShouldBeTrue();
            token1.Valid(member).IsSuccess.ShouldBeTrue();

            token2.Valid(context).IsSuccess.ShouldBeFalse();
            token2.Valid(member).IsSuccess.ShouldBeFalse();
        }
コード例 #12
0
        public void AunnCoo_RequireBooleanToken_ShouldBeValid()
        {
            var model = new AunnCoo {
                IsThisOk = true, IsThatOk = true, OtherInfo = true
            };

            var context   = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member1   = context.GetValue("IsThisOk");
            var member2   = context.GetValue("IsThatOk");
            var member3   = context.GetValue("OtherInfo");
            var contract1 = member1.ExposeContract();
            var contract2 = member2.ExposeContract();
            var contract3 = member3.ExposeContract();

            var token1 = new ValueRequiredBooleanToken(contract1);
            var token2 = new ValueRequiredBooleanToken(contract2);
            var token3 = new ValueRequiredBooleanToken(contract3);

            token1.Valid(context).IsSuccess.ShouldBeTrue();
            token1.Valid(member1).IsSuccess.ShouldBeTrue();

            token2.Valid(context).IsSuccess.ShouldBeTrue();
            token2.Valid(member2).IsSuccess.ShouldBeTrue();

            token3.Valid(context).IsSuccess.ShouldBeTrue();
            token3.Valid(member3).IsSuccess.ShouldBeTrue();
        }
コード例 #13
0
 public FluentValidationRegistrar(Type type, string name, IValidationRegistrar parentRegistrar)
 {
     _name                     = name;
     SourceType                = type;
     _parentRegistrar          = parentRegistrar ?? throw new ArgumentNullException(nameof(parentRegistrar));
     _verifiableObjectContract = VerifiableObjectContractManager.Resolve(type);
     Rules                     = new List <CorrectValueRule>();
 }
コード例 #14
0
        public void DictionaryWithNameAfterCreatedObjectContextTest()
        {
            var d = new Dictionary <string, object>
            {
                ["Name"]       = "NiceBoat1000",
                ["Length"]     = 1000,
                ["Width"]      = 30,
                ["CreateTime"] = DateTimeFactory.Create(2020, 12, 21),
                ["Email"]      = "*****@*****.**"
            };

            var context = VerifiableObjectContractManager.Resolve <NiceBoat>().WithDictionary(d, "nice_boat_2000");

            context.ShouldNotBeNull();
            context.Type.ShouldBe(typeof(NiceBoat));
            context.ObjectKind.ShouldBe(VerifiableObjectKind.StructureType);
            context.IsBasicType().ShouldBeFalse();
            context.Instance.ShouldBeNull();
            context.KeyValueCollection.ShouldBe(d);
            context.InstanceName.ShouldBe("nice_boat_2000");

            //annotations/attributes - class level
            context.IncludeAnnotations.ShouldBeTrue();
            context.Attributes.Count.ShouldBe(0);

            //member/value-contract
            context.GetMembers().Count().ShouldBe(5);

            context.GetMember("Name").MemberName.ShouldBe("Name");
            context.GetMember("Length").MemberName.ShouldBe("Length");
            context.GetMember("Width").MemberName.ShouldBe("Width");
            context.GetMember("CreateTime").MemberName.ShouldBe("CreateTime");
            context.GetMember("Email").MemberName.ShouldBe("Email");

            context.GetMember(0).MemberName.ShouldBe("Name");
            context.GetMember(1).MemberName.ShouldBe("Length");
            context.GetMember(2).MemberName.ShouldBe("Width");
            context.GetMember(3).MemberName.ShouldBe("Email"); //Property first
            context.GetMember(4).MemberName.ShouldBe("CreateTime");

            //value/value-context
            context.GetValues().Count().ShouldBe(5);

            context.GetValue("Name").MemberName.ShouldBe("Name");
            context.GetValue("Length").MemberName.ShouldBe("Length");
            context.GetValue("Width").MemberName.ShouldBe("Width");
            context.GetValue("CreateTime").MemberName.ShouldBe("CreateTime");
            context.GetValue("Email").MemberName.ShouldBe("Email");

            context.GetValue(0).MemberName.ShouldBe("Name");
            context.GetValue(1).MemberName.ShouldBe("Length");
            context.GetValue(2).MemberName.ShouldBe("Width");
            context.GetValue(3).MemberName.ShouldBe("Email"); //Property first
            context.GetValue(4).MemberName.ShouldBe("CreateTime");

            context.GetMemberMap().Count.ShouldBe(5);
            context.GetValueMap().Count.ShouldBe(5);
        }
コード例 #15
0
        public void BasicTypeToObjectContractTest()
        {
            var int16Contract    = VerifiableObjectContractManager.Resolve <Int16>();
            var int32Contract    = VerifiableObjectContractManager.Resolve <Int32>();
            var int64Contract    = VerifiableObjectContractManager.Resolve <Int64>();
            var single32Contract = VerifiableObjectContractManager.Resolve <float>();
            var single64Contract = VerifiableObjectContractManager.Resolve <double>();

            int16Contract.ShouldNotBeNull();
            int32Contract.ShouldNotBeNull();
            int64Contract.ShouldNotBeNull();
            single32Contract.ShouldNotBeNull();
            single64Contract.ShouldNotBeNull();

            int16Contract.Type.ShouldBe(typeof(Int16));
            int32Contract.Type.ShouldBe(typeof(Int32));
            int64Contract.Type.ShouldBe(typeof(Int64));
            single32Contract.Type.ShouldBe(typeof(float));
            single64Contract.Type.ShouldBe(typeof(double));

            int16Contract.IncludeAnnotations.ShouldBeFalse();
            int32Contract.IncludeAnnotations.ShouldBeFalse();
            int64Contract.IncludeAnnotations.ShouldBeFalse();
            single32Contract.IncludeAnnotations.ShouldBeFalse();
            single64Contract.IncludeAnnotations.ShouldBeFalse();

            int16Contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);
            int32Contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);
            int64Contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);
            single32Contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);
            single64Contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);

            int16Contract.IsBasicType.ShouldBeTrue();
            int32Contract.IsBasicType.ShouldBeTrue();
            int64Contract.IsBasicType.ShouldBeTrue();
            single32Contract.IsBasicType.ShouldBeTrue();
            single64Contract.IsBasicType.ShouldBeTrue();

            int16Contract.GetMemberContracts().Should().HaveCount(1);
            int32Contract.GetMemberContracts().Should().HaveCount(1);
            int64Contract.GetMemberContracts().Should().HaveCount(1);
            single32Contract.GetMemberContracts().Should().HaveCount(1);
            single64Contract.GetMemberContracts().Should().HaveCount(1);

            int16Contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(Int16));
            int32Contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(Int32));
            int64Contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(Int64));
            single32Contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(float));
            single64Contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(double));

            int16Contract.GetMemberContract(0).MemberType.ShouldBe(typeof(Int16));
            int32Contract.GetMemberContract(0).MemberType.ShouldBe(typeof(Int32));
            int64Contract.GetMemberContract(0).MemberType.ShouldBe(typeof(Int64));
            single32Contract.GetMemberContract(0).MemberType.ShouldBe(typeof(float));
            single64Contract.GetMemberContract(0).MemberType.ShouldBe(typeof(double));
        }
コード例 #16
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="declaringType"></param>
        /// <param name="memberValue"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        public virtual VerifyResult VerifyOne(Type declaringType, object memberValue, string memberName)
        {
            var memberContract = VerifiableObjectContractManager.Resolve(declaringType)?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract);

            return(VerifyOneImpl(memberContext));
        }
コード例 #17
0
        public void GenericTypeCreateObjectValueContractTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceBoat>();

            contract.ShouldNotBeNull();
            contract.Type.ShouldBe(typeof(NiceBoat));
            contract.ObjectKind.ShouldBe(VerifiableObjectKind.StructureType);
            contract.IsBasicType.ShouldBeFalse();

            //value-contract
            contract.GetMemberContracts().Count().ShouldBe(5);

            var value1 = contract.GetMemberContract("Name");
            var value2 = contract.GetMemberContract("Length");
            var value3 = contract.GetMemberContract("Width");
            var value4 = contract.GetMemberContract("CreateTime");
            var value5 = contract.GetMemberContract("Email");

            value1.MemberName.ShouldBe("Name");
            value1.MemberType.ShouldBe(TypeClass.StringClazz);
            value1.DeclaringType.ShouldBe(typeof(NiceBoat));
            value1.MemberKind.ShouldBe(VerifiableMemberKind.Property);
            value1.IsBasicType.ShouldBeTrue();
            value1.IncludeAnnotations.ShouldBeTrue();

            value2.MemberName.ShouldBe("Length");
            value2.MemberType.ShouldBe(TypeClass.LongClazz);
            value2.DeclaringType.ShouldBe(typeof(NiceBoat));
            value2.MemberKind.ShouldBe(VerifiableMemberKind.Property);
            value2.IsBasicType.ShouldBeTrue();
            value2.IncludeAnnotations.ShouldBeTrue();

            value3.MemberName.ShouldBe("Width");
            value3.MemberType.ShouldBe(TypeClass.LongClazz);
            value3.DeclaringType.ShouldBe(typeof(NiceBoat));
            value3.MemberKind.ShouldBe(VerifiableMemberKind.Property);
            value3.IsBasicType.ShouldBeTrue();
            value3.IncludeAnnotations.ShouldBeTrue();

            value4.MemberName.ShouldBe("CreateTime");
            value4.MemberType.ShouldBe(TypeClass.DateTimeClazz);
            value4.DeclaringType.ShouldBe(typeof(NiceBoat));
            value4.MemberKind.ShouldBe(VerifiableMemberKind.Field);
            value4.IsBasicType.ShouldBeTrue();
            value4.IncludeAnnotations.ShouldBeTrue();

            value5.MemberName.ShouldBe("Email");
            value5.MemberType.ShouldBe(TypeClass.StringClazz);
            value5.DeclaringType.ShouldBe(typeof(NiceBoat));
            value5.MemberKind.ShouldBe(VerifiableMemberKind.Property);
            value5.IsBasicType.ShouldBeTrue();
            value5.IncludeAnnotations.ShouldBeTrue();
        }
コード例 #18
0
        public void StructureTypeToObjectContractTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceStruct>();

            contract.ShouldNotBeNull();
            contract.Type.ShouldBe(typeof(NiceStruct));
            contract.IncludeAnnotations.ShouldBeFalse();
            contract.ObjectKind.ShouldBe(VerifiableObjectKind.StructureType);
            contract.IsBasicType.ShouldBeFalse();

            contract.GetMemberContracts().Count().ShouldBe(2);
        }
コード例 #19
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="memberValue"></param>
        /// <typeparam name="TVal"></typeparam>
        /// <returns></returns>
        public virtual VerifyResult VerifyOne <TVal>(Expression <Func <T, TVal> > expression, TVal memberValue)
        {
            var memberName     = PropertySelector.GetPropertyName(expression);
            var memberContract = VerifiableObjectContractManager.Resolve <T>()?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract);

            return(VerifyOneImpl(memberContext));
        }
コード例 #20
0
        public void AunnCoo_Null_For_NullToken_And_ShouldBeValid()
        {
            var model = new AunnCoo();

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("OtherInfo");
            var contract = member.ExposeContract();

            var token = new ValueNullToken(contract);

            token.Valid(context).IsSuccess.ShouldBeTrue();
            token.Valid(member).IsSuccess.ShouldBeTrue();
        }
コード例 #21
0
        public void AunnCoo_String_For_NotEmptyToken_And_ShouldBeValid()
        {
            var model = new AunnCoo();

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("Name");
            var contract = member.ExposeContract();

            var token = new ValueNotEmptyToken(contract);

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();
        }
コード例 #22
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="memberValue"></param>
        /// <param name="memberName"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        public virtual VerifyResult VerifyOneWithInstance(object memberValue, string memberName, T instance)
        {
            var parentContract = VerifiableObjectContractManager.Resolve <T>();
            var memberContract = parentContract?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract, parentContract.WithInstance(instance));

            return(VerifyOneImpl(memberContext));
        }
コード例 #23
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="declaringType"></param>
        /// <param name="memberValue"></param>
        /// <param name="memberName"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        VerifyResult IValidator.VerifyOneWithInstance(Type declaringType, object memberValue, string memberName, object instance)
        {
            var parentContract = VerifiableObjectContractManager.Resolve(declaringType);
            var memberContract = parentContract?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract, parentContract.WithInstance(instance));

            return(VerifyOneImpl(memberContext));
        }
コード例 #24
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="declaringType"></param>
        /// <param name="memberValue"></param>
        /// <param name="memberName"></param>
        /// <param name="keyValueCollection"></param>
        /// <returns></returns>
        public virtual VerifyResult VerifyOneWithDictionary(Type declaringType, object memberValue, string memberName, IDictionary <string, object> keyValueCollection)
        {
            var parentContract = VerifiableObjectContractManager.Resolve(declaringType);
            var memberContract = parentContract?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract, parentContract.WithDictionary(keyValueCollection));

            return(VerifyOneImpl(memberContext));
        }
コード例 #25
0
        public void EnumTypeToObjectContractTest()
        {
            var contract = VerifiableObjectContractManager.Resolve <NiceEnum>();

            contract.ShouldNotBeNull();
            contract.Type.ShouldBe(typeof(NiceEnum));
            contract.IncludeAnnotations.ShouldBeFalse();
            contract.ObjectKind.ShouldBe(VerifiableObjectKind.BasicType);
            contract.IsBasicType.ShouldBeTrue();

            contract.GetMemberContracts().Count().ShouldBe(1);
            contract.GetMemberContract(VerifiableMemberContract.BASIC_TYPE).MemberType.ShouldBe(typeof(NiceEnum));
            contract.GetMemberContract(0).MemberType.ShouldBe(typeof(NiceEnum));
        }
コード例 #26
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="memberValue"></param>
        /// <param name="keyValueCollection"></param>
        /// <typeparam name="TVal"></typeparam>
        /// <returns></returns>
        public virtual VerifyResult VerifyOneWithDictionary <TVal>(Expression <Func <T, TVal> > expression, TVal memberValue, IDictionary <string, object> keyValueCollection)
        {
            var parentContract = VerifiableObjectContractManager.Resolve <T>();
            var memberName     = PropertySelector.GetPropertyName(expression);
            var memberContract = parentContract?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            var memberContext = VerifiableMemberContext.Create(memberValue, memberContract, parentContract.WithDictionary(keyValueCollection));

            return(VerifyOneImpl(memberContext));
        }
コード例 #27
0
        /// <summary>
        /// Verify a member of the entity. <br />
        /// 成员验证入口
        /// </summary>
        /// <param name="declaringType"></param>
        /// <param name="memberValue"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        VerifyResult IValidator.VerifyOne(Type declaringType, object memberValue, string memberName)
        {
            if (memberValue is null)
            {
                return(_options.ReturnNullReferenceOrSuccess());
            }
            var memberContract = VerifiableObjectContractManager.Resolve(declaringType)?.GetMemberContract(memberName);

            if (memberContract is null)
            {
                return(VerifyResult.MemberIsNotExists(memberName));
            }
            return(VerifyOneInternal(VerifiableMemberContext.Create(memberValue, memberContract)));
        }
コード例 #28
0
        public void AunnCoo_ValueType_For_GreaterThanToken_And_ShouldBeInvalid()
        {
            var model = new AunnCoo {
                Age = 10
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("Age");
            var contract = member.ExposeContract();

            var token = new ValueGreaterThanToken(contract, 10);

            token.Valid(context).IsSuccess.ShouldBeFalse();
            token.Valid(member).IsSuccess.ShouldBeFalse();
        }
コード例 #29
0
        public void AunnCoo_Object_For_GenericEqualToken_And_ShouldBeValid()
        {
            var model = new AunnCoo {
                OtherInfo = Haha
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("OtherInfo");
            var contract = member.ExposeContract();

            var token = new ValueEqualToken <object>(contract, Haha, null);

            token.Valid(context).IsSuccess.ShouldBeTrue();
            token.Valid(member).IsSuccess.ShouldBeTrue();
        }
コード例 #30
0
        public void AunnCoo_ValueType_For_GenericEqualToken_And_ShouldBeValid()
        {
            var model = new AunnCoo {
                Age = 10
            };

            var context  = VerifiableObjectContractManager.Resolve <AunnCoo>().WithInstance(model);
            var member   = context.GetValue("Age");
            var contract = member.ExposeContract();

            var token = new ValueEqualToken <int>(contract, 10, null);

            token.Valid(context).IsSuccess.ShouldBeTrue();
            token.Valid(member).IsSuccess.ShouldBeTrue();
        }