Esempio n. 1
0
        /// <summary>
        /// 全ReactivePropertyにValidationAttributeによる検証ロジックを設定する
        /// </summary>
        /// <param name="context"></param>
        public static ForValidation SetupReactiveProperties(this ForValidation context)
        {
            var propValues = context.GetTargetPropValues();

            foreach (var prop in propValues)
            {
                var type = prop.Value.GetType();
                if (!type.IsGenericType)
                {
                    continue;
                }

                var rpType = type.GetGenericTypeDefinition();
                if (rpType != typeof(ReactiveProperty <>))
                {
                    continue;
                }

                // MethodInfoの場合はこう?
                // https://ja.stackoverflow.com/questions/12801/c-type%E5%9E%8B%E3%81%A7%E6%8C%87%E5%AE%9A%E3%81%97%E3%81%9F%E5%9E%8B%E3%81%AB%E5%8B%95%E7%9A%84%E3%82%AD%E3%83%A3%E3%82%B9%E3%83%88%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF
                var typeParameter = type.GetGenericArguments()[0];
                var methodInfo    = SetValidationMethodInfos.First(a =>
                                                                   a.GetParameters()[1].ParameterType == typeof(PropertyInfo));
                var genericMethod = methodInfo.MakeGenericMethod(new[] { typeParameter });
                genericMethod.Invoke(null, new[] { prop.Value, context.GetTargetPropInfo(prop.Key) });
            }

            return(context);
        }
        public void WhenAllValuesSet_ValidationPasses()
        {
            var validator = new AtLeastOneNonNullPropertyValidationAttribute("FirstValue", "SecondValue");
            var instance = new ForValidation
                               {
                                   FirstValue = 1,
                                   SecondValue = 2
                               };
            var context = new ValidationContext(instance, null, null);
            ValidationResult result = validator.GetValidationResult(instance, context);

            Assert.Same(ValidationResult.Success, result);
        }
Esempio n. 3
0
        public void WhenAllValuesSet_ValidationPasses()
        {
            var validator = new AtLeastOneNonNullPropertyValidationAttribute("FirstValue", "SecondValue");
            var instance  = new ForValidation
            {
                FirstValue  = 1,
                SecondValue = 2
            };
            var context             = new ValidationContext(instance, null, null);
            ValidationResult result = validator.GetValidationResult(instance, context);

            Assert.Same(ValidationResult.Success, result);
        }
        public void WhenAllValuesNull_ValidationFails()
        {
            var validator = new AtLeastOneNonNullPropertyValidationAttribute("FirstValue", "SecondValue");
            var instance = new ForValidation
                               {
                                   FirstValue = null,
                                   SecondValue = null
                               };
            var context = new ValidationContext(instance, null, null);
            ValidationResult result = validator.GetValidationResult(instance, context);

            Assert.NotSame(ValidationResult.Success, result);
            Assert.NotNull(result.ErrorMessage);
            Assert.False(result.MemberNames.Except(new[] {"FirstValue", "SecondValue"}).Any());
        }
Esempio n. 5
0
        public void WhenAllValuesNull_ValidationFails()
        {
            var validator = new AtLeastOneNonNullPropertyValidationAttribute("FirstValue", "SecondValue");
            var instance  = new ForValidation
            {
                FirstValue  = null,
                SecondValue = null
            };
            var context             = new ValidationContext(instance, null, null);
            ValidationResult result = validator.GetValidationResult(instance, context);

            Assert.NotSame(ValidationResult.Success, result);
            Assert.NotNull(result.ErrorMessage);
            Assert.False(result.MemberNames.Except(new[] { "FirstValue", "SecondValue" }).Any());
        }
Esempio n. 6
0
        /// <summary>
        /// 全ReactivePropertyの検証エラーを返すヘルパ
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IObservable <IEnumerable <ValidationResult> > GetAllErrorsObservable(this ForValidation context)
        {
            var targets = context.GetTargetPropValues().Where(a =>
            {
                var type = a.Value.GetType();
                if (!type.IsGenericType)
                {
                    return(false);
                }

                var rpType = type.GetGenericTypeDefinition();
                if (rpType != typeof(ReactiveProperty <>))
                {
                    return(false);
                }

                return(true);
            })
                          .Select(a =>
            {
                var type       = a.Value.GetType();
                var prop       = type.GetProperty("ObserveErrorChanged");
                var observable = prop.GetValue(a.Value) as IObservable <System.Collections.IEnumerable>;
                if (observable == null)
                {
                    return(null);
                }
                return(observable.Select(e => e?.OfType <ValidationResult>()));
            })
                          .Where(a => a != null);

            return(Observable.CombineLatest(targets)
                   .Select(a => a.Where(e => e != null).SelectMany(e => e)));
        }