Skip to content

Commit

Permalink
refactor(rules): further separate oas3 and oas2 rules
Browse files Browse the repository at this point in the history
  • Loading branch information
casserni authored and Marc MacLeod committed Dec 11, 2018
1 parent ea1d029 commit 6691a70
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 77 deletions.
65 changes: 0 additions & 65 deletions src/rulesets/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,36 +68,6 @@ export const commonOasRules = (): RuleCollection => ({
},

// Generic Rules

'api-host': {
summary: 'OpenAPI `host` must be present and non-empty string.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'host',
function: RuleFunction.TRUTHY,
},
tags: ['api'],
},
'api-schemes': {
summary: 'OpenAPI host `schemes` must be present and non-empty array.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'schemes',
function: RuleFunction.SCHEMA,
functionOptions: {
schema: {
items: {
type: 'string',
},
minItems: 1,
type: 'array',
},
},
},
tags: ['api'],
},
'contact-properties': {
enabled: false,
summary: 'Contact object should have `name`, `url` and `email`.',
Expand Down Expand Up @@ -173,16 +143,6 @@ export const commonOasRules = (): RuleCollection => ({
},
tags: ['api'],
},
'model-description': {
enabled: false,
summary: 'Definition `description` must be present and non-empty string.',
type: RuleType.STYLE,
given: '$..definitions[*]',
then: {
field: 'description',
function: RuleFunction.TRUTHY,
},
},
'no-eval-in-markdown': {
enabled: false,
summary: 'Markdown descriptions should not contain `eval(`.',
Expand Down Expand Up @@ -399,31 +359,6 @@ export const commonOasRules = (): RuleCollection => ({
},
},
},
'server-not-example.com': {
enabled: false,
summary: 'Server URL should not point at `example.com`.',
type: RuleType.STYLE,
given: '$.servers[*]',
then: {
field: 'url',
function: RuleFunction.PATTERN,
functionOptions: {
notMatch: 'example.com',
},
},
},
'server-trailing-slash': {
summary: 'Server URL should not have a trailing slash.',
type: RuleType.STYLE,
given: '$.servers[*]',
then: {
field: 'url',
function: RuleFunction.PATTERN,
functionOptions: {
notMatch: '/$',
},
},
},
'tag-description': {
enabled: false,
summary: 'Tag object should have a `description`.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Spectral } from '../../../spectral';
import { commonOasRules } from '../index';
import { oas2Rules } from '../index';

const ruleset = { rules: commonOasRules() };
const ruleset = { rules: oas2Rules() };

describe('api-host', () => {
const s = new Spectral();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Spectral } from '../../../spectral';
import { commonOasRules } from '../index';
import { oas2Rules } from '../index';

const ruleset = { rules: commonOasRules() };
const ruleset = { rules: oas2Rules() };

describe('api-schemes', () => {
const s = new Spectral();
Expand Down
31 changes: 31 additions & 0 deletions src/rulesets/oas2/__tests__/host-not-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Spectral } from '../../../spectral';
import { oas2Rules } from '../index';

const ruleset = { rules: oas2Rules() };

describe('host-not-example', () => {
const s = new Spectral();
s.addRules({
'host-not-example': Object.assign(ruleset.rules['host-not-example'], {
enabled: true,
}),
});

test('validate a correct object', () => {
const results = s.run({
swagger: '2.0',
paths: {},
host: 'stoplight.io',
});
expect(results.results.length).toEqual(0);
});

test('return errors if server is example.com', () => {
const results = s.run({
swagger: '2.0',
paths: {},
host: 'https://example.com',
});
expect(results.results.length).toEqual(1);
});
});
31 changes: 31 additions & 0 deletions src/rulesets/oas2/__tests__/host-trailing-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Spectral } from '../../../spectral';
import { oas2Rules } from '../index';

const ruleset = { rules: oas2Rules() };

describe('host-trailing-slash', () => {
const s = new Spectral();
s.addRules({
'host-trailing-slash': Object.assign(ruleset.rules['host-trailing-slash'], {
enabled: true,
}),
});

test('validate a correct object', () => {
const results = s.run({
swagger: '2.0',
paths: {},
host: 'stoplight.io',
});
expect(results.results.length).toEqual(0);
});

test('return errors if host url ends with a slash', () => {
const results = s.run({
swagger: '2.0',
paths: {},
host: 'stoplight.io/',
});
expect(results.results.length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Spectral } from '../../../spectral';
import { commonOasRules } from '../index';
import { oas2Rules } from '../index';

const ruleset = { rules: commonOasRules() };
const ruleset = { rules: oas2Rules() };

describe('model-description', () => {
const s = new Spectral();
Expand All @@ -12,12 +12,26 @@ describe('model-description', () => {
});

test('validate a correct object', () => {
const results = s.run({ definitions: { user: { description: 'this describes the user model' } } });
const results = s.run({
swagger: '2.0',
paths: {},
host: 'stoplight.io',
definitions: {
user: {
description: 'this describes the user model',
},
},
});
expect(results.results.length).toEqual(0);
});

test('return errors if a definition is missing description', () => {
const results = s.run({ definitions: { user: {} } });
const results = s.run({
swagger: '2.0',
paths: {},
host: 'stoplight.io',
definitions: { user: {} },
});
expect(results.results.length).toEqual(1);
});
});
67 changes: 67 additions & 0 deletions src/rulesets/oas2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { commonOasFunctions as oas2Functions } from '../oas';

export const oas2Rules = () => {
return merge(commonOasRules(), {
// specification validation
'oas2-schema': {
summary: 'Validate structure of OpenAPIv2 specification.',
type: RuleType.VALIDATION,
Expand All @@ -21,5 +22,71 @@ export const oas2Rules = () => {
},
tags: ['schema'],
},

// generic
'api-host': {
summary: 'OpenAPI `host` must be present and non-empty string.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'host',
function: RuleFunction.TRUTHY,
},
tags: ['api'],
},
'api-schemes': {
summary: 'OpenAPI host `schemes` must be present and non-empty array.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'schemes',
function: RuleFunction.SCHEMA,
functionOptions: {
schema: {
items: {
type: 'string',
},
minItems: 1,
type: 'array',
},
},
},
tags: ['api'],
},
'host-not-example': {
enabled: false,
summary: 'Server URL should not point at `example.com`.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'host',
function: RuleFunction.PATTERN,
functionOptions: {
notMatch: 'example.com',
},
},
},
'host-trailing-slash': {
summary: 'Server URL should not have a trailing slash.',
type: RuleType.STYLE,
given: '$',
then: {
field: 'host',
function: RuleFunction.PATTERN,
functionOptions: {
notMatch: '/$',
},
},
},
'model-description': {
enabled: false,
summary: 'Definition `description` must be present and non-empty string.',
type: RuleType.STYLE,
given: '$..definitions[*]',
then: {
field: 'description',
function: RuleFunction.TRUTHY,
},
},
});
};
39 changes: 39 additions & 0 deletions src/rulesets/oas3/__tests__/api-servers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Spectral } from '../../../spectral';
import { oas3Rules } from '../index';

const ruleset = { rules: oas3Rules() };

describe('api-servers', () => {
const s = new Spectral();
s.addRules({
'api-servers': Object.assign(ruleset.rules['api-servers'], {
enabled: true,
}),
});

test('validate a correct object', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
servers: [{ url: 'https://stoplight.io' }],
});
expect(results.results.length).toEqual(0);
});

test('return errors if servers is missing ', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
});
expect(results.results.length).toEqual(1);
});

