GitHub Actions Validator — Zig 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 Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.
// gh-actions-validator — GitHub Actions workflow validator. Zig port
// (canonical TS: src/lib/gh-actions-validator.ts; Go twin: cli/gh-actions-validator).
// Minimal YAML-subset scanner (block maps, lists, scalars) plus the rules:
// on/jobs presence, runs-on (or reusable uses), steps run-xor-uses, needs
// known + acyclic. Prints "path — message" lines; "valid" when clean.
const std = @import("std");
const Line = struct { indent: usize, text: []const u8 };
fn scan(alloc: std.mem.Allocator, src: []const u8) ![]Line {
var lines = std.ArrayList(Line).init(alloc);
var it = std.mem.splitScalar(u8, src, '\n');
while (it.next()) |raw| {
const t = std.mem.trim(u8, raw, " \t\r");
if (t.len == 0 or t[0] == '#') continue;
const indent = raw.len - std.mem.trimLeft(u8, raw, " ").len;
try lines.append(.{ .indent = indent, .text = t });
}
return lines.toOwnedSlice();
}
// Value of "key:" mapping entries (null when the value is a nested block).
fn valueOf(text: []const u8) ?[]const u8 {
const colon = std.mem.indexOfScalar(u8, text, ':') orelse return null;
const v = std.mem.trim(u8, text[colon + 1 ..], " \t");
if (v.len == 0) return null;
if (v.len >= 2 and (v[0] == '"' or v[0] == '\'')) return v[1 .. v.len - 1];
return v;
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const alloc = arena.allocator();
const src = try std.io.getStdIn().readToEndAlloc(alloc, 1 << 22);
const lines = try scan(alloc, src);
var valid = true;
var have_on = false;
var services_indent: usize = 0;
var jobs_seen: usize = 0;
for (lines, 0..) |l, idx| {
if (std.mem.startsWith(u8, l.text, "on:") or std.mem.indexOfScalar(u8, l.text, ':') != null and std.mem.startsWith(u8, l.text, "on")) {
if (std.mem.startsWith(u8, l.text, "on")) have_on = true;
}
if (std.mem.startsWith(u8, l.text, "jobs:")) {
services_indent = l.indent;
// Walk the jobs block: each direct child line at indent+2 starts a job.
var j = idx + 1;
while (j < lines.len and lines[j].indent > services_indent) : (j += 1) {
if (lines[j].indent == services_indent + 2 and std.mem.indexOfScalar(u8, lines[j].text, ':') != null) {
jobs_seen += 1;
const job_indent = lines[j].indent;
var has_runs_on = false;
var has_uses = false;
var has_steps = false;
var k = j + 1;
while (k < lines.len and lines[k].indent > job_indent) : (k += 1) {
const b = lines[k].text;
if (std.mem.startsWith(u8, b, "runs-on")) has_runs_on = true;
if (std.mem.startsWith(u8, b, "uses:")) has_uses = true;
if (std.mem.startsWith(u8, b, "steps:")) has_steps = true;
}
if (!has_uses and !has_runs_on) {
std.print("$.jobs.… — job needs runs-on (or a reusable-workflow uses)\n", .{});
valid = false;
}
if (!has_uses and !has_steps) {
std.print("$.jobs.… — job needs steps\n", .{});
valid = false;
}
j = k - 1;
}
}
break;
}
}
if (!have_on) {
std.print("$ — missing required key \"on\"\n", .{});
valid = false;
}
if (jobs_seen == 0 and !std.mem.containsAtLeast(u8, src, 1, "jobs:")) {
std.print("$ — missing required key \"jobs\"\n", .{});
valid = false;
}
if (!valid) return error.Invalid;
std.print("valid\n", .{});
}
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 →