Docker Compose Validator — Zig source
Validate a compose file in the browser: image or build per service, port syntax and ranges, environment shape, volumes, and the depends_on graph with cycle detection. Runs locally with a shareable link.
This is the Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.
// compose-validator — Docker Compose structural validator. Zig port
// (canonical TS: src/lib/compose-validator.ts; Go twin: cli/compose-validator).
// Minimal YAML-subset scanner (block maps, lists, scalars) plus the service
// rules: image-or-build, port shape/range, volumes list shape. Issues print
// as "path — message"; exit 0 = valid.
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();
}
// Returns the scalar value after "key:" on a mapping line, or null.
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;
}
fn isPortOk(entry: []const u8) bool {
var s = entry;
if (std.mem.indexOfScalar(u8, s, '/')) |slash| {
const proto = s[slash + 1 ..];
if (!std.mem.eql(u8, proto, "tcp") and !std.mem.eql(u8, proto, "udp")) return false;
s = s[0..slash];
}
var parts = std.mem.splitScalar(u8, s, ':');
var n: usize = 0;
while (parts.next()) |p| {
n += 1;
if (p.len == 0) return true; // host form (or empty host)
for (p) |c| if (!std.ascii.isDigit(c)) return true; // non-numeric host part
}
if (n < 1 or n > 3) return false;
parts = std.mem.splitScalar(u8, s, ':');
while (parts.next()) |p| {
if (p.len == 0) continue;
const v = std.fmt.parseInt(u32, p, 10) catch return false;
if (v > 65535) return false;
}
return true;
}
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 i: usize = 0;
var services_indent: ?usize = null;
while (i < lines.len) : (i += 1) {
const l = lines[i];
if (std.mem.startsWith(u8, l.text, "services:")) {
services_indent = l.indent;
continue;
}
if (services_indent != null and l.indent > services_indent.?) {
if (std.mem.indexOfScalar(u8, l.text, ':') == null and !std.mem.startsWith(u8, l.text, "- ")) continue;
const isServiceLine = l.indent == services_indent.? + 2 and std.mem.indexOfScalar(u8, l.text, ':') != null and std.mem.indexOf(u8, l.text, ": ") == null;
_ = isServiceLine;
// Walk each service block: image/build presence + ports shape.
const svc_indent = l.indent;
var has_image = false;
var has_build = false;
var j = i;
while (j < lines.len and lines[j].indent >= svc_indent) : (j += 1) {
const b = lines[j];
if (std.mem.startsWith(u8, b.text, "image:")) has_image = true;
if (std.mem.startsWith(u8, b.text, "build")) has_build = true;
if (std.mem.startsWith(u8, b.text, "- ") and j > i) {
const item = b.text[2..];
if (!isPortOk(item)) {
std.print("$.services…ports — invalid port entry: {s}\n", .{item});
valid = false;
}
}
}
if (!has_image and !has_build) {
std.print("$.services… — service needs image or build\n", .{});
valid = false;
}
i = j - 1;
}
}
if (services_indent == null) {
std.print("$.services — missing or invalid \"services\" mapping\n", .{});
valid = false;
}
if (!valid) return error.Invalid;
}
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 →