test('return errors if servers is an empty array ', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
servers: [],
});
expect(results.results.length).toEqual(1);
});
});
41 changes: 41 additions & 0 deletions src/rulesets/oas3/__tests__/model-description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Spectral } from '../../../spectral';
import { oas3Rules } from '../index';

const ruleset = { rules: oas3Rules() };

describe('model-description', () => {
const s = new Spectral();
s.addRules({
'model-description': Object.assign(ruleset.rules['model-description'], {
enabled: true,
}),
});

test('validate a correct object', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
components: {
schemas: {
user: {
description: 'this describes the user model',
},
},
},
});
expect(results.results.length).toEqual(0);
});

test('return errors if a definition is missing description', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
components: {
schemas: {
user: {},
},
},
});
expect(results.results.length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Spectral } from '../../../spectral';
import { commonOasRules } from '../index';
import { oas3Rules } from '../index';

const ruleset = { rules: commonOasRules() };
const ruleset = { rules: oas3Rules() };

describe('server-not-example.com', () => {
const s = new Spectral();
Expand All @@ -13,6 +13,8 @@ describe('server-not-example.com', () => {

test('validate a correct object', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
servers: [
{
url: 'https://stoplight.io',
Expand All @@ -24,6 +26,8 @@ describe('server-not-example.com', () => {

test('return errors if server is example.com', () => {
const results = s.run({
openapi: '3.0.0',
paths: {},
servers: [
{
url: 'https://example.com',
Expand Down
Loading

0 comments on commit 6691a70

Please sign in to comment.