GitHub Actions Validator — Swift 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 Swift implementation — the same logic the interactive tool runs, in a shareable, citable form.
// gh-actions-validator — GitHub Actions workflow validator. Swift port
// (canonical TS: src/lib/gh-actions-validator.ts; Go twin: cli/gh-actions-validator).
// Minimal YAML-subset parser (block maps, lists, scalars) then the rules:
// on/jobs presence, runs-on (or reusable uses), steps run-xor-uses, needs
// known + acyclic. Issues print as "path — message"; "valid" when clean.
import Foundation
final class Node {
var map: [String: Node] = [:]
var list: [Node] = []
var scalar: String?
}
func parse(_ lines: [(indent: Int, text: String)], _ i: inout Int, _ indent: Int) -> Node {
let n = Node()
while i < lines.count {
let (ind, t) = lines[i]
if ind < indent { break }
if t.hasPrefix("- ") {
let child = Node()
child.scalar = String(t.dropFirst(2)).trimmingCharacters(in: CharacterSet(charactersIn: " \t\"'"))
n.list.append(child)
i += 1
continue
}
guard let colon = t.firstIndex(of: ":") else { i += 1; continue }
let key = String(t[..<colon]).trimmingCharacters(in: .whitespaces)
let rest = String(t[t.index(after: colon)...]).trimmingCharacters(in: .whitespaces)
if rest.isEmpty {
i += 1
n.map[key] = parse(lines, &i, indent + 2)
} else {
i += 1
let child = Node()
child.scalar = rest.trimmingCharacters(in: CharacterSet(charactersIn: "\"'"))
n.map[key] = child
}
}
return n
}
func needsNames(_ job: Node) -> [String] {
if let needs = job.map["needs"] {
if !needs.list.isEmpty { return needs.list.compactMap(\.scalar) }
if let s = needs.scalar { return [s] }
}
return []
}
func needsCycle(_ jobs: [String: Node]) -> String? {
var color: [String: Int] = [:]
func visit(_ id: String, _ stack: [String]) -> String? {
if color[id] == 2 { return nil }
if color[id] == 1 {
let from = stack.firstIndex(of: id) ?? 0
return (stack[from...] + [id]).joined(separator: " -> ")
}
color[id] = 1
if let job = jobs[id] {
for n in needsNames(job) where jobs[n] != nil {
if let c = visit(n, stack + [id]) { return c }
}
}
color[id] = 2
return nil
}
for id in jobs.keys { if let c = visit(id, []) { return c } }
return nil
}
let src = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8) ?? ""
let lines: [(Int, String)] = src.split(separator: "\n").map { (line: Substring) -> (Int, String) in
let s = String(line)
let trimmed = s.trimmingCharacters(.whitespaces)
return (s.count - trimmed.count, trimmed)
}.filter { !$0.1.isEmpty && !$0.1.hasPrefix("#") }
var i = 0
let root = parse(lines, &i, 0)
var issues: [(String, String)] = []
if root.map["on"] == nil { issues.append(("$", "missing required key \"on\"")) }
guard let jobs = root.map["jobs"] else {
issues.append(("$", "missing required key \"jobs\""))
for (p, m) in issues { print("\(p) — \(m)") }
exit(0)
}
if jobs.map.isEmpty { issues.append(("$.jobs", "jobs must not be empty")) }
for (id, job) in jobs.map {
let at = "$.jobs.\(id)"
let reusable = job.map["uses"]?.scalar != nil
if !reusable && job.map["runs-on"] == nil {
issues.append(("\(at).runs-on", "job needs runs-on (or a reusable-workflow uses)"))
}
if !reusable && job.map["steps"] == nil {
issues.append(("\(at).steps", "job needs steps"))
}
if let steps = job.map["steps"], !steps.list.isEmpty {
for (k, step) in steps.list.enumerated() {
let hasRun = step.map["run"]?.scalar != nil
let hasUses = step.map["uses"]?.scalar != nil
if !hasRun && !hasUses { issues.append(("\(at).steps[\(k)]", "step needs run or uses")) }
if hasRun && hasUses { issues.append(("\(at).steps[\(k)]", "step has both run and uses - pick one")) }
}
}
needsNames(job).enumerated().forEach { (k, n) in
if jobs.map[n] == nil { issues.append(("\(at).needs[\(k)]", "unknown job \"\(n)\"")) }
}
}
if let cycle = needsCycle(jobs.map) { issues.append(("$.jobs", "needs cycle: \(cycle)")) }
for (p, m) in issues { print("\(p) — \(m)") }
if issues.isEmpty { print("valid") }
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 →