ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 58 forks source link

Add support for enum value validation in the copybook module #6862

Closed MohamedSabthar closed 1 month ago

MohamedSabthar commented 1 month ago

Description: Consider the following copybook schema representations:

Example 1:

01 REQUIRED             PIC 9.
   88 YES               VALUE 1.
   88 NO                VALUE 0.

Example 2:

01 REQUIRED             PIC XXX.
   88 YES               VALUE 'YES'.
   88 NO                VALUE 'NO '.

These schemas can be considered as enums in a copybook. In both cases, providing a value that does not match the specified conditions should result in an error.

For example, in Example 2, the following code should trigger a validation error:

import ballerina/io;
import ballerinax/copybook;

public function main() returns error? {
    copybook:Converter converter = check new ("example2.cbl");
    map<json> inputData = {REQUIRED: "N"}; // "N" is not valid here
    string|error ascii = converter.toCopybook(inputData);
    io:println(ascii);
}

The error details of the returned error might include the following information: {"errors": ["Value 'N ' is invalid for field 'REQUIRED'. Allowed values are: 'YES', 'NO '."]}

Describe your task(s)

Related Issues (optional):

Suggested Labels (optional):

Suggested Assignees (optional):