public void TestIsValidFalse() { string err = string.Empty; string modStr = string.Empty; StringField field = new StringField(null); Assert.IsFalse(field.Validate(out err, out modStr)); Assert.AreEqual("Value is null.", err); Assert.IsNull(modStr); }
public void TestIsValid() { string err = string.Empty; string modStr = string.Empty; StringField field = new StringField("VA3HJ"); Assert.IsTrue(field.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; }
public void TestIsValidNewLine() { string err = string.Empty; string modStr = string.Empty; StringField field = new StringField("VA3HJ" + Environment.NewLine + "VA3JNO"); Assert.IsFalse(field.Validate(out err, out modStr)); Assert.AreEqual("\tString value contains a new line character. This is not allowed in StringField types.", err); Assert.IsNull(modStr); }