/// <summary> /// Return list of validations for composite type. /// </summary> /// <param name="p"></param> /// <param name="name"></param> /// <param name="method"></param> /// <param name="ancestors"></param> /// <param name="isCompositeProperties"></param> /// <returns></returns> public static List<string> ValidateCompositeType(this IParameter p, string name, HttpMethod method, HashSet<string> ancestors, bool isCompositeProperties = false) { List<string> x = new List<string>(); if (method != HttpMethod.Patch || !p.IsBodyParameter() || isCompositeProperties) { foreach (var prop in ((CompositeType)p.Type).Properties) { var primary = prop.Type as PrimaryType; var sequence = prop.Type as SequenceType; var map = prop.Type as MapType; var composite = prop.Type as CompositeType; if (primary != null || sequence != null || map != null) x.AddRange(prop.ValidateType($"{name}.{prop.Name}", method, true)); else if (composite != null) { if (ancestors.Contains(composite.Name)) { x.AddNullValidation($"{name}.{prop.Name}", p.IsRequired); } else { ancestors.Add(composite.Name); x.AddRange(prop.ValidateCompositeType($"{name}.{prop.Name}", method, ancestors, true)); ancestors.Remove(composite.Name); } } } } x.AddRange(from prop in ((CompositeType)p.Type).Properties where prop.IsReadOnly select GetConstraint($"{name}.{prop.Name}", ReadOnlyConstraint, "true")); List<string> y = new List<string>(); if (x.Count > 0) { if (p.CheckNull() || isCompositeProperties) y.AddRange(x.AddChain(name, NullConstraint, p.IsRequired)); else y.AddRange(x); } else { if (p.IsRequired && (p.CheckNull() || isCompositeProperties)) y.AddNullValidation(name, p.IsRequired); } return y; }
/// <summary> /// Return list of validations for primary, map, sequence and rest of the types. /// </summary> /// <param name="p"></param> /// <param name="name"></param> /// <param name="method"></param> /// <param name="isCompositeProperties"></param> /// <returns></returns> public static List<string> ValidateType(this IParameter p, string name, HttpMethod method, bool isCompositeProperties = false) { List<string> x = new List<string>(); if (method != HttpMethod.Patch || !p.IsBodyParameter() || isCompositeProperties) { x.AddRange(p.Constraints.Select(c => GetConstraint(name, c.Key.ToString(), c.Value)).ToList()); } List<string> y = new List<string>(); if (x.Count > 0) { if (p.CheckNull() || isCompositeProperties) y.AddRange(x.AddChain(name, NullConstraint, p.IsRequired)); else y.AddRange(x); } else { if (p.IsRequired && (p.CheckNull() || isCompositeProperties)) y.AddNullValidation(name, p.IsRequired); } return y; }