public void TestIsNotValid2() { string err = string.Empty; string modStr = string.Empty; NumberField nf = new NumberField("4.2.5"); Assert.IsFalse(nf.Validate(out err, out modStr)); Assert.AreEqual("\tValue must be a number.", err); Assert.IsNull(modStr); }
public void TestIsValid() { string err = string.Empty; string modStr = string.Empty; NumberField nf = new NumberField("94"); Assert.IsTrue(nf.Validate(out err, out modStr)); Assert.IsNull(err); Assert.IsNull(modStr); }
/// <summary> /// Validate the field values: /// 1. Field name must be of format APP_{PROGRAMNAME}_{FIELDNAME}. PROGRAMNAME and /// FIELDNAME are not checked. /// 2. Datatype must be in the enumeration of data types defined in /// </summary> /// <param name="err">Error message if values are not valid</param> /// <param name="modStr">Message if value has been modified</param> /// <returns>true if values are valid, false otherwise</returns> public override bool Validate(out string err, out string modStr) { err = null; modStr = null; string[] appParts = Name.Split('_'); if(appParts.Length < 3 || appParts[1].Equals(string.Empty) || appParts[2].Equals(string.Empty) || !appParts[0].ToUpper().Equals("APP")) { err = "\tInvalid Application Defined Fieldname."; return false; } if(!DataType.Validate(out err, out modStr)) { err = "\tInvalid Data Type."; return false; } switch(DataType.Value) { case "A": // AwardList type string[] awards = Value.Split(','); string[] awardsList = Value.Split(','); for(int i = 0; i < awards.Length; i++) { if(!DataType.aEnums.IsInEnumeration("Award", awards[i])) { modStr = "\tInvalid AwardList item: '" + awards[i] + "'. Item removed."; awardsList[i] = null; } } Value = string.Empty; foreach(string award in awardsList) { if(award != null) { Value += award + ","; } } if(Value != string.Empty) { Value = Value.Substring(0, Value.Length - 1); } break; case "B": // Boolean type BooleanField bf = new BooleanField(Value); if(!bf.Validate(out err, out modStr)) { err = "\tInvalid Boolean Value: '" + Value + "'."; return false; } break; case "C": // CreditList type string[] credits = Value.Split(','); string[] creditList = Value.Split(','); for(int i = 0; i < credits.Length; i++) { if(!DataType.aEnums.IsInEnumeration("Credit", credits[i])) { modStr = "\tInvalid CreditList item: '" + credits[i] + "'."; creditList[i] = null; } } Value = string.Empty; foreach(string credit in creditList) { if(credit != null) { Value += credit + ","; } } if(Value != string.Empty) { Value = Value.Substring(0, Value.Length - 1); } break; case "D": // Date type DateField df = new DateField(Value); if(!df.Validate(out err, out modStr)) { return false; } break; case "L": // Location type Location loc = new Location(Value); if(!loc.Validate(out err, out modStr)) { err = "\tInvalid location: '" + Value + "'."; return false; } break; case "M": // multiline string type MultilineStringField sf = new MultilineStringField(Value); if(!sf.Validate(out err, out modStr)) { err = "\tInvalid multiline string: '" + Value + "'."; return false; } break; case "N": // number type NumberField nf = new NumberField(Value); if(!nf.Validate(out err, out modStr)) { err = "\tInvalid number: '" + Value + "'."; return false; } break; case "P": // sponsored award list type awards = Value.Split(','); string[] awardList = Value.Split(','); for(int i = 0; i < awardList.Length; i++) { string gError = string.Empty; string gMod = string.Empty; Award_Granted granted = new Award_Granted(awards[i], DataType.aEnums); if(!granted.Validate(out gError, out gMod)) { err += gError + Environment.NewLine; } if(gMod != null) { modStr += gMod + Environment.NewLine; } } return err == null; case "S": // string type StringField str = new StringField(Value); if(!str.Validate(out err, out modStr)) { return false; } break; case "T": // time type TimeField tf = new TimeField(Value); if(!tf.Validate(out err, out modStr)) { return false; } break; default: err = "\tInvalid data type: '" + DataType.Value + "'."; return false; } return true; }