Example #1
0
        //Validate missVal. It must be a comma delimited list of values convertable to FieldDataType.
        //An item in the list might possibly be in format "x thru y" where x and y must be convertable
        //with x <= y.
        private void CheckMissVal(string valueToCheck, FieldDataType fdt)
        {
            //Check MissVal by attempting to create a RangeList object.
            RangeList throwaway = new RangeList(valueToCheck, fdt);



            ////create a string array containing the comma delimited items
            //string[] listItems = valueToCheck.Split(new char[] { ',' });

            ////validate each item
            //foreach (string sItem in listItems)
            //{
            //    //create a string array by splitting each item on the string "thru"
            //    string[] splitItem = sItem.ToLower().Split(new string[] { "thru" }, StringSplitOptions.None);

            //    //throw an exception if splitting results in more that 2 elements
            //    if (splitItem.Length > 2)
            //    {
            //        throw new ArgumentException("missVal not valid.");
            //    }

            //    foreach (string s in splitItem)
            //    {
            //        CheckType(s, fdt);
            //    }

            //    //verify that x <= y
            //    if (splitItem.Length == 2)
            //    {
            //        switch (fdt)
            //        {
            //            case FieldDataType.DATE:
            //                if (!(DateTime.Parse(splitItem[0]) <= DateTime.Parse(splitItem[1])))
            //                {
            //                    throw new ArgumentException("missVal not valid.");
            //                }
            //                break;

            //            case FieldDataType.FLOAT:
            //                if (!(float.Parse(splitItem[0]) <= float.Parse(splitItem[1])))
            //                {
            //                    throw new ArgumentException("missVal not valid.");
            //                }
            //                break;

            //            case FieldDataType.INT:
            //                if (!(int.Parse(splitItem[0]) <= int.Parse(splitItem[1])))
            //                {
            //                    throw new ArgumentException("missVal not valid.");
            //                }
            //                break;

            //            case FieldDataType.TEXT:
            //                // nothing to check
            //                break;

            //            default:
            //                throw new Exception("FieldDataType invalid.");

            //        }
            //    }
            //}
        }
