GitHub Actions Validator — C# 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 C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// gh-actions-validator — GitHub Actions workflow structural validator. C#
// port (canonical TS: src/lib/gh-actions-validator.ts; Go twin:
// cli/gh-actions-validator). Trimmed port: a minimal YAML-subset parser
// (block maps/sequences with `- key: value` items, plain/quoted scalars —
// no flow style, anchors or block scalars; inline lists like needs: [a, b]
// stay literal scalars) plus terse rule checks: root shape, on/jobs
// presence, jobs non-empty, runs-on, steps run-xor-uses, needs known +
// acyclic. .NET standard library only; never throws on bad input — errors
// travel in the result (Error null when the verdict is real).
// Language: C# 12 / .NET 8.
using System;
using System.Collections.Generic;
#nullable enable
namespace CosmoDev.GhActions;
public sealed record Issue(string Path, string Message);
public sealed record GhResult(bool Valid, IReadOnlyList<Issue> Issues, string? Error);
public static class GhActionsValidator
{
private sealed record Line(int Ind, string Text);
private sealed class P { public readonly List<Line> L; public int I; public P(List<Line> l) { L = l; } }
private static bool Seqmark(string t) => t == "-" || t.StartsWith("- ", StringComparison.Ordinal);
private static bool IsMap(object? v) => v is Dictionary<string, object?>;
private static bool IsSeq(object? v) => v is List<object?>;
private static bool IsStr(object? v) => v is string;
private static object? Get(Dictionary<string, object?> m, string k) => m.TryGetValue(k, out var v) ? v : null;
/// <summary>Strip matching quotes; single-quote style un-doubles ''.</summary>
private static string Unquote(string s)
{
int n = s.Length;
if (n < 2 || s[0] != s[n - 1] || (s[0] != '\'' && s[0] != '"')) return s;
var b = new System.Text.StringBuilder();
for (int i = 1; i + 1 < n; i++) { b.Append(s[i]); if (s[0] == '\'' && s[i] == '\'' && i + 2 < n && s[i + 1] == '\'') i++; }
return b.ToString();
}
/// <summary>Cut a line at an unquoted # that starts the line or follows whitespace.</summary>
private static string StripComment(string s)
{
char q = '\0';
for (int i = 0; i < s.Length; i++)
if (q != '\0') { if (s[i] == q) q = '\0'; }
else if (s[i] == '\'' || s[i] == '"') q = s[i];
else if (s[i] == '#' && (i == 0 || s[i - 1] == ' ' || s[i - 1] == '\t')) return s[..i];
return s;
}
/// <summary>Split "key: rest" at the first unquoted colon ending the line or followed by space/tab.</summary>
private static (string Key, string Rest) SplitKey(string t)
{
char q = '\0';
for (int i = 0; i < t.Length; i++)
{
if (q != '\0') { if (t[i] == q) q = '\0'; }
else if (t[i] == '\'' || t[i] == '"') q = t[i];
else if (t[i] == ':' && (i + 1 == t.Length || t[i + 1] == ' ' || t[i + 1] == '\t'))
return (Unquote(t[..i].Trim()), t[(i + 1)..].TrimStart(' ', '\t'));
}
return (Unquote(t.Trim()), "");
}
private static List<Line> BuildLines(string raw)
{
var lines = new List<Line>();
foreach (var rawLine in raw.Split('\n'))
{
var cut = StripComment(rawLine.TrimEnd('\r'));
var t = cut.Trim();
if (t.Length == 0 || t == "---" || t == "...") continue;
lines.Add(new Line(cut.Length - cut.TrimStart().Length, t));
}
return lines;
}
/// <summary>Value for a key (or "- key") consumed at the cursor: inline scalar, child block, child seq or null.</summary>
private static object? ChildVal(P p, int ind, string rest)
{
if (rest.Length > 0) { p.I++; return Unquote(rest); }
if (p.I + 1 < p.L.Count && p.L[p.I + 1].Ind > ind) { p.I++; return ParseBlock(p); }
if (p.I + 1 < p.L.Count && p.L[p.I + 1].Ind == ind && Seqmark(p.L[p.I + 1].Text)) { p.I++; return ParseSeq(p, ind); }
p.I++;
return null;
}
private static void Pair(P p, int ind, Dictionary<string, object?> m)
{ // one "key: value" line of a mapping at ind
var (k, r) = SplitKey(p.L[p.I].Text);
m[k] = ChildVal(p, ind, r);
}
private static object? ParseMap(P p, int ind)
{
var m = new Dictionary<string, object?>();
while (p.I < p.L.Count && p.L[p.I].Ind == ind && !Seqmark(p.L[p.I].Text)) Pair(p, ind, m);
return m;
}
private static object? ParseSeq(P p, int ind)
{
var s = new List<object?>();
while (p.I < p.L.Count && p.L[p.I].Ind == ind && Seqmark(p.L[p.I].Text))
{
string t = p.L[p.I].Text;
int ro = t.Length > 1 && (t[1] == ' ' || t[1] == '\t') ? 2 : 1;
while (ro < t.Length && (t[ro] == ' ' || t[ro] == '\t')) ro++;
int rc = ind + ro; // column the item text starts at
object? v;
if (ro >= t.Length) v = ChildVal(p, ind, "");
else
{
var (k, r) = SplitKey(t[ro..]);
if (r.Length > 0 || t.EndsWith(':')) // inline map item: "- key: value" (+ pairs at rc)
{
var m = new Dictionary<string, object?> { [k] = ChildVal(p, rc, r) };
while (p.I < p.L.Count && p.L[p.I].Ind == rc && !Seqmark(p.L[p.I].Text)) Pair(p, rc, m);
v = m;
}
else { v = Unquote(t[ro..]); p.I++; }
}
s.Add(v);
}
return s;
}
private static object? ParseBlock(P p)
{
var l = p.L[p.I];
return Seqmark(l.Text) ? ParseSeq(p, l.Ind) : ParseMap(p, l.Ind);
}
private static void Visit(Dictionary<string, object?> jobs, string id, List<string> stack,
Dictionary<string, int> color, List<Issue> issues)
{ // needs-graph cycle detection: DFS from every job over declared, known edges
if (color.TryGetValue(id, out int c))
{
if (c == 2) return;
int from = stack.IndexOf(id); // c == 1: id already sits on the current path — cycle
var chain = stack.GetRange(from, stack.Count - from);
chain.Add(id);
issues.Add(new Issue("$.jobs", "needs cycle: " + string.Join(" -> ", chain)));
return;
}
color[id] = 1; stack.Add(id);
if (Get(jobs, id) is Dictionary<string, object?> job && Get(job, "needs") is { } needs)
{
if (needs is string n) { if (jobs.ContainsKey(n)) Visit(jobs, n, stack, color, issues); }
else if (needs is List<object?> ns)
foreach (var x in ns) if (x is string xn && jobs.ContainsKey(xn)) Visit(jobs, xn, stack, color, issues);
}
stack.RemoveAt(stack.Count - 1); color[id] = 2;
}
private static void Check(object? root, List<Issue> issues)
{
if (root is not Dictionary<string, object?> r0) { issues.Add(new Issue("$", "workflow root must be a mapping")); return; }
if (!r0.ContainsKey("jobs")) issues.Add(new Issue("$", "missing required key \"jobs\""));
if (!r0.ContainsKey("on")) issues.Add(new Issue("$", "missing required key \"on\""));
if (Get(r0, "jobs") is not Dictionary<string, object?> jobs)
{ if (r0.ContainsKey("jobs")) issues.Add(new Issue("$.jobs", "jobs must be a mapping")); return; }
if (jobs.Count == 0) issues.Add(new Issue("$.jobs", "jobs must not be empty"));
var stack = new List<string>(); var color = new Dictionary<string, int>();
foreach (var (id, jobObj) in jobs)
{
string at = "$.jobs." + id;
if (jobObj is not Dictionary<string, object?> job) { issues.Add(new Issue(at, "job must be a mapping")); continue; }
bool reusable = Get(job, "uses") is string;
object? ro = Get(job, "runs-on"), steps = Get(job, "steps"), needs = Get(job, "needs");
if (!reusable && !IsStr(ro) && !IsSeq(ro)) issues.Add(new Issue(at + ".runs-on", "job needs runs-on (or a reusable-workflow uses)"));
if (!reusable && !job.ContainsKey("steps")) issues.Add(new Issue(at + ".steps", "job needs steps"));
if (steps is List<object?> sv)
{
if (sv.Count == 0) issues.Add(new Issue(at + ".steps", "steps must not be empty"));
for (int k = 0; k < sv.Count; k++)
{
string sat = $"{at}.steps[{k}]";
if (sv[k] is not Dictionary<string, object?> st) { issues.Add(new Issue(sat, "step must be a mapping")); continue; }
bool hr = Get(st, "run") is string, hu = Get(st, "uses") is string;
if (!hr && !hu) issues.Add(new Issue(sat, "step needs run or uses"));
if (hr && hu) issues.Add(new Issue(sat, "step has both run and uses - pick one"));
}
}
else if (job.ContainsKey("steps")) issues.Add(new Issue(at + ".steps", "steps must be a list"));
if (job.ContainsKey("needs"))
{
if (needs is string n0) { if (!jobs.ContainsKey(n0)) issues.Add(new Issue(at + ".needs[0]", $"unknown job \"{n0}\"")); }
else if (needs is List<object?> ns)
for (int k = 0; k < ns.Count; k++)
if (ns[k] is string xn && !jobs.ContainsKey(xn)) issues.Add(new Issue($"{at}.needs[{k}]", $"unknown job \"{xn}\""));
else issues.Add(new Issue(at + ".needs", "needs must be a string or a list"));
}
}
foreach (var id in jobs.Keys) Visit(jobs, id, stack, color, issues);
}
/// <summary>Validate a GitHub Actions workflow YAML document.</summary>
public static GhResult ValidateGhActions(string raw)
{
if (string.IsNullOrWhiteSpace(raw)) return new GhResult(false, Array.Empty<Issue>(), "A workflow document is required");
var lines = BuildLines(raw);
var p = new P(lines);
var issues = new List<Issue>();
Check(lines.Count > 0 ? ParseBlock(p) : null, issues);
return new GhResult(issues.Count == 0, issues, null);
}
}
// Example: GhActionsValidator.ValidateGhActions("on: push\njobs:\n a:\n needs: b\n runs-on: ubuntu-latest\n steps:\n - run: make\n b:\n needs: a\n runs-on: ubuntu-latest\n steps:\n - run: make").Issues[0] // { "$.jobs", "needs cycle: a -> b -> a" }
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 →