Example #1
0
        public override FieldError ValidateValue(
            StringBuilder data)
        {
            FieldError error = null;
            string     str   = data.ToString();

            if (data.Length < minLength || data.Length == 5)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
                return(error);
            }

            if (data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
                return(error);
            }

            //run thru the data to ensure all are digits
            for (int i = 0; i < data.Length; i++)
            {
                if (!DataTypeHelper.IsDigit(data[i]))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), data.ToString());
                    return(error);
                }
            }

            //data is between min and max length, thus it is atleast 4 chars
            //and all are digits
            int hour = 10 * (data[0] - '0') + (data[1] - '0');
            int min  = 10 * (data[2] - '0') + (data[3] - '0');
            int sec  = data.Length == 4 ? 0 : 10 * (data[4] - '0') + (data[5] - '0');

            if (hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
            }


            return(error);
        }
Example #2
0
        /*
         * This method does validation of a numeric data type after precision point has been
         * removed during serialization and raw data during parsing. The following rules need
         * to be obeyed
         *
         * 1) 0th char is - or digit, + is not allowed
         * 2) All other chars are digit
         * 3) If length > minLength, then no leading zeroes are allowed
         * 4) Sign is not part of length calculation
         *
         * This method doesn't apply precision point nor does it check for it
         */
        public static bool ValidateRDataType(StringBuilder data, int minL, int maxL, int decimalSeparator, out FieldError error)
        {
            error = null;
            int dataLen = data.Length;

            if (dataLen == 0) //too short error
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), string.Empty);
                return(true);
            }

            char c         = data[0];
            bool isNegSign = false;
            bool isError   = false;

            if (c == '+')
            {
                isError = true;
            }

            else if (c == '-')
            {
                isNegSign = true;
                if (data.Length == 1)
                {
                    isError = true;                   //data is -, single negative sign which is invalid
                }
            }

            if (isError)
            {
                AppendInvalidCharCode(data);
                return(true);
            }

            //This loop would ensure all are digits or digits + one point
            //otherwise loop would abort and return true
            bool bPointFound  = false;
            int  decimalIndex = -1;

            for (int i = isNegSign ? 1 : 0; i < dataLen; i++)
            {
                if (DataTypeHelper.IsDigit(data[i])) //digit found, no-op
                {
                }

                else if (!bPointFound && data[i] == decimalSeparator)
                {
                    bPointFound  = true;
                    decimalIndex = i;
                    //Edifact data should have atleast one digit after decimal point
                    if (!(i + 1 < dataLen && DataTypeHelper.IsDigit(data[i + 1])))
                    {
                        error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                                  X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                    }
                }

                else //error state
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }


            if (isNegSign)
            {
                dataLen--; //count of digits
                if (dataLen > 0)
                {
                    c = data[1];
                }
                else
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }



            //check for leading zeroes
            //Leading zero is the 1st char being zero, otherwise not
            //leading zero is allowed only when data has minimum length
            //skip the check if minL is not specified, #3454
            if (c == '0' && minL > 0 && false)
            {
                if (decimalIndex < 0) //no decimal found
                {
                    if ((dataLen > minL && minL > 0) || minL < 0)
                    {
                        AppendInvalidCharCode(data);
                        return(true);
                    }
                }

                else //decimal was found, so 0.1, -0.1 are valid but 00.1 or -00.1 are invalid
                {
                    if ((dataLen > minL && minL > 0) || minL < 0)
                    {
                        int offset = isNegSign ? 2 : 1;
                        if (data.Length > offset && data[offset] == decimalSeparator)
                        {
                        }

                        else
                        {
                            AppendInvalidCharCode(data);
                            return(true);
                        }
                    }
                }
            }

            if (decimalIndex >= 0)
            {
                dataLen--;
                if (data[data.Length - 1] == '0' && false &&
                    ((dataLen > minL && minL > 0) || minL < 0))
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }

            if (minL > 0 && dataLen < minL)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());

                return(true);
            }

            if (maxL > 0 && dataLen > maxL)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), data.ToString());
                return(true);
            }

            return(false);
        }
Example #3
0
        public override FieldError ValidateValue(StringBuilder data)
        {
            FieldError error = null;
            string     str   = data.ToString();

            // First try the .NET function to check if date is valid
            // if not we will follow other way (check yyyymmdd)

            DateTime dt;

            if (DateTime.TryParse(str, out dt))
            {
                return(error);
            }

            if (data.Length < minLength || data.Length == 7)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
                return(error);
            }

            if (data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
                return(error);
            }

            //run thru the data to ensure all are digits
            for (int i = 0; i < data.Length; i++)
            {
                if (!DataTypeHelper.IsDigit(data[i]))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), data.ToString());
                    return(error);
                }
            }

            //data is between min and max length, thus it is atleast 6 chars
            //and all are digits
            int offset = data.Length == 6 ? 2 : 4;
            int year   = data.Length == 6 ? 10 * (data[0] - '0') + (data[1] - '0')
                    : 1000 * (data[0] - '0') + 100 * (data[1] - '0') + 10 * (data[2] - '0') + (data[3] - '0');

            int month = 10 * (data[offset] - '0') + (data[offset + 1] - '0');
            int day   = 10 * (data[offset + 2] - '0') + (data[offset + 3] - '0');

            if (year == 0)
            {
                year = 2000;
            }

            try
            {
                DateTime dateTime = new DateTime(year, month, day);
            }
            catch (ArgumentOutOfRangeException)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
            }

            return(error);
        }
Example #4
0
        /*
         * This method does validation of a numeric data type after precision point has been
         * removed during serialization and raw data during parsing. The following rules need
         * to be obeyed
         *
         * 1) 0th char is - or digit, + is not allowed
         * 2) All other chars are digit
         * 3) If length > minLength, then no leading zeroes are allowed
         * 4) Sign is not part of length calculation
         *
         * This method doesn't apply precision point nor does it check for it
         */
        private bool ValidateNDataType(StringBuilder data, out FieldError error)
        {
            error = null;
            int dataLen = data.Length;

            if (dataLen == 0) //too short error
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), string.Empty);
                return(true);
            }

            char c         = data[0];
            bool isNegSign = false;

            if (c == '+')
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                return(true);
            }

            else if (c == '-')
            {
                isNegSign = true;
            }

            for (int i = isNegSign ? 1 : 0; i < dataLen; i++)
            {
                // "." is valid character
                if (!(DataTypeHelper.IsDigit(data[i]) || data[i] == '.'))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                    return(true);
                }
            }


            if (isNegSign)
            {
                dataLen--; //count of digits
                if (dataLen > 0)
                {
                    c = data[1];
                }
                else
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());
                    return(true);
                }
            }



            if (/*(mPrecision > 0 && dataLen < mPrecision) || */ (minLength >= 0 && dataLen < minLength))
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());
                return(true);
            }

            if (maxLength >= 0 && dataLen > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), data.ToString());
                return(true);
            }

            return(false);
        }