Example #2
0
        /*
         * Returns a List<string> containing any errors.  If no errors the List.count ==0.
         *
         * Checks:
         *
         * ID is required.
         * fldname is Required.
         *
         * FieldDataType is required.
         *
         * At least one of IsInsertField, IsEntryField, IsReadOnly must be true.
         *
         * If IsReadOnly == true then
         *  IsEntryField == false
         *  IsInsertField == false
         *  IsDoubleEntryField == false
         *
         * If IsEntryField == false then
         *  IsDoubleEntryField == false
         *
         * Validates any supplied value for FormatString, MinVal, MaxVal, ValidList, RegEx
         *
         * Attempts to create RangeList objects for any supplied ValidList or MissVal.
         * See RangeList Class for details.
         */
        public List <string> ValidateAndInitializeProperties()
        {
            List <string> errors = new List <string>();

            #region fldname, ID, FieldDataType

            if (string.IsNullOrEmpty(this.FldName))
            {
                errors.Add("A value for the fldname property is required.");
            }

            if (string.IsNullOrEmpty(this.ID))
            {
                errors.Add("A value for the ID property is a required.");
            }

            if (this.FieldDataType == FieldDataType.UNKNOWN)
            {
                errors.Add("FieldDataType is required. A value must be set on the field tag if metadata is missing.");
            }

            #endregion

            #region FormatString, Min/MaxVal, ValidList

            //validation of FormatString, Min/MaxVal, ValidList depends on existence of FieldDataType
            //so return if it might be missing
            if (errors.Count != 0)
            {
                return(errors);
            }

            // If supplied, make sure FormatString is valid by attempting
            // to apply it
            if (string.IsNullOrEmpty(FormatString) == false)
            {
                string fstr = FormatString;
                try
                {
                    //calls to string.Format() will fail if fstr invalid
                    string formatted = string.Empty;
                    switch (FieldDataType)
                    {
                    case FieldDataType.DATE:
                        formatted = string.Format(fstr, DateTime.Parse("5/5/2004"));
                        break;

                    case FieldDataType.FLOAT:
                        formatted = string.Format(fstr, float.Parse("4.235"));
                        break;

                    case FieldDataType.INT:
                        formatted = string.Format(fstr, int.Parse("5"));
                        break;

                    case FieldDataType.TEXT:

                        // throw an exception because formatstring isn't
                        // allowed for text types
                        throw new Exception("FormatString isn't allowed for text data type.");

                    default:
                        throw new Exception("Value of FieldDataType can't be UNKNOWN if setting FormatString");
                    }
                }
                catch (Exception e)
                {
                    errors.Add("Invalid format string: " + e.Message);
                }
            }



            // If supplied, make sure MinVal value is appropriate for
            // data type
            if (string.IsNullOrEmpty(MinVal) == false)
            {
                try
                {
                    switch (FieldDataType)
                    {
                    case FieldDataType.DATE:
                        DateTime.Parse(MinVal);
                        break;

                    case FieldDataType.FLOAT:
                        float.Parse(MinVal);
                        break;

                    case FieldDataType.INT:
                        int.Parse(MinVal);
                        break;

                    case FieldDataType.TEXT:
                        // nothing to check
                        break;

                    default:
                        throw new Exception("Invalid FieldDataType");
                    }
                }
                catch
                {
                    errors.Add(string.Format("MinVal invalid because it cannot be converted to {0}.", FieldDataType));
                }
            }

            // If supplied make sure MaxVal value is appropriate for
            // data type
            if (string.IsNullOrEmpty(MaxVal) == false)
            {
                try
                {
                    switch (FieldDataType)
                    {
                    case FieldDataType.DATE:
                        DateTime.Parse(MaxVal);
                        break;

                    case FieldDataType.FLOAT:
                        float.Parse(MaxVal);
                        break;

                    case FieldDataType.INT:
                        int.Parse(MaxVal);
                        break;

                    case FieldDataType.TEXT:
                        // nothing to check
                        break;

                    default:
                        throw new Exception("Invalid FieldDataType");
                    }
                }
                catch
                {
                    errors.Add(string.Format("MaxVal invalid because it cannot be converted to {0}.", FieldDataType));
                }
            }

            //Initialize ValidList RangeList
            if (string.IsNullOrEmpty(ValidList) == false)
            {
                try
                {
                    _validListRangeList = new RangeList(ValidList, FieldDataType);
                }
                catch (Exception e)
                {
                    errors.Add(string.Format("ValidList {0} invalid. " + e.Message, ValidList));
                }
            }

            //Initialize MissVal RangeList
            if (string.IsNullOrEmpty(MissVal) == false)
            {
                try
                {
                    _missValRangeList = new RangeList(MissVal, FieldDataType);
                }
                catch (Exception e)
                {
                    errors.Add(string.Format("MissVal {0} invalid. " + e.Message, MissVal));
                }
            }


            #endregion

            #region Others

            //At least one of IsInsertField, IsEntryField, IsReadOnly must be true.
            if ((IsInsertField || IsEntryField || IsReadOnly) == false)
            {
                errors.Add("At least one of IsInsertField, IsEntryField, IsReadOnly must be true.");
            }

            // If IsReadOnly == true then
            //  IsEntryField == false
            //  IsInsertField == false
            //  IsDoubleEntryField == false
            if (IsReadOnly == true && (IsEntryField || IsInsertField || IsDoubleEntryField))
            {
                errors.Add("A ReadOnly field can't also have IsEntryField, IsInsertField, or IsDoubleEntry set to true.");
            }

            // If IsEntryField == false then
            //  IsDoubleEntryField == false
            if (IsEntryField == false && IsDoubleEntryField == true)
            {
                errors.Add("A setting of IsDoubleEntryField = true requires IsEntryField = true.");
            }


            // if RegEx supplied test the value
            if (string.IsNullOrEmpty(RegEx) == false)
            {
                try
                {
                    Regex r = new Regex(RegEx);
                }
                catch (ArgumentException ae)
                {
                    if (ae != null)
                    {
                        errors.Add("Invalid regular expression: " + RegEx);
                    }
                }
            }


            #endregion

            //return errors
            return(errors);
        }