コード例 #1
0
        private bool ValidateModifiableEntity(ValidationContext validationContext, ModifiableEntity mod)
        {
            if (mod is Entity && validationContext.Visited.Contains(mod))
            {
                return(true);
            }

            validationContext.Visited.Add(mod);

            bool          isValid       = true;
            PropertyScope propertyScope = new PropertyScope();

            validationContext.KeyBuilders.Push(propertyScope);

            var entity = mod as Entity;

            using (entity == null ? null : entity.Mixins.OfType <CorruptMixin>().Any(c => c.Corrupt) ? Corruption.AllowScope() : Corruption.DenyScope())
            {
                foreach (var kvp in PropertyConverter.GetPropertyConverters(mod.GetType()))
                {
                    if (kvp.Value.AvoidValidate)
                    {
                        continue;
                    }

                    propertyScope.PropertyName = kvp.Key;
                    if (SignumValidate(validationContext, kvp.Value.GetValue(mod)) ?? true)
                    {
                        isValid = false;
                    }

                    string error = kvp.Value.PropertyValidator.PropertyCheck(mod);

                    if (error != null)
                    {
                        string key = CalculateKey(validationContext);
                        if (validationContext.ModelState.IsValidField(key))
                        {
                            isValid = false;
                            validationContext.ModelState.AddModelError(key, error);
                        }
                    }
                }
            }

            if (entity != null && entity.Mixins.Any())
            {
                propertyScope.PropertyName = "mixins";
                PropertyScope mixinScope = new PropertyScope();
                validationContext.KeyBuilders.Push(mixinScope);
                foreach (var mixin in entity.Mixins)
                {
                    mixinScope.PropertyName = mixin.GetType().Name;
                    if (!ValidateModifiableEntity(validationContext, mixin))
                    {
                        isValid = false;
                    }
                }
                validationContext.KeyBuilders.Pop();
            }

            validationContext.KeyBuilders.Pop();

            validationContext.Visited.Remove(mod);
            return(isValid);
        }
コード例 #2
0
        private bool ValidateModifiableEntity(ModifiableEntity mod)
        {
            using (Validator.ModelBinderScope())
            {
                bool isValid = true;

                var entity = mod as Entity;
                using (entity == null ? null : entity.Mixins.OfType <CorruptMixin>().Any(c => c.Corrupt) ? Corruption.AllowScope() : Corruption.DenyScope())
                {
                    foreach (var kvp in PropertyConverter.GetPropertyConverters(mod.GetType()))
                    {
                        if (kvp.Value.AvoidValidate)
                        {
                            continue;
                        }

                        string?error = kvp.Value.PropertyValidator !.PropertyCheck(mod);
                        if (error != null)
                        {
                            isValid = false;
                            ModelState.AddModelError(this.Key + "." + kvp.Key, error);
                        }

                        var val = kvp.Value.GetValue !(mod);
                        if (val != null && this.CurrentPath.Push(val))
                        {
                            using (StateManager.Recurse(this, this.Key + "." + kvp.Key, null, val, null))
                            {
                                if (this.SignumValidate() == false)
                                {
                                    isValid = false;
                                }
                            }
                        }
                    }
                }

                if (entity != null && entity.Mixins.Any())
                {
                    foreach (var mixin in entity.Mixins)
                    {
                        if (this.CurrentPath.Push(mixin))
                        {
                            using (StateManager.Recurse(this, "mixins[" + mixin.GetType().Name + "].element", null, mixin, null))
                            {
                                isValid &= ValidateModifiableEntity(mixin);
                            }
                        }
                    }
                }

                return(isValid);
            }
        }