Skip to content

GitHub Actions Validator — TypeScript source

Check a workflow YAML before you push: on-shape, jobs, runs-on, steps with run or uses, and the needs graph including cycle detection — every issue reported at its path. Runs locally in your browser.

This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// Pure surface of the GitHub Actions Validator (slug: gh-actions-validator).
// Parses a workflow YAML and checks structural rules: root shape, `on:`
// event shape, jobs with needs-graph acyclicity, runs-on (unless the job
// delegates to a reusable workflow), and steps that are exactly one of
// run/uses. The rule set and message texts are the contract a Go CLI twin
// must reproduce (Go side: yaml.v3). Not checked (by design, documented):
// action versions, expressions (${{ }}) beyond syntax-free presence, runner
// label validity, duplicate YAML keys, `permissions` granularity.
import { parse } from 'yaml';

export interface WorkflowIssue {
  path: string;
  message: string;
}

export type GhActionsValidateResult =
  | { valid: true; issues: []; error: null }
  | { valid: false; issues: WorkflowIssue[]; error: null }
  | { valid: false; issues: []; error: string };

type Node = null | boolean | number | string | Node[] | { [k: string]: Node };

const EVENTS = new Set([
  'push', 'pull_request', 'pull_request_target', 'workflow_dispatch', 'schedule', 'release',
  'issues', 'issue_comment', 'watch', 'fork', 'create', 'delete', 'deployment', 'page_build',
  'public', 'pull_request_review', 'pull_request_review_comment', 'push_tag', 'registry_package',
  'repository_dispatch', 'status', 'workflow_run', 'check_run', 'check_suite', 'label',
]);

function isMap(v: Node | undefined): v is { [k: string]: Node } {
  return v !== null && typeof v === 'object' && !Array.isArray(v);
}

/** Validate a workflow document (already parsed). */
function check(root: Node, out: WorkflowIssue[]): void {
  if (!isMap(root)) {
    out.push({ path: '$', message: 'workflow root must be a mapping' });
    return;
  }
  if (!('jobs' in root)) out.push({ path: '$', message: 'missing required key "jobs"' });
  if (!('on' in root)) out.push({ path: '$', message: 'missing required key "on"' });

  // on: string | array of strings | map of event -> config
  const on = root.on;
  if ('on' in root) {
    if (typeof on === 'string') {
      if (!EVENTS.has(on)) out.push({ path: '$.on', message: `unknown event "${on}"` });
    } else if (Array.isArray(on)) {
      on.forEach((e, i) => {
        if (typeof e !== 'string') out.push({ path: `$.on[${i}]`, message: 'event list entries must be strings' });
        else if (!EVENTS.has(e)) out.push({ path: `$.on[${i}]`, message: `unknown event "${e}"` });
      });
    } else if (isMap(on)) {
      for (const name of Object.keys(on)) {
        if (!EVENTS.has(name)) out.push({ path: `$.on.${name}`, message: `unknown event "${name}"` });
        const cfg = on[name];
        if (isMap(cfg) && 'branches' in cfg && !Array.isArray(cfg.branches) && typeof cfg.branches !== 'string') {
          out.push({ path: `$.on.${name}.branches`, message: 'branches must be a string or a list' });
        }
      }
    } else {
      out.push({ path: '$.on', message: 'on must be a string, list or mapping' });
    }
  }

  const jobs = root.jobs;
  if (!isMap(jobs)) {
    if ('jobs' in root) out.push({ path: '$.jobs', message: 'jobs must be a mapping' });
    return;
  }
  const jobIds = Object.keys(jobs);
  if (jobIds.length === 0) out.push({ path: '$.jobs', message: 'jobs must not be empty' });

  for (const id of jobIds) {
    const job = jobs[id];
    const at = `$.jobs.${id}`;
    if (!isMap(job)) {
      out.push({ path: at, message: 'job must be a mapping' });
      continue;
    }
    const reusable = typeof job.uses === 'string';
    if (!reusable && typeof job['runs-on'] !== 'string' && !Array.isArray(job['runs-on'])) {
      out.push({ path: `${at}.runs-on`, message: 'job needs runs-on (or a reusable-workflow uses)' });
    }
    if (!reusable && job.steps === undefined) {
      out.push({ path: `${at}.steps`, message: 'job needs steps' });
    }
    if (Array.isArray(job.steps)) {
      if (job.steps.length === 0) out.push({ path: `${at}.steps`, message: 'steps must not be empty' });
      job.steps.forEach((step, i) => {
        const sat = `${at}.steps[${i}]`;
        if (!isMap(step)) {
          out.push({ path: sat, message: 'step must be a mapping' });
          return;
        }
        const hasRun = typeof step.run === 'string';
        const hasUses = typeof step.uses === 'string';
        if (!hasRun && !hasUses) out.push({ path: sat, message: 'step needs run or uses' });
        if (hasRun && hasUses) out.push({ path: sat, message: 'step has both run and uses - pick one' });
      });
    } else if (job.steps !== undefined) {
      out.push({ path: `${at}.steps`, message: 'steps must be a list' });
    }
    if (job.needs !== undefined) {
      const needs = Array.isArray(job.needs) ? job.needs : [job.needs];
      if (!Array.isArray(job.needs) && typeof job.needs !== 'string') {
        out.push({ path: `${at}.needs`, message: 'needs must be a string or a list' });
      } else {
        needs.forEach((n, i) => {
          if (typeof n !== 'string') out.push({ path: `${at}.needs[${i}]`, message: 'needs entries must be strings' });
          else if (!(n in jobs)) out.push({ path: `${at}.needs[${i}]`, message: `unknown job "${n}"` });
        });
      }
    }
  }

  // needs-graph cycle detection: DFS from every job over declared, known edges.
  const color = new Map<string, 1 | 2>(); // 1 = visiting, 2 = done
  const visit = (id: string, stack: string[]): void => {
    if (color.get(id) === 2) return;
    if (color.get(id) === 1) {
      const from = stack.indexOf(id);
      out.push({ path: '$.jobs', message: `needs cycle: ${stack.slice(from).concat(id).join(' -> ')}` });
      return;
    }
    color.set(id, 1);
    const job = jobs[id];
    if (isMap(job) && (typeof job.needs === 'string' || Array.isArray(job.needs))) {
      const needs = Array.isArray(job.needs) ? job.needs : [job.needs];
      for (const n of needs) {
        if (typeof n === 'string' && n in jobs) visit(n, [...stack, id]);
      }
    }
    color.set(id, 2);
  };
  for (const id of jobIds) visit(id, []);
}

/** Validate a GitHub Actions workflow YAML document. */
export function validateGhActions(raw: string): GhActionsValidateResult {
  if (!raw.trim()) return { valid: false, issues: [], error: 'A workflow document is required' };
  let root: Node;
  try {
    root = parse(raw, { merge: true }) as Node;
  } catch (e) {
    return { valid: false, issues: [], error: e instanceof Error ? e.message : 'Invalid YAML' };
  }
  const issues: WorkflowIssue[] = [];
  check(root, issues);
  return issues.length === 0
    ? { valid: true, issues: [], error: null }
    : { valid: false, issues, error: null };
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →