Let's define API Style Guides collaboratively on examples.
This repository contains executable examples of API Design Style Guide Rules (API Blueprint, OpeAPI/Swagger, ... code snippets) to enable vendor independence, all the time up-to-date documentation tested in CI and preventing regressions caused by unwanted changes in behavior of underlying Style Guide engine.
- Catch the idea
- Create a written free-form verbal documentation, formal specification, white paper etc..
- Collect all the PDFs, Google Docs, Markdowns, READMEs, rtfs and docs you already have in your drawers
- Identify and isolate single rules in the textual styleguide you created on step 1
- Add good and bad examples for every single new rule using API Description Language (OAS/Swagger, API Blueprint, ...)
- install
node.js
v6+ - install dependencies
$ npm install
Fork this repo.
Run:
$ ./scripts/init name-of-your-new-rule-directory
The following structure should be created in /styleguides
directory
├ main-rule-dir
│ └examples - examples directory
│ ├ bad - bad examples directory
│ │ ├ bad-example1
│ │ │ ├ api-description-document - API description document that should fail
│ │ │ └ meta.yaml - configuration and metadata for the example
│ │ └ bad-example2
│ │ ├ api-description-document
│ │ └ meta.yaml
│ └ good
│ └ good-example1 - good examples directory
│ ├ api-description-document - API description document that should pass
│ └ meta.yaml - configuration and metadata for the example
├ functions.js - validation function definition
├ rule.yaml - rule configuration and metadata
└ README.md - generated readme file (do not edit)
Write at least one good and one bad API description document.
Write the validation function to functions.js
Function must be defined by declaration. Function must be exported by
module.exports = {
myFunction,
};
Each function has one input parameter - minim element
if minim
is set to true
in rule configuration or (deprecated) JSON object found on desired
path in
refract tree.
Function is not executed if no object is found.
The function must return true if validation passes or a string which describes reason of failure if validation fails.
You can require
npm packages (after installing them to node_modules
) or libraries.
lodash package is available out of the box.
Any I/O are disallowed for security reasons.
Write the rule configuration to rule.yaml
title: title of this rule
intent: intent/description of this rule
allowedTargets: list of allowed targets
minim: true
functionName: name of the function defined in functions.js file
allowedTargets
is the list of targets this rule can be applied to.
api
meta
title
copy
resourceGroup
resourceGroup.title
resourceGroup.copy
resource
resource.title
resource.copy
resource.href
resource.hrefVariables
transition
transition.title
transition.method
transition.copy
transition.hrefVariables
transition.requestAndResponse
request
request.copy
request.messageBody
request.messageBodySchema
request.headers
request.header
response
response.statusCode
response.copy
response.messageBody
response.messageBodySchema
response.headers
response.header
header
Write the build configuration to ./build/build.yaml
title: styleguide title
description: description of this styleguide
rules:
- name-of-your-new-rule-directory
by running
npm run test
- every rule defined in
./build/build.yaml
is tested against its good and bad example - README.md file is generated for each rule defined in
./build/build.yaml
in its directory - compound README.md file is generated to
./build/README.md
- bundle with all functions and its dependencies defined in
./build/build.yaml
is generated to./build/functions.js
./build/rules.json
is generated
rules.json
and functions.js
bundle can be used for Apiary Styleguides or
Apiary CLI
If you feel that your rule(s) could beneficial for others, feel free to submit PR.