Technical Resources
Validation in the New ANSI COBOL Standard
Author : Gary DeWard Brown
This is the second part of a series on validation. The previous part described validation in general by illustrating some traditional programming techniques. This part describes the validation features in the new ANSI standard.
The new ANSI standard has recognized the ubiquity and importance of validation by letting you specify the validation when you describe the data item, which not only places the validation in the Data Division, but also ties it directly to the data item being validated and makes it easy to locate and change. Then to do the actual validation, you execute a new VALIDATE statement that names the data to validate. COBOL then validates the data item according to the tests you coded in the data descriptions.
To specify the validation, several new clauses have been added to the data descriptions. These clauses are ignored and have no effect except when the VALIDATE statement is executed. To illustrate how validation might be done with the new ANSI standard, suppose that you had the following record that you needed to validate.
01 A-Record.
05 Num-Val PIC S9(5)V99 COMP-3.
05 Char-Val PIC X(10).
To specify the validation, you code the new clauses in the data descriptions. Num-Val is numeric, and you might want to test that it contains numeric data. This is done with the CLASS clause where you specify the type of data the item is to contain.
CLASS IS NUMERIC checks the data value for valid numeric values, and operates much the same as if you had coded IF Num-Val IS NUMERIC in the Procedure Division. Besides NUMERIC, you can also specify ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER, BOOLEAN, or a class-name in the SPECIAL-NAMES paragraph.
In addition, you might want to test Num-Val for a valid range of numeric values. You code valid values with a level 88 condition name description with a VALID VALUES clause.
88 VALID VALUES ARE 10 THRU 1000
Often in a data range, there are exceptions. Suppose that values of 0 and 10000, while not in the range, are to be allowed in as valid values. For this, there is an ALLOW phrase to list the exceptions.
ALLOW 0 OR 10000
ALLOW permits values outside the VALID range to be deemed valid. For the reverse, invalid values inside the range of the VALID clause, you can code the INVALID clause. The following clause makes Num-Val invalid if it is equal to 21, even if 21 falls within the range of values in the VALID clause. Note that INVALID is written much like an IF statement and is not part of a level 88 item.
INVALID WHEN Num-Val = 21
Here is how the description of Num-Val might look when all the clauses are put together.
05 Num-Val PIC S9(5)V99 COMP-3
CLASS IS NUMERIC
INVALID WHEN Num-Val = 21
88 VALID VALUES ARE Min-Val THRU Max-Val.
These clauses do absolutely nothing until you execute the VALIDATE statement in the Procedure Division. When you execute it, COBOL checks the data for errors according to the specifications you have coded. Assuming it detects an error, you need to display an error message. You can do this, but in a somewhat indirect way. You define a new data item not associated with the data being validated to contain the error message. It is just storage for the error message. In it’s description, you add an ERROR STATUS clause with both the error message and the name of the data item to trigger the error message to be moved to the storage you have provided for it.
01 Error-Msg-1 PIC X(20)
ERROR STATUS IS "Num-Val not valid" FOR Num-Val.
Now when the VALIDATE statement is executed, should COBOL find an error in Num-Val, it sees that an ERROR STATUS clause has named Num-Val, which tells it to move the error message to Error-Msg-1. If in the validation, Num-Val were found to be valid, COBOL would move spaces to Error-Msg-1. (For no error, alphanumeric items will contain blanks; numeric items zero.) The error message tells us only that Num-Val is in error. To tell why, the ERROR STATUS clause lets you add an ON clause to give a different error message for each cause. You code the cause, FORMAT (format errors), CONTENT (invalid content), or RELATION (an ALLOW or INVALID WHEN condition) in the ON clause. The following illustrates this.
01 Error-Msg-1 PIC X(20)
ERROR STATUS IS "Num-Val format invalid" ON FORMAT
FOR Num-Val
ERROR STATUS IS "Num-Val contents invalid" ON CONTENT
FOR Num-Val
ERROR STATUS IS "Num-Val relations invalid" ON RELATION
FOR Num-Val.
A PRESENT WHEN clause lets you code a condition for validating the data item. If the condition is evaluated as true, the value of the data item is validated; otherwise, it is not validated. Here Char-Val is validated only if Num-Val contains 7 or 8. In addition, an ALLOW clause can be coded to apply only when a condition is true.
05 Char-Val PIC X
PRESENT WHEN Num-Val = 7 OR 8
CLASS IS ALPHABETIC-UPPER
ALLOW "A" OR "C" OR "E" WHEN Num-Val = 7
ALLOW "X" OR "Y" OR "Z" WHEN Num-Val – 8.
We could then code separate error messages for Char-Val.
01 Error-Msg-2 PIC X(20)
ERROR STATUS IS "Char-Val not valid data" ON FORMAT
FOR Char-Val
ERROR STATUS IS "Char-Val not upper-case data" ON CONTENT
FOR Char-Val
ERROR STATUS IS "Char-Val not correct code" ON RELATION
FOR Char-Val.
You name the identifiers to validate in the VALIDATE statement. When you name a group item, COBOL validates all the items subordinate to it.
VALIDATE Char-Val, Num-Val Same as VALIDATE A-Record
Although you code all the editing in the data description, the actual data checking is not done until you execute the EVALUATE statement in the Procedure Division. EVALUATE causes COBOL to evaluate the data, and if errors are found, the error messages and a value for the flag are stored.
EVALUATE A-Record
IF Error-Msg-1 NOT = SPACES OR Error-Msg-2 NOT = SPACES
THEN DISPLAY A-Record
END-IF
IF Error-Msg-1 NOT = SPACES
THEN DISPLAY Error-Msg-1
END-IF
IF Error-Msg-2 NOT = SPACES
THEN DISPLAY Error-Msg-2
END-IF
The following examples illustrate some of the additional clauses provided.
DEFAULT IS 0
The DEFAULT clause specifies a value to use for the remainder of the validation when a value in the data item is found to be invalid. It doesn’t change the value of the data item; it only specifies a value to use for further validation.
DESTINATION IS Error-Val
The DESTINATION clause moves the value of the field being validated to the identifier you name. This enables you display the invalid data or whatever.
The level 88 condition name also has an INVALID clause to specify specific invalid values.
88 INVALID VALUES ARE 1 THRU 10.
88 INVALID VALUES ARE 1 THRU 10
WHEN The-Val < 10 OR > 1000.
For validating values in a table, there is a VARYING clause that specifies a counter for COBOL to use as a subscript in validating or storing different invalid data values in different element of a table. COBOL automatically defines and creates the temporary counter, which then exists only while the data item is being validated.
01 A-Record.
02 A-Table PIC S9(9) OCCURS 10 TIMES
VARYING T-Cntr FROM 1 BY 1
INVALID WHEN A-VAL (T-Cntr) < 10 OR > 1000.
The Author
Gary DeWard Brown is an internationally recognized COBOL expert and best-selling author of Advanced COBOL - For Structured and Object-Oriented Programming, Third Edition, Wiley Computer Publishing and System 390 JCL, Fourth Edition, Wiley Computer Publishing. He is the President of Spear H. Computing Corporation, a national computer consulting firm on mainframe systems and COBOL programming. Gary can be reached at GaryDeWard@aol.com
|