Skip to content

Commit 47d3841

Browse files
committed
feat: add helpers to extract processed api from spectral context
The Spectral 'context' object contains processed data about the OpenAPI definition being validated, including the resolved and unresolved versions of the definition and information about references. We use these objects in rule functions and they need to be extracted from deeply nested fields in the spectral context. Rather than always needing to remember or look up the fields, this commit allows us to use simple helper functions to get the data we need. Signed-off-by: Dustin Popp <[email protected]>
1 parent c060ca9 commit 47d3841

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

docs/openapi-ruleset-utilities.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,39 @@ for OpenAPI documents where a `required` property is not defined under the `prop
354354

355355
#### Returns `boolean`
356356

357+
### `getResolvedSpec(context)`
358+
359+
Returns the programmatic representation of an OpenAPI document, stored in the
360+
Spectral-created "context" object, with all non-circular references resolved.
361+
362+
#### Parameters
363+
364+
- **`context`** `<object>`: passed as an argument to Spectral-based rule functions
365+
366+
#### Returns `object`: the resolved version of an OpenAPI document
367+
368+
### `getUnresolvedSpec(context)`
369+
370+
Returns the programmatic representation of an OpenAPI document, stored in
371+
the Spectral-created "context" object, with all references still intact.
372+
373+
#### Parameters
374+
375+
- **`context`** `<object>`: passed as an argument to Spectral-based rule functions
376+
377+
#### Returns `object`: the unresolved version of an OpenAPI document
378+
379+
### `getNodes(context)`
380+
381+
Returns the graph nodes, with information about references and the locations
382+
they resolve to, that are computed by the Spectral resolver.
383+
384+
#### Parameters
385+
386+
- **`context`** `<object>`: passed as an argument to Spectral-based rule functions
387+
388+
#### Returns `object`: the graph nodes
389+
357390
### `validateComposedSchemas(schema, path, validate, includeSelf, includeNot)`
358391

359392
Performs validation on a schema and all of its composed schemas.

packages/utilities/src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 - 2024 IBM Corporation.
2+
* Copyright 2017 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

@@ -13,6 +13,7 @@ module.exports = {
1313
schemaHasProperty: require('./schema-has-property'),
1414
schemaLooselyHasConstraint: require('./schema-loosely-has-constraint'),
1515
schemaRequiresProperty: require('./schema-requires-property'),
16+
...require('./spectral-context-utils'),
1617
validateComposedSchemas: require('./validate-composed-schemas'),
1718
validateNestedSchemas: require('./validate-nested-schemas'),
1819
validateSubschemas: require('./validate-subschemas'),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2025 IBM Corporation.
3+
* SPDX-License-Identifier: Apache2.0
4+
*/
5+
6+
/**
7+
* Returns the programmatic representation of an OpenAPI document, stored in the
8+
* Spectral-created "context" object, with all non-circular references resolved.
9+
* @param {object} context passed as an argument to Spectral-based rule functions
10+
* @returns {object} the resolved version of an OpenAPI document
11+
*/
12+
function getResolvedSpec(context) {
13+
return context.documentInventory.resolved;
14+
}
15+
16+
/**
17+
* Returns the programmatic representation of an OpenAPI document, stored in
18+
* the Spectral-created "context" object, with all references still intact.
19+
* @param {object} context passed as an argument to Spectral-based rule functions
20+
* @returns {object} the unresolved version of an OpenAPI document
21+
*/
22+
function getUnresolvedSpec(context) {
23+
return context.document.parserResult.data;
24+
}
25+
26+
/**
27+
* Returns the graph nodes, with information about references and the locations
28+
* they resolve to, that are computed by the Spectral resolver.
29+
* @param {object} context passed as an argument to Spectral-based rule functions
30+
* @returns {object} the graph nodes
31+
*/
32+
function getNodes(context) {
33+
return context.documentInventory.graph.nodes;
34+
}
35+
36+
module.exports = {
37+
getNodes,
38+
getResolvedSpec,
39+
getUnresolvedSpec,
40+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2025 IBM Corporation.
3+
* SPDX-License-Identifier: Apache2.0
4+
*/
5+
6+
const { getNodes, getResolvedSpec, getUnresolvedSpec } = require('../src');
7+
8+
describe('Utility functions: Spectral Context Utils', () => {
9+
const mockSpectralContextObject = {
10+
documentInventory: {
11+
resolved: 'resolved spec',
12+
graph: {
13+
nodes: 'graph nodes',
14+
},
15+
},
16+
document: {
17+
parserResult: {
18+
data: 'unresolved spec',
19+
},
20+
},
21+
};
22+
23+
describe('getNodes', () => {
24+
it('should return graph nodes from context object', async () => {
25+
expect(getNodes(mockSpectralContextObject)).toBe('graph nodes');
26+
});
27+
});
28+
29+
describe('getResolvedSpec', () => {
30+
it('should return graph nodes from context object', async () => {
31+
expect(getResolvedSpec(mockSpectralContextObject)).toBe('resolved spec');
32+
});
33+
});
34+
35+
describe('getUnresolvedSpec', () => {
36+
it('should return graph nodes from context object', async () => {
37+
expect(getUnresolvedSpec(mockSpectralContextObject)).toBe(
38+
'unresolved spec'
39+
);
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)