Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Checklist for Jira already offers a highly versatile Validator but sometimes you require more complex validations. This is where a custom scripted validator comes into play.

Info

The following scripts are using a ScriptRunner Script Validator.

To validate a Checklist, you first retrieve the collection of checklist items from the issue.

Code Block

...

languagejava
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField);

Then you extract and validate the desired information from the item

Code Block
// Find out if all items have statuses
def allItemsHaveStatuses = existingChecklistValue.size() > 0;
for (ChecklistItem item : existingChecklistValue) {
    if (!item.hasStatus()) {
      allItemsHaveStatuses = false;
      break;
    }
}

Finally, you throw an exception if the validation failed.

Code Block
languagejava
// Throw exception if not all items have statuses
if (!allItemsHaveStatuses) {
    throw new InvalidInputException("All items must have statuses in '" + checklistCustomField.getName() + "' Checklist.");
}

...

Following is a complete example of a Custom Validator that will verify verifies if at least one item is checked.

Code Block
languagejava
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.component.ComponentAccessor;
import com.opensymphony.workflow.InvalidInputException;

// Retrieve the Custom Field Type for the Checklist Custom Field
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def checklistCustomField = customFieldManager.getCustomFieldObject("customfield_10000");
def checklistCustomFieldType = (ChecklistCFType) checklistCustomField.getCustomFieldType();

// AddRetrieve Checklist items to
the existing Checklist
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField);

def anyItemChecked
= false;
// Find out if any item is checked
def anyItemChecked = false;
for (ChecklistItem item : existingChecklistValue) {
    if (item.isChecked()) {
      anyItemChecked = true;
      break;
    }
}

if (!anyItemChecked) {
    throw new InvalidInputException("At least one item must be checked in '" + checklistCustomField.getName() + "' Checklist.");
}