/// <summary>
        /// Verify the entire entity <br />
        /// 验证入口
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public VerifyResult Verify(T instance)
        {
            if (instance is null)
            {
                return(_options.ReturnNullReferenceOrSuccess());
            }

            VerifyResult result1 = null, result2 = null, result3 = null;
            var          context = _objectResolver.Resolve(instance);

            if (_projectManager.TryResolve(_type, _name, out var project))
            {
                result1 = project.Verify(context);
            }

            if (_options.CustomValidatorEnabled)
            {
                result2 = CorrectEngine.ValidViaCustomValidators(context, _customValidatorManager.ResolveAll());
            }

            if (_options.AnnotationEnabled)
            {
                result3 = _annotationValidator.Verify(context);
            }

            if (result1 is null && result2 is null && result3 is null)
            {
                return(_options.ReturnUnregisterProjectForSuchTypeOrSuccess());
            }

            return(result1 is null
                ? VerifyResult.MakeTogether(result2, result3)
                : VerifyResult.Merge(result1, result2, result3));
        }
        /// <summary>
        /// Verify each member in the non-strongly typed dictionary. <br />
        /// 字典验证入口
        /// </summary>
        /// <param name="keyValueCollections"></param>
        /// <returns></returns>
        public VerifyResult VerifyMany(IDictionary <string, object> keyValueCollections)
        {
            if (keyValueCollections is null)
            {
                return(_options.ReturnNullReferenceOrSuccess());
            }

            VerifyResult result1 = null, result2 = null, result3 = null;
            var          context = _objectResolver.Resolve <T>(keyValueCollections);

            if (_projectManager.TryResolve(_type, _name, out var project))
            {
                result1 = project.VerifyMany(context.GetValueMap());
            }

            if (_options.CustomValidatorEnabled)
            {
                result2 = CorrectEngine.ValidViaCustomValidators(context.GetValueMap(), _customValidatorManager.ResolveAll());
            }

            if (_options.AnnotationEnabled)
            {
                result3 = _annotationValidator.Verify(context);
            }

            if (result1 is null && result2 is null && result3 is null)
            {
                return(_options.ReturnUnregisterProjectForSuchTypeOrSuccess());
            }

            return(result1 is null
                ? VerifyResult.MakeTogether(result2, result3)
                : VerifyResult.Merge(result1, result2, result3));
        }
Beispiel #3
0
        public static VerifyResult ValidViaCustomValidators(IDictionary <string, VerifiableMemberContext> keyValueCollections, IEnumerable <CustomValidator> validators)
        {
            var slaveResults = new List <VerifyResult>();

            foreach (var context in keyValueCollections.Values)
            {
                slaveResults.AddRange(validators.Select(validator => validator.VerifyOneViaContext(context)));
            }

            return(VerifyResult.MakeTogether(slaveResults));
        }
Beispiel #4
0
        protected override VerifyResult VerifyImpl(VerifiableObjectContext context)
        {
            if (context is null)
            {
                return(_options.ReturnNullReferenceOrSuccess());
            }

            List <VerifyResult> results = new();
            var values = context.GetValues();

            foreach (var value in values)
            {
                // 如果 Value 为 String,对其进行验证
                if (value.Value is string str)
                {
                    var attr = value.GetAttributes <LengthShould16Attribute>().FirstOrDefault();

                    if (attr is null)
                    {
                        continue;
                    }

                    if (str.Length != 16)
                    {
                        results.Add(new VerifyResult(new VerifyFailure(value.MemberName, "Length should 16.", str)));
                    }
                }
                // 否则,如果 Value 不是基础类型(即 Value 为引用类型、结构等),对其进一步解析并验证
                else if (!value.BasicTypeState())
                {
                    results.Add(VerifyImpl(value.ConvertToObjectContext()));
                }
            }

            return(VerifyResult.MakeTogether(results));
        }
        private VerifyResult VerifyOneInternal(VerifiableMemberContext memberContext)
        {
            VerifyResult result1 = null, result2 = null, result3 = null;

            if (_projectManager.TryResolve(_type, _name, out var project))
            {
                result1 = project.VerifyOne(memberContext);
            }
            if (_options.CustomValidatorEnabled)
            {
                result2 = CorrectEngine.ValidViaCustomValidators(memberContext, _customValidatorManager.ResolveAll());
            }
            if (_options.AnnotationEnabled)
            {
                result3 = _annotationValidator.VerifyOne(memberContext);
            }
            if (result1 is null && result2 is null && result3 is null)
            {
                return(_options.ReturnUnregisterProjectForSuchTypeOrSuccess());
            }
            return(result1 is null
                ? VerifyResult.MakeTogether(result2, result3)
                : VerifyResult.Merge(result1, result2, result3));
        }
Beispiel #6
0
 public static VerifyResult ValidViaCustomValidators(VerifiableMemberContext context, IEnumerable <CustomValidator> validators)
 {
     return(VerifyResult.MakeTogether(validators.Select(validator => validator.VerifyOneViaContext(context)).ToList()));
 }