Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-117-description-ends-with-period', [
{
name: 'valid description',
document: {
components: {
schemas: {
Schema: {
properties: {
id: {
description: 'Description.',
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid descriptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noPeriod: {
description: 'Description',
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-description-ends-with-period',
message: 'Descriptions must end with a full stop(.).',
path: ['components', 'schemas', 'Schema', 'properties', 'noPeriod'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'ignores descriptions ending with table',
document: {
components: {
schemas: {
Schema: {
properties: {
noPeriod: {
description: 'Description\n| Table |',
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid components with exceptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noPeriod: {
description: 'Description',
'x-xgen-IPA-exception': {
'xgen-IPA-117-description-ends-with-period': 'reason',
},
},
},
},
},
},
},
errors: [],
},
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-117-description-starts-with-uppercase', [
{
name: 'valid description',
document: {
components: {
schemas: {
Schema: {
properties: {
id: {
description: 'Description',
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid descriptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noUpperCase: {
description: 'description',
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-description-starts-with-uppercase',
message: 'Descriptions must start with Uppercase.',
path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCase'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid components with exceptions',
document: {
components: {
schemas: {
Schema: {
properties: {
noUpperCase: {
description: 'description',
'x-xgen-IPA-exception': {
'xgen-IPA-117-description-starts-with-uppercase': 'reason',
},
},
},
},
},
},
},
errors: [],
},
]);
51 changes: 51 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,8 @@

functions:
- IPA117HasDescription
- IPA117DescriptionStartsWithUpperCase
- IPA117DescriptionEndsWithPeriod

rules:
xgen-IPA-117-description:
Expand Down Expand Up @@ -30,3 +32,52 @@ rules:
- '$.components.parameters[*]'
then:
function: 'IPA117HasDescription'
xgen-IPA-117-description-starts-with-uppercase:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q: Can't we merge them into same rule as you did before, and giving inline table marks as functionOptions

Copy link
Member

@wtrocki wtrocki Mar 21, 2025

Choose a reason for hiding this comment

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

+1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Was thinking about it initially though I didn't want to skip validating that the first letter is uppercase because it ends with |, so I split them into two to have clearer logic in the validator. Though we could add this as a function option for the description-ends-with-period, wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

Good context. Based on your explanation current state in PR SGTM

description: |
Descriptions must start with Uppercase.

##### 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-starts-with-uppercase'
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: 'IPA117DescriptionStartsWithUpperCase'
xgen-IPA-117-description-ends-with-period:
description: |
Descriptions must 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
The rule ignores descriptions that end with `|`, i.e. inline markdown tables
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-ends-with-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: 'IPA117DescriptionEndsWithPeriod'
29 changes: 29 additions & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,35 @@ 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-starts-with-uppercase

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

##### 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

#### xgen-IPA-117-description-ends-with-period

![warn](https://img.shields.io/badge/warning-yellow)
Descriptions must 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
The rule ignores descriptions that end with `|`, i.e. inline markdown tables



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

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

export default (input, opts, { path }) => {
// Ignore missing descriptions or descriptions that ends with an inline table
if (!input['description'] || input['description'].endsWith('|') || input['description'].endsWith('|\n')) {
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 periodEnd = new RegExp(`[.]$`);

try {
if (!periodEnd.test(description)) {
return [{ path, message: ERROR_MESSAGE_PERIOD }];
}
return [];
} catch (e) {
handleInternalError(RULE_NAME, path, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { hasException } from './utils/exceptions.js';
import {
collectAdoption,
collectAndReturnViolation,
collectException,
handleInternalError,
} from './utils/collectionUtils.js';

const RULE_NAME = 'xgen-IPA-117-description-starts-with-uppercase';
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]`);

try {
if (!upperCaseStart.test(description)) {
return [{ path, message: ERROR_MESSAGE_UPPER_CASE }];
}
return [];
} catch (e) {
handleInternalError(RULE_NAME, path, e);
}
}
Loading