Validating an OpenAPI or Swagger Spec: What Schema Validation Catches (and What It Can't)

By · Published · Updated

The OpenAPI Initiative publishes a JSON Schema for each version of the spec: one for Swagger 2.0, and one each for OpenAPI 3.0, 3.1, and 3.2. Validating a document means checking it against the schema for the version it declares. That catches most of the mistakes that make codegen tools choke or documentation render wrong. It doesn't catch all of them, because some rules compare one part of the document with another, and a JSON Schema can't express that. The validator on this site runs both kinds of check, and this guide covers what each one finds.

Mistakes the schema catches

These are the ones that come up again and again, usually in specs written by hand or edited without a linter:

One mistake, one message

The official schemas describe a lot of places as "either a $ref or the real object", and parameters as "one of path, query, header, or cookie". A standard validator reports a failure for every alternative that didn't match. So a single missing description comes back as three errors, and a parameter with in: body comes back as eight, most of them about alternatives you never meant to use. This tool works out which alternative you were writing and reports only its problem. For the in: body case that's one line saying in must be one of path, query, header, or cookie. Locations are written the way you'd reach them in code, like paths["/pets/{id}"].get.parameters[0], so you can find the spot without decoding a JSON pointer.

What a JSON Schema can't check

A schema describes what each part of a document is allowed to look like, one part at a time. Some rules are about how parts relate to each other, so the validator checks them separately:

Advertisement

One gap to know about in 3.1 and 3.2

In 3.1, Schema Objects became full JSON Schema 2020-12, and the official 3.1 and 3.2 schemas check only that each one is an object or a boolean. They leave the contents to a separate "dialect" schema. So a type: strng inside components.schemas is caught in a 3.0 document but not in a 3.1 one. Everything outside Schema Objects, including paths, operations, parameters, and responses, is checked fully in every version.

The version field, and a YAML trap

The validator picks the schema from the openapi or swagger field, so it has to be exact. openapi needs a patch number (3.1.0, not 3.1). In YAML, write it in quotes. openapi: 3.0 without quotes is read as the number 3, which isn't a version at all, and every tool downstream will reject it for a reason that's hard to spot.

What a valid spec doesn't tell you

Passing validation means the document is a well-formed OpenAPI description. It doesn't mean it describes the API you actually run. A spec can be perfectly valid and still say a field is a string when the server sends a number. Catching that takes real responses, which is what API contract testing is about.

Everything runs in your browser. The YAML parser and the schema for your document's version download the first time you validate, and nothing you paste is sent anywhere.

Advertisement

Try the OpenAPI / Swagger Validator →

Related tools & guides

Part of API Contract Drift: Catching Breaking Response Changes