<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carmine DiMascio</title>
    <description>The latest articles on DEV Community by Carmine DiMascio (@cdimascio).</description>
    <link>https://dev.to/cdimascio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F123553%2Ff28c6390-e932-492c-9691-280a6cf4d114.jpeg</url>
      <title>DEV Community: Carmine DiMascio</title>
      <link>https://dev.to/cdimascio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cdimascio"/>
    <language>en</language>
    <item>
      <title>Build an API with Node.js: Automatically validate API requests using an OpenAPI 3 specification</title>
      <dc:creator>Carmine DiMascio</dc:creator>
      <pubDate>Tue, 26 Mar 2019 14:01:08 +0000</pubDate>
      <link>https://dev.to/cdimascio/build-an-api-with-nodejs-automatically-validate-api-requests-using-an-openapi-3-specification-2gl5</link>
      <guid>https://dev.to/cdimascio/build-an-api-with-nodejs-automatically-validate-api-requests-using-an-openapi-3-specification-2gl5</guid>
      <description>&lt;p&gt;&lt;a href="http://github.com/cdimascio/express-openapi-validator" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsx5qv0lrvxciowylxe7g.png" alt="express-openapi-validator" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To leverage and understand an API, documentation becomes a critical necessity. Specifications like &lt;a href="https://github.com/OAI/OpenAPI-Specification" rel="noopener noreferrer"&gt;OpenAPI 3 &lt;/a&gt;enable APIs to be described in a standard format that can easily be rendered e.g. as interactive, HTML documentation.&lt;/p&gt;

&lt;p&gt;In this article, we’ll see how an OpenAPI specification can also be used to &lt;strong&gt;automatically validate API requests!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ll build the API server using Node.js and Express. We’ll utilize &lt;a href="https://github.com/cdimascio/express-openapi-validator" rel="noopener noreferrer"&gt;express-openapi-validator&lt;/a&gt; to automatically validate API requests using an OpenAPI 3 specification.&lt;/p&gt;

&lt;p&gt;Let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an Express application for our simple API
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt; let’s create a simple Express application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshqr8a3tvf7jggafr63a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshqr8a3tvf7jggafr63a.png" width="800" height="1002"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running the above code launches an API server that exposes the following routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GET /v1/pets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;POST /v1/pets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GET /v1/pets/:id&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: &lt;em&gt;The APIs return values are contrived and not relevant to this tutorial.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an OpenAPI spec to describe our API
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(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 &lt;a href="https://github.com/cdimascio/express-openapi-validator/blob/master/openapi.yaml" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/em&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xy12b9lfgkpd2jh5w1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xy12b9lfgkpd2jh5w1n.png" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ueibkxd5rsx3lzsl3br.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ueibkxd5rsx3lzsl3br.png" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll also add the NewPets component to our OpenAPI 3 spec.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifrmoafjdhszv8x1v43t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifrmoafjdhszv8x1v43t.png" width="642" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrate automatic request validation with Express-OpenAPI-Validator
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Finally&lt;/strong&gt;, we’ll make a few minor code adjustments to enable our API server to automatically validate API requests using our OpenAPI 3 specification.&lt;/p&gt;

&lt;p&gt;The code adjustments include the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Require &lt;a href="https://github.com/cdimascio/express-openapi-validator" rel="noopener noreferrer"&gt;express-openapi-validator&lt;/a&gt; — a package to automatically validate routes defined in Express against an OpenAPI 3 spec&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the OpenApiValidator onto our express application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provide an Express error handler to customize our error responses&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After making these changes, our final code is as follows:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note, steps 1, 2, and 3 indicating the new code that’s been added)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0leonxlr309ydhzptvdx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0leonxlr309ydhzptvdx.png" width="800" height="1140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start the server, then…&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it out
&lt;/h2&gt;

&lt;p&gt;Let’s execute some API requests with &lt;strong&gt;curl&lt;/strong&gt; and observe the automatic request validation in action.&lt;/p&gt;

&lt;p&gt;Let’s try &lt;strong&gt;GET /v1/pets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxj3qky787lpditfvv63.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxj3qky787lpditfvv63.png" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Returns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc1jnm4cz12i8tguy144w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc1jnm4cz12i8tguy144w.png" width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s try &lt;strong&gt;POST /v1/pets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9lsgiqwf7ks9eakrnbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9lsgiqwf7ks9eakrnbc.png" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Returns&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuanmbhli3szovoxyga2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuanmbhli3szovoxyga2l.png" width="800" height="489"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The full source code for this example can be found &lt;a href="https://github.com/cdimascio/express-openapi-validator/blob/master/example/app.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you dig &lt;a href="https://github.com/cdimascio/express-openapi-validator" rel="noopener noreferrer"&gt;express-openapi-validator&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/cdimascio/express-openapi-validator" rel="noopener noreferrer"&gt;Star&lt;/a&gt; it on Github!&lt;/p&gt;

</description>
      <category>node</category>
      <category>openapi</category>
      <category>express</category>
      <category>rest</category>
    </item>
  </channel>
</rss>
