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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Level Up Your API Design: 8 Principles for World-Class REST APIs
  • API Design First: AsyncAPI in .Net
  • Rate Limiting Strategies for Efficient Traffic Management

Trending

  • How to Submit a Post to DZone
  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  • Implementing Secure API Gateways for Microservices Architecture
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  1. DZone
  2. Data Engineering
  3. Databases
  4. Hands-on With Spectral: Using API Linting for Better API Design and Governance [Video]

Hands-on With Spectral: Using API Linting for Better API Design and Governance [Video]

Axway Catalysts Erik Wilde and Chris Wood walk you through seven steps to learn how to use Spectral for an increasingly powerful set of tasks.

By 
Erik Wilde user avatar
Erik Wilde
·
May. 20, 21 · Presentation
Likes (3)
Comment
Save
Tweet
Share
10.1K Views

Join the DZone community and get the full member experience.

Join For Free

Spectral is an open-source API linting tool that allows users to write rules which then can be used to check API descriptions. Typically, these would be in description languages such as OpenAPI, AsyncAPI, or Swagger. If you’re interested in an overview and an introduction, check out this interview with Spectral maintainer Phil Sturgeon where he walks us through the history of Spectral, what it does, and how it can be used.

7 Steps

In this article and presentation, we’re diving a bit deeper. Axway Catalyst, Chris Wood walks us through seven steps in which you can learn how to use Spectral for an increasingly powerful set of tasks. This means you can start making Spectral part of your API platform and your API governance with very little effort and can level up the way you’re using it over time.

At its core, Spectral is very simple: As input, it takes an API description (OpenAPI, AsyncAPI, Swagger, …) and a set of rules, and then checks whether the API description follows the rules. This can help with automating API guidelines to some extent and depending on how much effort you put into it, you can check for rather basic things such as that every resource should have a description associated with it, or you can get more sophisticated to cover more of your API guidelines.

If you’re interested in working through the examples, all demo code used in the video is freely available on GitHub to get you started. For a full presentation on how everything works please check out the video, in this blog post we simply walk you through what the individual steps are doing.

1. Out Of the Box

Spectral comes with built-in rules that go beyond what the API specifications require. This means that you can use Spectral out of the box without adding any rules yourself. You then get the result of checking API descriptions about Spectral’s default rules, and that can already catch some issues with API descriptions.

spectral lint examples/petstore-openapi-scenario-1.yaml


2. Create/Extend Rulesets

Many users want to change and/or add rules, and for this Spectral allows to specify additional rulesets. Using Spectral this way now allows you to specify additional rulesets that Spectral will use to check your API descriptions.

spectral lint --ruleset rulesets/petstore-scenario-2.yaml examples/petstore-openapi-scenario-2.yaml


3. Turning Off Rules

Some rules of rulesets that you are using may not be to your liking. For this case, Spectral supports selectively switching off certain rules. This means that you can use an existing ruleset without changing it, but you can still control how it’s used by switching off certain rules.
extends:

extends:
  - ./petstore-scenario-2.yaml
rules:
  operation-description: off
  oas3-unused-components-schema: off


4. Built-in Functions

Spectral has a number of built-in functions which you can use to implement rules yourself. These functions allow you to perform operations and will return success or failure results. In this example, we’re using a regular expression function for matching a string against a regular expression.

extends:
  - ./petstore-scenario-3.yaml
rules:
  hyphen-delimited-uris:
    message: "Words in the path must be lowercase and delimited by a hyphen or concatenated, or be a path parameter"
    severity: error
    given: "$.paths[*]~"
    then:
      function: pattern
      functionOptions:
        match: "^(?:\/(?:[a-z]+(?:-[a-z]+|)+|\\{[a-zA-Z]+\\}))+$"


5. Custom Functions

 If what you want to check in a rule is not covered by Spectral’s features or built-in functions, you can write your own functions (which have to be implemented in JavaScript). You can then use these custom functions in rules in the same way as built-in functions.

extends:
  - ./petstore-scenario-4.yaml
functions: [responses]
rules:
  operation-2xx-response: off
  default-response-fallback:
    message: "Response object does not have 2xx operation or default set"
    given: "$.paths.[*].[*].responses"
    severity: error
    then:
      function: responses


6. Organization-level rulesets

Moving on from using Spectral individually, the next step is to manage rulesets so that they are organized to be applied by different teams, and possible for different types of APIs (e.g., private/partner/public) or for different maturity stages of APIs (e.g., prototype vs. publicly available). This is where Spectral becomes a powerful tool for API governance, making it possible for an organization to encode and check guidelines in a way that can be incorporated into development processes.

7. Automating Spectral

 Building on the previous point, the last point now is to make Spectral checks part of your development pipelines, meaning that checks will be done automatically every time you make changes to the API descriptions. This means that there is no time-consuming manual review process, and teams get alerted of problems with the API descriptions as quickly as possible.

Video Presentation

These are the seven steps how you can take Spectral from a simple out-of-the-box tool to one that can help with API governance and API guidelines. The nice aspect here is that getting started is as simple as installing Spectral, and all other steps then can be done when necessary.

If you are interested in more details covered in the full presentation, here’s Chris Wood walking you through all the steps, the code from the GitHub demo repository, and explaining more details along the way.


API Design

Published at DZone with permission of . See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Level Up Your API Design: 8 Principles for World-Class REST APIs
  • API Design First: AsyncAPI in .Net
  • Rate Limiting Strategies for Efficient Traffic Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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