Spectral is a linting engine that helps you define custom rules and enforce them on JSON and YAML files. Postman supports Spectral v6 rules for the configurable API Governance and Security rules for your team. Postman also supports ES6 syntax and CommonJS syntax for custom functions configurable in custom governance rules.
Spectral checks that the APIs defined in OpenAPI documents conform to API design guidelines using a specific set of rules. For example, you can use Spectral to check that all properties of all data models are in camel case, or that all operations have a summary.
A Spectral rule targets a given location using a JSON Path Plus expression, then tests the values it finds with a function. It returns an error if the values don’t conform to the rule.
This example shows how to check that the name of an API doesn’t contain the word “API,” regardless of case (for instance, “api” or “Api”):
First, the rule targets the title property of the info object located at the root ($) of the document with the JSON Path Plus expression $.info.title. In then, the function named pattern checks whether the value of the title property doesn’t match (functionOptions.notMatch) the regular expression looking for the word api in any case (/\b(api)\b/i).
A Spectral document (often called a ruleset) has a rules property that can have one or more rules. Spectral identifies each rule with a key, which can be any JSON-compatible string.
Postman supports many of Spectral’s features, though not all.
Whether it’s created within Postman or imported from another source, a Spectral document needs to contain the properties shown in this example:
To be valid in Postman, your Spectral document can’t contain any properties beyond those listed here. For the full list of rules and their descriptions, see Spectral rule properties.
You’ll find each rule defined in rules in the Custom Rules section in the configurable API Governance page.
Remember to turn on your custom rules after you create or import them.
A JSON Path (or JSON Path Plus) expression aims to represent the path to some elements in a JSON or YAML document. For example, $.info.title represents the title property of the info object located at the document’s root ($).
Initially, JSON Path was created to be XPath for JSON and aimed to represent the path to some element in an XML document. JSON Path Plus is an extension of JSON Path. It adds more operators and makes some behaviors of the original specification explicit.
You can use the official JSON Path Plus documentation to build and test your rules’ given paths. Syntax Through Examples and the JSON Path Plus demo are both useful.
These examples show the typical JSON Path Plus features you’ll need to create rules in Postman:
$.info.title - The title property inside the info object, which is located at the document’s root ($).$.paths.*.*.responses - All responses of all operations of all paths. * gets all elements inside an object or an array.$.paths.*[post,patch,put] - All POST, PATCH, and PUT operations across all paths. [ ] can be used to filter elements.$..properties - All properties of all schema objects wherever they’re located (for example, in parameters, responses, or reusable components). .. gets all elements in the path tree.In the following example, the rule is supposed to check that there’s a description of the API. The way it’s written, though, it will never return a rule violation when the description isn’t present in the info object.
If the given path doesn’t find any value, the then checks won’t run. This means you can’t use the path $.info.description to check that the API description is defined in the OpenAPI document. This verification must be done using the path $.info, which will return the info object and the field value set with the name of the field you’re looking for (in this example, description).
You can add custom governance functions to your custom governance rules that Postman applies to your API specifications in Spec Hub. You can use these guidelines to write custom functions in JavaScript and add them to your custom governance rules. Postman supports ES6 syntax and CommonJS syntax for custom functions.
Postman recommends using ES6 syntax for your custom functions.
Your custom function must have the targetVal parameter, the message property in your return statement, and either the export default declaration (ES6) or module.exports object property (CommonJS) exporting your function.
To add a custom function to a rule, your rule must have the then.function property whose value is the name of the file containing the custom function. The filename is defined using the Name field when you create a custom function.
Use the following parameters in your custom functions depending on your use case. You must add parameters to your custom function in the following order: targetVal, options, then context.
You can use any parameter names you want. Postman expects the parameters to be in a specific order.
Use the following properties to write the return statement in your custom functions depending on your use case.
Required. Export your custom function. This enables you to add the custom function’s filename to your custom rule using the then.function property. The custom function name you export must match the name in the function declaration.
If your custom function accepts options, Postman recommends adding a JSON Schema to your custom function. A JSON Schema enables you to define and validate your custom function’s options when editing your rule. This requires you to export your custom function using the createRulesetFunction Spectral function.
export default function-name.module.exports = function-name.You can import the following Spectral functions into your custom function file in Postman.
The createRulesetFunction function is an optional Spectral function you can import from the @stoplight/spectral-core module.
Use a JSON Schema to define your custom function’s options, enabling you to validate whether the options provided in your rule using then.functionOptions match specific criteria. The JSON Schema must be nested inside of a JSON object. If the provided options don’t match the JSON Schema, an error message will explain the issue when editing your rule.
If you use ES6 syntax, error messages refer to your custom function as "<unknown>" function.
Add a JSON Schema for each of the following to your JSON object:
When you export your custom function, call the createRulesetFunction Spectral function and include a JSON object containing JSON Schemas and the custom function’s name as arguments. For a complete example of using a JSON Schema with a custom function, see Check that a value isn’t in a list (JSON Schema).
import { createRulesetFunction } from "@stoplight/spectral-core";.export default createRulesetFunction(json-object, function-name);.const { createRulesetFunction } = require("@stoplight/spectral-core");.module.exports = createRulesetFunction(json-object, function-name);.JSON Schema specification enables you to describe a JSON document using a standard format.
You can add a JSON Schema that defines and validates your custom function’s options. To learn about adding a JSON Schema to your custom function, see the createRulesetFunction Spectral function.
You can use the official JSON Schema documentation to learn more about describing a JSON document using this format.
The following examples show JSON Schema key-value pairs you can use to validate your custom function’s options:
$.options - The root object that contains the JSON Schema of the options parameter, which is your custom function’s options.$.options.type - A validation keyword that defines a constraint on the JSON data. In this example, the value is object, meaning the data must be a JSON object.$.options.properties - An object whose values define the parameter used to pass options to your custom function. In this example, values is a variable that stores the value of the options parameter.$.options.required - A validation keyword that’s an array of strings containing keys from $.options.properties. Add properties to the array if they must be defined in the JSON Schema. In this example, values must be defined in the JSON Schema.In the following example, the custom function named notInEnumeration is in a file named not_in_enumeration. The filename is defined using the Name field when you create a custom function.
If your custom function accepts options, Postman recommends adding a JSON Schema to your custom function. A JSON Schema enables you to define and validate your custom function’s options when editing your rule. This requires you to export your custom function using the createRulesetFunction Spectral function.
The custom function checks the value of the option values, which is defined in the Spectral document (or ruleset) using the then.functionOptions property. The value of values is a list of numeric strings. If targetVal is a value already in the list, the rule violation is triggered.
After the custom function, export default or module.exports references the custom function’s name. This exports the custom function, enabling you to add its filename to your rule using the then.function property.
In the following example, the custom function named notInEnumeration is in a file named not_in_enumeration. The filename is defined using the Name field when you create a custom function.
Before the custom function, the createRulesetFunction Spectral function is imported into the file. This enables you to define the expected options in a JSON Schema to validate whether the provided options in your rule match specific criteria.
The custom function checks the value of the option values, which is defined in the Spectral document (or ruleset) using the then.functionOptions property. The value of values is a list of numeric strings. If targetVal is a value already in the list, the rule violation is triggered.
After the custom function, export default or module.exports calls the createRulesetFunction Spectral function and includes the following arguments: a JSON object containing JSON Schemas of the targetVal parameter and options parameter, and the custom function’s name. This exports the custom function, enabling you to add its filename to your rule using the then.function property.
Learn more about the JSON Schemas used in this example.
In the following example, the Spectral document has a rule named http-status-obsolete that uses a custom function file named not_in_enumeration, which is defined using the Name field when you create a custom function. The custom function file has a custom function named notInEnumeration. The custom function filename is added to the rule using the then.function property.
The custom function accepts options using the then.functionOptions property, defining a property named values that’s a list of numeric strings. The value of then.functionOptions.values is passed to the custom function notInEnumeration. The custom function then checks whether a rule violation occurred at the given path appended with the value of the then.field property.