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

fix(oas): validate falsy examples #1418

Merged
merged 2 commits into from
Dec 23, 2020
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
23 changes: 23 additions & 0 deletions src/rulesets/oas/__tests__/oas2-valid-schema-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ describe('oas2-valid-schema-example', () => {
]);
});

describe.each(['', null, 0, false])('given falsy %s value', value => {
test.each(['example', 'x-example'])('will validate empty %s value', async field => {
const results = await s.run({
[parentField]: [
{
xoxo: {
enum: ['a', 'b'],
[field]: value,
},
},
],
});

expect(results).toEqual([
expect.objectContaining({
code: 'oas2-valid-schema-example',
message: `\`${field}\` property should be equal to one of the allowed values: \`a\`, \`b\``,
severity: DiagnosticSeverity.Error,
}),
]);
});
});

test('will pass when complex example is used ', async () => {
const results = await s.run({
[parentField]: [
Expand Down
30 changes: 28 additions & 2 deletions src/rulesets/oas/__tests__/oas3-valid-media-example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { setFunctionContext } from '../../evaluators';
import { functions } from '../../../functions';
import oasExample from '../functions/oasExample';

const Decimal = require('decimal.js');

describe('oas3-valid-media-example', () => {
let s: Spectral;

Expand Down Expand Up @@ -57,6 +59,30 @@ describe('oas3-valid-media-example', () => {
]);
});

describe.each(['', null, 0, false])('given falsy %s value', value => {
test('will validate empty value', async () => {
const results = await s.run({
openapi: '3.0.2',
[path]: {
xoxo: {
schema: {
enum: ['a', 'b'],
},
example: value,
},
},
});

expect(results).toEqual([
expect.objectContaining({
code: 'oas3-valid-media-example',
message: '`example` property should be equal to one of the allowed values: `a`, `b`',
severity: DiagnosticSeverity.Error,
}),
]);
});
});

test('will pass when complex example is used ', async () => {
const results = await s.run({
openapi: '3.0.0',
Expand Down Expand Up @@ -247,8 +273,8 @@ describe('oas3-valid-media-example', () => {
['byte', 'MTI3'],
['int32', 2 ** 30],
['int64', 2 ** 40],
['float', 2 ** 64],
['double', 2 ** 1028],
['float', new Decimal(2).pow(128)],
['double', new Decimal(2).pow(1024)],
])('does not report valid usage of %s format', async (format, example) => {
const results = await s.run({
openapi: '3.0.0',
Expand Down
30 changes: 28 additions & 2 deletions src/rulesets/oas/__tests__/oas3-valid-schema-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { setFunctionContext } from '../../evaluators';
import { functions } from '../../../functions';
import oasExample from '../functions/oasExample';

const Decimal = require('decimal.js');

describe('oas3-valid-schema-example', () => {
let s: Spectral;

Expand Down Expand Up @@ -126,6 +128,30 @@ describe('oas3-valid-schema-example', () => {
]);
});

describe.each(['', null, 0, false])('given falsy %s value', value => {
test('will validate empty value', async () => {
const results = await s.run({
openapi: '3.0.2',
[field]: {
schemas: {
xoxo: {
enum: ['a', 'b'],
example: value,
},
},
},
});

expect(results).toEqual([
expect.objectContaining({
code: 'oas3-valid-schema-example',
message: '`example` property should be equal to one of the allowed values: `a`, `b`',
severity: DiagnosticSeverity.Error,
}),
]);
});
});

test('will pass when complex example is used ', async () => {
const results = await s.run({
openapi: '3.0.2',
Expand Down Expand Up @@ -302,8 +328,8 @@ describe('oas3-valid-schema-example', () => {
['byte', 'MTI3'],
['int32', 2 ** 30],
['int64', 2 ** 40],
['float', 2 ** 64],
['double', 2 ** 1028],
['float', new Decimal(2).pow(128)],
['double', new Decimal(2).pow(1024)],
])('does not report valid usage of %s format', async (format, example) => {
const results = await s.run({
openapi: '3.0.2',
Expand Down
20 changes: 10 additions & 10 deletions src/rulesets/oas/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@
"severity": 0,
"type": "validation",
"given": [
"$..definitions..[?(@property !== 'properties' && (@.example || @['x-example']) && (@.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && (@.example || @['x-example']) && (@.type || @.format || @.$ref || @.properties || @.items))]",
"$..responses..[?(@property !== 'properties' && (@.example || @['x-example']) && (@.type || @.format || @.$ref || @.properties || @.items))]"
"$..definitions..[?(@property !== 'properties' && (@.example !== void 0 || @['x-example'] !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && (@.example !== void 0 || @['x-example'] !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..responses..[?(@property !== 'properties' && (@.example !== void 0 || @['x-example'] !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]"
],
"then": {
"function": "oasExample",
Expand Down Expand Up @@ -581,9 +581,9 @@
"formats": ["oas3"],
"type": "validation",
"given": [
"$..content..[?(@.schema && (@.example || @.examples))]",
"$..headers..[?(@.schema && (@.example || @.examples))]",
"$..parameters..[?(@.schema && (@.example || @.examples))]"
"$..content..[?(@.schema && (@.example !== void 0 || @.examples))]",
"$..headers..[?(@.schema && (@.example !== void 0 || @.examples))]",
"$..parameters..[?(@.schema && (@.example !== void 0 || @.examples))]"
],
"then": {
"function": "oasExample",
Expand All @@ -602,10 +602,10 @@
"recommended": true,
"type": "validation",
"given": [
"$.components.schemas..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref || @.properties || @.items))]",
"$..content..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref || @.properties || @.items))]",
"$..headers..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref || @.properties || @.items))]"
"$.components.schemas..[?(@property !== 'properties' && @.example !== void 0 && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..content..[?(@property !== 'properties' && @.example !== void 0 && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..headers..[?(@property !== 'properties' && @.example !== void 0 && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && @.example !== void 0 && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]"
],
"then": {
"function": "oasExample",
Expand Down