Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 102 additions & 0 deletions tools/spectral/ipa/__tests__/IPA117DescriptionUpperCasePeriod.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-117-description-uppercase-period', [
{
name: 'valid description',
document: {
components: {
schemas: {
Schema: {
properties: {
id: {
description: 'Description.',
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid descriptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noPeriod: {
description: 'Description',
},
noUpperCase: {
description: 'description.',
},
noUpperCaseNoPeriod: {
description: 'description',
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-description-uppercase-period',
message: 'Descriptions must end with a full stop(.).',
path: ['components', 'schemas', 'Schema', 'properties', 'noPeriod'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-description-uppercase-period',
message: 'Descriptions must start with Uppercase.',
path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCase'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-description-uppercase-period',
message: 'Descriptions must start with Uppercase.',
path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCaseNoPeriod'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-description-uppercase-period',
message: 'Descriptions must end with a full stop(.).',
path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCaseNoPeriod'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid components with exceptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noPeriod: {
description: 'Description',
'x-xgen-IPA-exception': {
'xgen-IPA-117-description-uppercase-period': 'reason',
},
},
noUpperCase: {
description: 'description.',
'x-xgen-IPA-exception': {
'xgen-IPA-117-description-uppercase-period': 'reason',
},
},
noUpperCaseNoPeriod: {
description: 'description',
'x-xgen-IPA-exception': {
'xgen-IPA-117-description-uppercase-period': 'reason',
},
},
},
},
},
},
},
errors: [],
},
]);
25 changes: 25 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-117.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

functions:
- IPA117HasDescription
- IPA117DescriptionUpperCasePeriod

rules:
xgen-IPA-117-description:
Expand Down Expand Up @@ -30,3 +31,27 @@ rules:
- '$.components.parameters[*]'
then:
function: 'IPA117HasDescription'
xgen-IPA-117-description-uppercase-period:
description: |
Descriptions must start with Uppercase and end with a full stop(.)

##### Implementation details
Rule checks the format of the description property in the following components:
- Info object
- Tags
- Operation objects
- Inline schema properties for operation object requests and responses
- Parameter objects (in operations and components)
- Schema properties
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-uppercase-period'
severity: warn
given:
- '$.info'
- '$.tags[*]'
- '$.paths[*][get,put,post,delete,options,head,patch,trace]'
- '$.paths[*][get,put,post,delete,options,head,patch,trace].parameters[*]'
- '$.paths[*][get,put,post,delete,options,head,patch,trace]..content..properties[*]'
- '$.components.schemas..properties[*]'
- '$.components.parameters[*]'
then:
function: 'IPA117DescriptionUpperCasePeriod'
14 changes: 14 additions & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,20 @@ Rule checks for description property in the following components:
- Schema properties
The rule also fails if the description is an empty string.

#### xgen-IPA-117-description-uppercase-period

![warn](https://img.shields.io/badge/warning-yellow)
Descriptions must start with Uppercase and end with a full stop(.)

##### Implementation details
Rule checks the format of the description property in the following components:
- Info object
- Tags
- Operation objects
- Inline schema properties for operation object requests and responses
- Parameter objects (in operations and components)
- Schema properties



### IPA-123
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { hasException } from './utils/exceptions.js';
import {
collectAdoption,
collectAndReturnViolation,
collectException,
handleInternalError,
} from './utils/collectionUtils.js';

const RULE_NAME = 'xgen-IPA-117-description-uppercase-period';
const ERROR_MESSAGE_PERIOD = 'Descriptions must end with a full stop(.).';
const ERROR_MESSAGE_UPPER_CASE = 'Descriptions must start with Uppercase.';

export default (input, opts, { path }) => {
if (!input['description']) {
return;
}

if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

const errors = checkViolationsAndReturnErrors(input['description'], path);
if (errors.length !== 0) {
return collectAndReturnViolation(path, RULE_NAME, errors);
}
collectAdoption(path, RULE_NAME);
};

function checkViolationsAndReturnErrors(description, path) {
const upperCaseStart = new RegExp(`^[A-Z]`);
const periodEnd = new RegExp(`[.]$`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to consider the old case https://github.com/mongodb/openapi/blob/3915e0a1a921c00e2ccdb30a37e8b0b8a2120f5f/tools/spectral/.spectral.yaml#L56C10-L56C27 a lot of descriptions end with a markdown table |

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for pointing it out. We have another guideline to not allow inline tables, so perhaps in this case if it ends with a table we should ignore rather than say it's adopted

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another IPA guideline stating (and there will be another validation rule for) Descriptions should not include inline tables, as this may not work well with all tools, particularly generated client code. Should we consider descriptions ending with Markdown tables valid?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's what I was thinking, so for this rule instead of saying . and | is valid, we can ignore cases where it ends with | and not collect adoption

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw your comment after updating the page, sorry :D I am okay with ignoring them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP! Will update 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split them into two separate rules, and for the full stop test ignoring inline table endings

const errors = [];

try {
if (!upperCaseStart.test(description)) {
errors.push({ path, message: ERROR_MESSAGE_UPPER_CASE });
}
if (!periodEnd.test(description)) {
errors.push({ path, message: ERROR_MESSAGE_PERIOD });
}
return errors;
} catch (e) {
handleInternalError(RULE_NAME, path, e);
}
}
Loading