Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference property to context.report() for external documentation #1904

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/custom-plugins/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ The main method used is `context.report()`, which publishes a warning or error (
- `location` - {Location} (optional) An object specifying the location of the problem. Can be constructed using location object methods.
- `suggest` - {string[]} (optional) - "Did you mean" suggestion.
- `from` - {Location} (optional) - Referenced by location.
- `reference` - {string} (optional) - A URI reference to external documentation supporting the design decision of a particular rule.

You may use the message alone:

Expand Down
105 changes: 105 additions & 0 deletions packages/core/src/rules/arazzo/__tests__/reference-property.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { outdent } from 'outdent';
import { lintDocument } from '../../../lint';
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
import { BaseResolver } from '../../../resolve';

describe('Arazzo reference-property', () => {
const document = parseYamlToDocument(
outdent`
arazzo: '1.0.1'
info:
title: Cool API
version: 1.0.0
description: A cool API
sourceDescriptions:
- name: museum-api
type: openapi
url: openapi.yaml
workflows:
- workflowId: get-museum-hours
description: This workflow demonstrates how to get the museum opening hours and buy tickets.
parameters:
- in: header
name: Authorization
value: Basic Og==
steps:
- stepId: get-museum-hours
description: >-
Get museum hours by resolving request details with getMuseumHours operationId from openapi.yaml description.
operationId: museum-api.getMuseumHours
successCriteria:
- condition: $statusCode == 200
`,
'arazzo.yaml'
);

it('should report an error when the `reference` property is invalid', async () => {
const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({
rules: { 'reference-property': 'error' },
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
[
{
"location": [
{
"pointer": "#/workflows/0/steps/0/successCriteria/0",
"reportOnKey": false,
"source": "arazzo.yaml",
},
],
"message": "The \`reference\` property must be a valid URI.",
"ruleId": "reference-property",
"severity": "error",
"suggest": [],
},
]
`);
});

it('should not report an error when the `reference` property is valid', async () => {
const validDocument = parseYamlToDocument(
outdent`
arazzo: '1.0.1'
info:
title: Cool API
version: 1.0.0
description: A cool API
sourceDescriptions:
- name: museum-api
type: openapi
url: openapi.yaml
workflows:
- workflowId: get-museum-hours
description: This workflow demonstrates how to get the museum opening hours and buy tickets.
parameters:
- in: header
name: Authorization
value: Basic Og==
steps:
- stepId: get-museum-hours
description: >-
Get museum hours by resolving request details with getMuseumHours operationId from openapi.yaml description.
operationId: museum-api.getMuseumHours
successCriteria:
- condition: $statusCode == 200
reference: https://www.redocly.com
`,
'arazzo.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document: validDocument,
config: await makeConfig({
rules: { 'reference-property': 'error' },
}),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/rules/arazzo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { RequestBodyReplacementsUnique } from './requestBody-replacements-unique';
import { NoCriteriaXpath } from '../spot/no-criteria-xpath';
import { CriteriaUnique } from './criteria-unique';
import { ReferenceProperty } from './reference-property';

import type { Arazzo1Rule } from '../../visitors';
import type { Arazzo1RuleSet } from '../../oas-types';
Expand All @@ -33,6 +34,7 @@
'requestBody-replacements-unique': RequestBodyReplacementsUnique,
'no-criteria-xpath': NoCriteriaXpath,
'criteria-unique': CriteriaUnique,
'reference-property': ReferenceProperty,

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / latest-vs-next

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / prepare-smoke-plugins

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / build-and-unit

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / prepare-smoke

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / require-changeset-or-label

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / cli-package-test

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / code-style-check

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.

Check failure on line 37 in packages/core/src/rules/arazzo/index.ts

View workflow job for this annotation

GitHub Actions / coverage-report

Object literal may only specify known properties, and ''reference-property'' does not exist in type 'Arazzo1RuleSet<"built-in">'.
};

export const preprocessors = {};
22 changes: 22 additions & 0 deletions packages/core/src/rules/arazzo/reference-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Arazzo1Rule } from '../../visitors';
import type { UserContext } from '../../walk';

export const ReferenceProperty: Arazzo1Rule = () => {
return {
Report: {
enter(report, { report: ctxReport, location }: UserContext) {
const reference = report?.reference;
if (reference !== undefined) {
try {
new URL(reference);
} catch (_) {
ctxReport({
message: 'The `reference` property must be a valid URI.',
location: location.child(['reference']),
});
}
}
},
},
};
};
Loading