Skip to content
Latchkey

How to Validate Inputs in a Custom Action

Since the runner does not enforce input formats, validate them in the action and fail fast on bad values.

Check inputs against allowed values or a pattern at the start of the action. Fail with ::error:: plus exit 1 in a composite step, or core.setFailed in JavaScript.

Steps

  • Read the input early, before any side effects.
  • Compare against an allowlist or a regex.
  • Fail with a clear annotation on a bad value.

Composite validation

action.yml
inputs:
  level:
    description: 'Log level'
    default: 'info'
runs:
  using: "composite"
  steps:
    - run: |
        case "${{ inputs.level }}" in
          debug|info|warn|error) ;;
          *) echo "::error::level must be debug, info, warn, or error"; exit 1 ;;
        esac
      shell: bash

JavaScript validation

index.js
const allowed = ['debug', 'info', 'warn', 'error'];
const level = core.getInput('level') || 'info';
if (!allowed.includes(level)) {
  core.setFailed(`level must be one of ${allowed.join(', ')}`);
}

Gotchas

  • Validate before side effects so a bad input never leaves partial state.
  • The action.yml type field for inputs is not enforced by the runner; validate yourself.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →