示例#1
0
        protected override VerifyResult VerifyOneImpl(VerifiableMemberContext context)
        {
            if (context is null)
            {
                return(_options.ReturnNullReferenceOrSuccess());
            }

            // 如果 Value 为 String,对其进行验证
            if (context.Value is string str)
            {
                var attr = context.GetAttributes <LengthShould16Attribute>().FirstOrDefault();
                if (attr is null)
                {
                    return(VerifyResult.NullReferenceWith("There's no LengthShould16Attribute on this Member."));
                }
                if (str.Length == 16)
                {
                    return(VerifyResult.Success);
                }
                return(new VerifyResult(new VerifyFailure(context.MemberName, "Length should 16.", str)));
            }

            // 否则,如果 Value 不是基础类型(即 Value 为引用类型、结构等),对其进一步解析并验证
            if (!context.BasicTypeState())
            {
                return(VerifyImpl(context.ConvertToObjectContext()));
            }

            // 否则,认为其类型不是所期待的
            return(VerifyResult.UnexpectedTypeWith(context.MemberName));
        }
示例#2
0
 public static VerifyResult ReturnNullReferenceOrSuccess(this ValidationOptions options, string paramName)
 {
     if (options is null)
     {
         return(VerifyResult.Success);
     }
     return(options.FailureIfInstanceIsNull ? VerifyResult.NullReferenceWith(paramName) : VerifyResult.Success);
 }