private void GetValidationsForDto(GetValidationsParms parms) { parms.JsonString.Append(parms.JsonObjectName + ": { "); var dtoClass = TypeHelper.GetObjectType(parms.DtoObjectName, false, parms.DtoAlternateNamespace, parms.DtoAssemblyNames.ToArray()); if (dtoClass == null) { var message = string.Format("DTO '{0}' not found.", parms.DtoObjectName); throw new Exception(message); } var properties = dtoClass.GetProperties(); if (properties != null) { var isFirstProp = true; foreach (var prop in properties) { if (prop.CustomAttributes != null && prop.CustomAttributes.Count() > 0) { var attrStr = new StringBuilder(); var isFirstAttr = true; var displayName = GetDisplayName(prop); foreach (var attr in prop.CustomAttributes) { var converter = _converters.FirstOrDefault(vc => vc.IsAttributeMatch(attr)); if (converter != null) { converter.Convert(prop.Name, displayName, attr, attrStr, isFirstAttr, parms.ResourceNamespace, parms.ResourceAssemblyName); isFirstAttr = false; } } if (!isFirstAttr) { var sep = isFirstProp ? string.Empty : ","; if (parms.UseCamelCaseForProperties) { parms.JsonString.Append(sep + CamelCaseProperty(prop.Name) + ": { "); } else { parms.JsonString.Append(sep + prop.Name + ": { "); } parms.JsonString.Append(attrStr.ToString()); parms.JsonString.Append("}"); isFirstProp = false; } } } } parms.JsonString.Append("}"); }
/// <summary> /// Basically a copy-constructor /// </summary> public GetValidationsParms(GetValidationsParms p) { this.ValidationObject = p.ValidationObject; this.DtoObjectName = p.DtoObjectName; this.JsonObjectName = p.JsonObjectName; this.DtoAlternateNamespace = p.DtoAlternateNamespace; this.DtoAssemblyNames = p.DtoAssemblyNames; this.ResourceNamespace = p.ResourceNamespace; this.ResourceAssemblyName = p.ResourceAssemblyName; }
/// <summary> /// Get the validations for dtoObjectName and return the json object. /// </summary> /// <param name="dtoObjectName"></param> /// <param name="jsonObjectName"></param> /// <param name="assemblyNames">Names of assemblies to check</param> /// <returns></returns> public object GetValidations(string dtoObjectName, string jsonObjectName, string alternateNamespace, bool useCamelCaseForProperties, params string[] assemblyNames) { var parms = new GetValidationsParms(dtoObjectName, jsonObjectName) { DtoAlternateNamespace = alternateNamespace, DtoAssemblyNames = new List <string>(assemblyNames), UseCamelCaseForProperties = useCamelCaseForProperties }; return(GetValidations(parms)); }
/// <summary> /// Get the validations for the given parms. /// </summary> /// <param name="parms"></param> /// <returns></returns> public object GetValidations(GetValidationsParms parms) { if (parms == null) { throw new ArgumentNullException("parms", "Expecting GetValidationParms in GetValidations call and they were not supplied."); } GetValidationsForDto(parms); parms.CompleteJsonString(); return(JObject.Parse(parms.JsonString.ToString())); }
/// <summary> /// Get the validations for dtoObjectName and return the json object. /// </summary> /// <param name="dtoObjectName"></param> /// <param name="jsonObjectName"></param> /// <param name="assemblyNames">Names of assemblies to check</param> /// <returns></returns> public object GetValidations(string dtoObjectName, string alternateNamespace, params string[] assemblyNames) { var parms = new GetValidationsParms(dtoObjectName, "validations") { DtoAlternateNamespace = alternateNamespace, DtoAssemblyNames = new List<string>(assemblyNames), }; return GetValidations(parms); }
/// <summary> /// This function iterates over the properties of the Dto and adds the validation attributes to the /// JsonString. /// </summary> public Dictionary<string, object> GetValidations(GetValidationsParms parms) { if (parms == null) throw new ArgumentNullException("parms", "Expecting GetValidationParms in GetValidations call and they were not supplied."); var modelValidations = new Dictionary<string, object>(); var dtoClass = TypeHelper.GetObjectType(parms.DtoObjectName, false, parms.DtoAlternateNamespace, parms.DtoAssemblyNames.ToArray()); if (dtoClass == null) { var message = string.Format("DTO '{0}' not found.", parms.DtoObjectName); throw new Exception(message); } var properties = dtoClass.GetProperties(); if (properties != null) { foreach (var prop in properties) { var propertyValidations = new Dictionary<string, object>(); if (prop.CustomAttributes != null && prop.CustomAttributes.Count() > 0) { var validateSubclassProp = Attribute.IsDefined(prop, typeof(ValidateSubclass)); if(validateSubclassProp) { var nextParms = new GetValidationsParms(parms); nextParms.DtoObjectName = prop.PropertyType.Name; nextParms.JsonObjectName = prop.Name; modelValidations = modelValidations.Concat(GetValidations(nextParms)).ToDictionary(x => x.Key, x => x.Value); } foreach (var attr in prop.CustomAttributes) { var converter = _converters.FirstOrDefault(vc => vc.IsAttributeMatch(attr)); if (converter != null) { var ret = converter.Convert(prop.Name, attr); propertyValidations = propertyValidations.Concat(ret).ToDictionary(x => x.Key, x => x.Value); } } } if (propertyValidations.Count > 0) { modelValidations.Add(prop.Name, propertyValidations); } } } return new Dictionary<string, object>(){{parms.JsonObjectName, modelValidations}}; }