DEV Community

Dev Nestio
Dev Nestio

Posted on

Build a Browser-Only OpenAPI / Swagger Validator

Validating OpenAPI documents usually requires server-side tools. I built one that runs purely in the browser.

Try it: https://devnestio.pages.dev/openapi-validator/

Supports

  • OpenAPI 3.0 / 3.1 (JSON or YAML)
  • Swagger 2.0 (JSON or YAML)

What it checks

  • Required root fields (openapi/swagger, info, paths)
  • info.title and info.version present
  • All paths start with /
  • Every operation has a responses block
  • Duplicate operationId values
  • Valid HTTP status codes
  • Server entries have url
  • Schema definitions have a type or composition keyword
function validateV3(doc) {
  required(doc, ["openapi", "info", "paths"], "#");
  const operationIds = new Set();
  for (const [path, item] of Object.entries(doc.paths || {})) {
    for (const method of ["get","post","put","delete","patch"]) {
      const op = item[method];
      if (!op) continue;
      if (!op.responses) addIssue("error", path+"/"+method, "Missing responses", "");
      if (op.operationId) {
        if (operationIds.has(op.operationId)) addIssue("error", path, "Duplicate operationId", op.operationId);
        else operationIds.add(op.operationId);
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

DevNestio — browser-only developer tools.

Top comments (0)