Checklist for Jira already offers a quite highly versatile Validator, but sometimes you need a more specific validationrequire more complex validations. This is where a custom scripted validator comes in into play.
Info |
---|
The following scripts are using a ScriptRunner Script Validator. |
To validate a Checklist, you first retrieve the value collection of checklist items from the issue, calculate the desired information from the list, then throw an exception if the validation should not pass.
Code Block | |
---|---|
java | // Retrieve checklist items 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 | ||
---|---|---|
| ||
// Throw exception if not all items have statuses if (!allItemsHaveStatuses) { throw new InvalidInputException("All items must have statuses in '" + checklistCustomField.getName() + "' Checklist."); } |
...
Here Following is a complete example of a Custom Validator that verifies if at least one item is checked.
...