public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm, object fieldValue) { if ((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { if (fieldConfig.Validators.IsNullOrEmpty()) { if (!(rec is ExpandoObject)) { ChoValidator.ValidateFor(fieldValue, fn, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(rec.GetType(), fn)).ToArray()); } } else { ChoValidator.ValidateFor(fieldValue, fn, fieldConfig.Validators); } } }
protected virtual void LoadNCacheMembers(IEnumerable <ChoRecordFieldConfiguration> fcs) { if (!IsDynamicObject) { object defaultValue = null; object fallbackValue = null; foreach (var fc in fcs) { if (!PDDict.ContainsKey(fc.Name)) { continue; } fc.PD = PDDict[fc.Name]; fc.PI = PIDict[fc.Name]; //Load default value defaultValue = ChoType.GetRawDefaultValue(PDDict[fc.Name]); if (defaultValue != null) { fc.DefaultValue = defaultValue; fc.IsDefaultValueSpecified = true; } //Load fallback value fallbackValue = ChoType.GetRawFallbackValue(PDDict[fc.Name]); if (fallbackValue != null) { fc.FallbackValue = fallbackValue; fc.IsFallbackValueSpecified = true; } //Load Converters fc.PropConverters = ChoTypeDescriptor.GetTypeConverters(fc.PI); fc.PropConverterParams = ChoTypeDescriptor.GetTypeConverterParams(fc.PI); } PropertyNames = PDDict.Keys.ToArray(); } //Validators HasConfigValidators = (from fc in fcs where fc.Validators != null select fc).FirstOrDefault() != null; if (!HasConfigValidators) { if (!IsDynamicObject) { foreach (var fc in fcs) { if (!PDDict.ContainsKey(fc.Name)) { continue; } fc.Validators = ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(fc.PD).ToArray(); } } } ValDict = (from fc in fcs select new KeyValuePair <string, ValidationAttribute[]>(fc.Name, fc.Validators)).GroupBy(i => i.Key).Select(g => g.First()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); }
public static bool IsValidFor(this object @this, PropertyDescriptor mi, out Exception aggEx) { aggEx = null; ChoGuard.ArgumentNotNullOrEmpty(@this, "Target"); if (@this == null) { return(true); } var results = new List <ValidationResult>(); object surrObj = ChoMetadataObjectCache.Default.GetMetadataObject(@this); bool result = false; if (surrObj is IChoValidatable) { result = ((IChoValidatable)surrObj).TryValidateFor(@this, mi.Name, results); } else { var context = new ValidationContext(@this, null, null); context.MemberName = mi.Name; result = Validator.TryValidateValue(mi.GetValue(@this), context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(@this.GetType(), mi.Name))); } if (!result) { if (results.Count > 0) { aggEx = new ValidationException("Failed to validate '{0}' member. {2}{1}".FormatString(mi.Name, ToString(results), Environment.NewLine)); } else { aggEx = new ValidationException("Failed to valudate."); } } return(result); }
protected virtual void LoadNCacheMembers(IEnumerable <ChoRecordFieldConfiguration> fcs) { if (!IsDynamicObject) { string name = null; object defaultValue = null; object fallbackValue = null; foreach (var fc in fcs) { //if (fc is ChoFileRecordFieldConfiguration) // name = ((ChoFileRecordFieldConfiguration)fc).FieldName; //else name = fc.Name; fc.PD = PDDict.ContainsKey(name) ? PDDict[name] : (PDDict.Any(p => p.Value.Name == name) ? PDDict.Where(p => p.Value.Name == name).Select(p => p.Value).FirstOrDefault() : null); fc.PI = PIDict.ContainsKey(name) ? PIDict[name] : (PIDict.Any(p => p.Value.Name == name) ? PIDict.Where(p => p.Value.Name == name).Select(p => p.Value).FirstOrDefault() : null); if (fc.PD == null || fc.PI == null) { continue; } //Load default value defaultValue = ChoType.GetRawDefaultValue(fc.PD); if (defaultValue != null) { fc.DefaultValue = defaultValue; fc.IsDefaultValueSpecified = true; } //Load fallback value fallbackValue = ChoType.GetRawFallbackValue(fc.PD); if (fallbackValue != null) { fc.FallbackValue = fallbackValue; fc.IsFallbackValueSpecified = true; } //Load Converters fc.PropConverters = ChoTypeDescriptor.GetTypeConverters(fc.PI); fc.PropConverterParams = ChoTypeDescriptor.GetTypeConverterParams(fc.PI); //Load Custom Serializer fc.PropCustomSerializer = ChoTypeDescriptor.GetCustomSerializer(fc.PI); fc.PropCustomSerializerParams = ChoTypeDescriptor.GetCustomSerializerParams(fc.PI); if (fc.SourceType == null) { fc.SourceType = fc.GetSourceTypeFromConvertersIfAny(); } } PropertyNames = PDDict.Keys.ToArray(); } //Validators HasConfigValidators = (from fc in fcs where fc.Validators != null select fc).FirstOrDefault() != null; if (!HasConfigValidators) { if (!IsDynamicObject) { string name = null; foreach (var fc in fcs) { if (fc is ChoFileRecordFieldConfiguration) { name = ((ChoFileRecordFieldConfiguration)fc).FieldName; } else { name = fc.Name; } if (!PDDict.ContainsKey(name)) { continue; } fc.Validators = ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(fc.PD).ToArray(); } } } ValDict = (from fc in fcs select new KeyValuePair <string, ValidationAttribute[]>(fc is ChoFileRecordFieldConfiguration ? ((ChoFileRecordFieldConfiguration)fc).FieldName : fc.Name, fc.Validators)) .GroupBy(i => i.Key).Select(g => g.First()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); }
public static bool IsValidFor(this object @this, MemberInfo mi, out Exception aggEx) { aggEx = null; ChoGuard.ArgumentNotNullOrEmpty(@this, "Target"); if (@this == null) { return(true); } var results = new List <ValidationResult>(); object surrObj = ChoSurrogateObjectCache.Default.GetSurrogateObject(@this); if (surrObj is IChoValidatable) { ((IChoValidatable)surrObj).TryValidateFor(@this, mi.Name, results); } else { //if (ChoObjectMemberMetaDataCache.Default.IsRequired(mi) && ChoType.GetMemberValue(@this, mi) == null) // results.Add(new ValidationResult("Null value found for {0} member.".FormatString(mi.Name))); var context = new ValidationContext(@this, null, null); context.MemberName = mi.Name; Validator.TryValidateValue(ChoType.GetMemberValue(@this, mi), context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(@this.GetType(), mi.Name))); } if (results.Count > 0) { aggEx = new ApplicationException("Failed to validate '{0}' member. {2}{1}".FormatString(mi.Name, ToString(results), Environment.NewLine)); return(false); } else { return(true); } }