A simple way to write advanced API Standards #1377
Unanswered
acunniffe
asked this question in
Optic Feedback
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The more a team's API standards and best practices can be checked with automation, the easier it is for developers to design APIs that meet the standards. This lets API reviewers focus their feedback on the actual product concerns, not the basic design or use of OpenAPI. After helping many teams like Snyk build more advanced API standards than they could with Spectral, we've learned a lot about how to improve the process of automating your API standards.
Here's a screenshot of some API Standards written with Optic -- and a live preview of the documentation the engine generates for you!
Design principles
Meeting Rule Authors in the API Domain
Optic lets you write rules about the entities in your OpenAPI specification
Operation
,Parameter
,Property
,Response
...etcImagine we want to write a rule that says all Operations MUST have an
operationId
.First we call
Standard.Operation
:If you're using a Typescript IDE, you'll notice argument you pass into
Standard.Operation
has the same keys as an Operation in OpenAPI. This makes it easy for developers who work with OpenAPI to understand where each rule runs:To write our rule about
operationId
we just add arequirement
in our API standard. We did not even have to think about JSON Path or write$.paths[*][get,put,post,delete,options,head,patch,trace].operationId
Imagine our team generates client SDKs and changing the
operationId
is a breaking change? We can write achanged
rule right there, next to ourrequirement
rule:Optic even self-documents the standard this rule tests for (alongside some of our other standards):
Advanced API Standards
Before Optic, most of the large teams we worked with struggled to automate their most important API standards. The Spectral rules they wrote could help them write valid OpenAPI and require certain metadata, enforce consistent naming, and a few other basic standards. They struggled to write rules for things like Pagination, long-running requests, and their versioning policies.
1. Advanced rules need to apply hierarchically
Let's say we want to describe our team's standard for Get Operations that lookup a resource by its ID. That standard is made up of many assertions, which are tested by multiple rules. We do not want these rules to run on our entire OpenAPI spec -- they should only run on subtrees that match the standard we are testing for. That's what
applyWhen
is for:Every
parameter
andresponse
standard we write will run on operations that match the predicate we wrote. You can useapplyWhen
with all the entities we write standards for. For example one team wrote a standard that helped developers usex-extensible-enum
extension properly. It runs on every schema (and property schema) in the specification, that uses the extension:Pretty powerful stuff.
2. The human-side
We've heard a common story from many of the team's who adopted Optic this year. At one time they were automating more advanced API standards, but rolled them back because they were too disruptive. Existing OpenAPI operations were failing the new checks, but there was no way to "fix" them because those "fixes" would be breaking changes. That's fundamentally why "linting" is the wrong way to think about OpenAPI validation -- you can fix lint errors in code and get to 0...with APIs it's often impossible to get to 0 without breaking the user.
Most teams have existing APIs, and because we live in the real world their API standards are going to change over time as their team learns-- that's a good thing. Optic makes it possible to apply your API standards "forward-only" -- that is to the new surface area, while letting the existing surface area be.
This has made it safe to write more advanced standards without worrying about upsetting teams who have existing APIs to maintain. In fact we've measured the before and after, and teams using forwards-only rules are automating 8x as many API standards as they had before.
Developer preview
Right now the new rule authoring is in a developer-preview. We're working out a design challenges and awaiting some feedback before we launch it. Please share your feedback, requirements, and feature requests
Check out a live example on CodeSandbox
Share feedback in this thread 👇
Book office hours if you have ideas to share
Beta Was this translation helpful? Give feedback.
All reactions