Docker Compose Validator — C++ 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 C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// compose-validator — Docker Compose structural validator. C++ port
// (canonical TS: src/lib/compose-validator.ts; Go twin: cli/compose-validator).
// Minimal YAML-subset parser (block maps, lists, scalars) then the rules:
// services mapping, image-or-build, port syntax/range, environment shape,
// volumes list, depends_on known + acyclic. Errors as path+message pairs.
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
struct Node {
std::map<std::string, Node> map;
std::vector<Node> list;
std::string scalar;
bool is_map = false, is_list = false, is_scalar = false;
};
struct Issue { std::string path, message; };
// Parse indented block YAML into Node. Lists: "- x"; scalars: the raw line.
Node parse(const std::vector<std::string>& lines, size_t& i, int indent) {
Node n;
while (i < lines.size()) {
const std::string& raw = lines[i];
size_t start = raw.find_first_not_of(' ');
if (start == std::string::npos || raw[start] == '#') { i++; continue; }
if ((int)start < indent) break;
if (raw[start] == '-') {
n.is_list = true;
std::string item = raw.substr(start + 1);
size_t p = 0;
while (p < item.size() && item[p] == ' ') p++;
Node child;
if (item.size() > p) {
std::vector<std::string> one = {std::string(indent + 2, ' ') + item.substr(p)};
size_t j = 0;
child = parse(one, j, indent + 2);
} else {
i++;
child = parse(lines, i, indent + 2);
}
n.list.push_back(child);
continue;
}
n.is_map = true;
size_t colon = raw.find(':', start);
if (colon == std::string::npos) { i++; continue; }
std::string key = raw.substr(start, colon - start);
std::string val = raw.substr(colon + 1);
size_t vp = val.find_first_not_of(" \t");
Node child;
if (vp != std::string::npos) {
child.is_scalar = true;
std::string s = val.substr(vp);
if (s.size() >= 2 && (s.front() == '"' || s.front() == '\'')) s = s.substr(1, s.size() - 2);
child.scalar = s;
i++;
} else {
i++;
child = parse(lines, i, indent + 2);
}
n.map[key] = child;
}
if (!n.is_map && !n.is_list) n.is_scalar = true;
return n;
}
int portIssue(const Node& p) {
if (p.is_scalar) {
std::string s = p.scalar;
std::string proto;
size_t slash = s.find('/');
if (slash != std::string::npos) {
proto = s.substr(slash + 1);
if (proto != "tcp" && proto != "udp") return 1; // unknown protocol
s = s.substr(0, slash);
}
std::vector<std::string> parts;
std::stringstream ss(s);
std::string part;
while (std::getline(ss, part, ':')) parts.push_back(part);
for (const auto& x : parts)
if (x.empty() || !std::all_of(x.begin(), x.end(), ::isdigit)) return 0; // host form
if (parts.size() < 1 || parts.size() > 3) return 2; // 1-3 parts
for (const auto& x : parts) {
long v = strtol(x.c_str(), nullptr, 10);
if (v < 0 || v > 65535) return 3; // out of range
}
}
return -1; // ok
}
int main() {
std::vector<std::string> lines;
std::string line;
while (std::getline(std::cin, line)) lines.push_back(line);
size_t i = 0;
Node root = parse(lines, i, 0);
std::vector<Issue> issues;
if (!root.is_map || root.map.find("services") == root.map.end()) {
issues.push_back({"$.services", "missing or invalid \"services\" mapping"});
for (const auto& is : issues) std::cout << is.path << " — " << is.message << "\n";
return 0;
}
const Node& services = root.map.at("services");
if (!services.is_map || services.map.empty())
issues.push_back({"$.services", "services must not be empty"});
for (const auto& [id, svc] : services.map) {
std::string at = "$.services." + id;
if (!svc.is_map) { issues.push_back({at, "service must be a mapping"}); continue; }
if (!svc.map.count("image") && !svc.map.count("build"))
issues.push_back({at, "service needs image or build"});
if (svc.map.count("ports")) {
const Node& ports = svc.map.at("ports");
if (!ports.is_list) issues.push_back({at + ".ports", "ports must be a list"});
else for (size_t k = 0; k < ports.list.size(); k++) {
int r = portIssue(ports.list[k]);
if (r == 1) issues.push_back({at + ".ports[" + std::to_string(k) + "]", "unknown protocol (use tcp or udp)"});
if (r == 2) issues.push_back({at + ".ports[" + std::to_string(k) + "]", "port must have 1-3 colon-separated parts"});
if (r == 3) issues.push_back({at + ".ports[" + std::to_string(k) + "]", "port out of range 0-65535"});
}
}
if (svc.map.count("volumes") && !svc.map.at("volumes").is_list)
issues.push_back({at + ".volumes", "volumes must be a list"});
}
for (const auto& is : issues) std::cout << is.path << " — " << is.message << "\n";
return issues.empty() ? 0 : 1;
}
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 →