DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Creating a Secure REST API in Node.js
  • A Developer's Guide to Chrome Extension Manifest V3 Declarative Net Request API
  • From Microservices to Agent Services: The Next Architectural Shift
  • The Bill You Didn't See Coming

Trending

  • AI Agents for Software Engineering and Autonomous Development Workflows
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • Three Measured Outcomes of Manually Scaling Fleet in Prod

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. APIs With Node.js and Express: Automatically Validate API Requests Using an OpenAPI 3 Specification

APIs With Node.js and Express: Automatically Validate API Requests Using an OpenAPI 3 Specification

In this article, we’ll see how an OpenAPI specification can be used to automatically validate API requests.

By 
Carmine DiMascio
·
Updated Apr. 01, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Web APIs are central to today’s applications. They provide interfaces that are easily consumed by apps written in any programming language on any platform. They make complex technologies simple by exposing easy-to-use, intuitive interfaces that enable app developers to weave together incredible experiences quickly.

To leverage and understand an API, documentation becomes a critical necessity. Specifications like OpenAPI 3 enable APIs to be described in a standard format that can easily be rendered e.g. as interactive, HTML documentation.

In this article, we’ll see how an OpenAPI specification can also be used to automatically validate API requests.Image title

We’ll build the API server using Node.js and Express and we’ll utilize express-openapi-validator to automatically validate API requests using an OpenAPI 3 specification.

Let’s get started.

Create an Express Application for Our Simple API

First, let’s create a simple Express application.

Running the above code launches an API server that exposes the following routes:

  • GET /v1/pets 
  • POST /v1/pets
  • GET /v1/pets/:id

Note: The APIs return values are contrived and not relevant to this tutorial.

Create an OpenAPI Spec to Describe Our API

Now that we have written our simple API, let’s add some validation. But, instead of writing a bunch of validation code, we’ll describe our API by creating an OpenAPI 3 specification.

(I’ll assume you know how to create an OpenApi spec, hence I’ll describe only the relevant snippets. If you’d like to see the full spec, go here).

Let’s make it a requirement that requests to GET /v1/pets must provide the query parameter, limit. Let’s also require that limit be an integer with a value greater than zero.

Let’s also make it a requirement that requests to POST /v1/pets must provide a JSON body containing a required field, name.

We’ll also add the NewPets component to our OpenAPI 3 spec.

Integrate Automatic Request Validation With Express-OpenAPI-Validator

Finally, we’ll make a few minor code adjustments to enable our API server to automatically validate API requests using our OpenAPI 3 specification.

The code adjustments include the following:

  1. Require express-openapi-validator — a package to automatically validate routes defined in Express against an OpenAPI 3 spec
  2. Install the OpenApiValidator onto our express application
  3. Provide an Express error handler to customize our error responses

After making these changes, our final code is as follows:

(Note, steps 1, 2, and 3 indicating the new code that’s been added)

Start the server, then…

Try It Out

Let’s execute some API requests with curl and observe the automatic request validation in action.

Let’s try GET /v1/pets

Returns:

Let’s try POST /v1/pets 

Returns:

The full source code for this example can be found here.

The express-openapi-validator lives here.

API Express Requests Node.js

Opinions expressed by DZone contributors are their own.

Related

  • Creating a Secure REST API in Node.js
  • A Developer's Guide to Chrome Extension Manifest V3 Declarative Net Request API
  • From Microservices to Agent Services: The Next Architectural Shift
  • The Bill You Didn't See Coming

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook