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: make example-value-or-externalValue only target oas3 document #1059

Merged
merged 5 commits into from
Apr 6, 2020
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
6 changes: 3 additions & 3 deletions docs/reference/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ unused-definition:

## xor

Communicate that only one of these properties is allowed, and no more than one of them.
Communicate that one of these properties is required, and no more than one is allowed to be defined.

<!-- title: functionOptions -->

Expand All @@ -268,10 +268,10 @@ properties | the properties to check | yes
<!-- title: example -->

```yaml
example-value-or-externalValue:
components-example-value-or-externalValue:
description: Example should have either a `value` or `externalValue` field.
type: style
given: "$..example"
given: "$.components.examples.*"
then:
function: xor
functionOptions:
Expand Down
48 changes: 24 additions & 24 deletions docs/reference/openapi-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,6 @@ info:
url: goarmy.com/apis/support
```

### example-value-or-externalValue

Examples for `requestBody` or response examples can have an `externalValue` or a `value`, but they cannot have both.

**Recommended:** Yes

**Bad Example**

```yaml
paths:
/pet:
put:
operationId: "relace-pet"
requestBody:
content:
'application/json':
examples:
foo:
summary: A foo example
value: {"foo": "bar"}
externalValue: 'http://example.org/foo.json'
# marp! no, can only have one or the other
```

### info-contact

Info object should contain `contact` object.
Expand Down Expand Up @@ -523,6 +499,30 @@ servers:

If this is going out to the world, maybe have production and a general sandbox people can play with.

### oas3-example-value-or-externalValue

Examples for `requestBody` or response examples can have an `externalValue` or a `value`, but they cannot have both.

**Recommended:** Yes

**Bad Example**

```yaml
paths:
/pet:
put:
operationId: "replace-pet"
requestBody:
content:
'application/json':
examples:
foo:
summary: A foo example
value: {"foo": "bar"}
externalValue: 'http://example.org/foo.json'
# marp! no, can only have one or the other
```

### oas3-operation-security-defined

Operation `security` values must match a scheme defined in the `components.securitySchemes` object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

describe('example-value-or-externalValue', () => {
describe('oas3-example-value-or-externalValue', () => {
const s = new Spectral();
s.registerFormat('oas3', () => true);
s.setRules({
'example-value-or-externalValue': Object.assign(ruleset.rules['example-value-or-externalValue'], {
'oas3-example-value-or-externalValue': Object.assign(ruleset.rules['oas3-example-value-or-externalValue'], {
recommended: true,
type: RuleType[ruleset.rules['example-value-or-externalValue'].type],
type: RuleType[ruleset.rules['oas3-example-value-or-externalValue'].type],
}),
});

Expand Down Expand Up @@ -77,7 +78,7 @@ describe('example-value-or-externalValue', () => {
const results = await s.run({ components: { examples: { first: {} } } });
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'examples', 'first'],
range: {
Expand All @@ -101,7 +102,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'examples', 'third'],
range: {
Expand All @@ -125,7 +126,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'examples', 'first'],
range: {
Expand Down Expand Up @@ -155,7 +156,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'examples', 'second'],
range: {
Expand Down Expand Up @@ -189,7 +190,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'headers', 'headerName', 'examples', 'second'],
range: {
Expand Down Expand Up @@ -223,7 +224,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['components', 'parameters', 'parameterName', 'examples', 'second'],
range: {
Expand Down Expand Up @@ -265,7 +266,7 @@ describe('example-value-or-externalValue', () => {
});
expect(results).toEqual([
{
code: 'example-value-or-externalValue',
code: 'oas3-example-value-or-externalValue',
message: 'Example should have either a `value` or `externalValue` field.',
path: ['paths', '/path', 'get', 'responses', '200', 'content', 'application/json', 'examples', 'second'],
range: {
Expand Down
47 changes: 24 additions & 23 deletions src/rulesets/oas/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,6 @@
"api"
]
},
"example-value-or-externalValue": {
"description": "Example should have either a `value` or `externalValue` field.",
"recommended": true,
"type": "style",
"given": [
"$.components.examples.*",
"$.paths..content.*.examples.*",
"$.components..content.*.examples.*",
"$.paths..parameters.*.examples.*",
"$.components..parameters.*.examples.*",
"$.paths..headers.*.examples.*",
"$.components..headers.*.examples.*"
],
"then": {
"function": "xor",
"functionOptions": {
"properties": [
"externalValue",
"value"
]
}
}
},
"info-contact": {
"description": "Info object should contain `contact` object.",
"recommended": true,
Expand Down Expand Up @@ -657,6 +634,30 @@
"api"
]
},
"oas3-example-value-or-externalValue": {
"description": "Example should have either a `value` or `externalValue` field.",
"recommended": true,
"formats": ["oas3"],
"type": "style",
"given": [
"$.components.examples.*",
"$.paths..content.*.examples.*",
"$.components..content.*.examples.*",
"$.paths..parameters.*.examples.*",
"$.components..parameters.*.examples.*",
"$.paths..headers.*.examples.*",
"$.components..headers.*.examples.*"
],
"then": {
"function": "xor",
"functionOptions": {
"properties": [
"externalValue",
"value"
]
}
}
},
"oas3-operation-security-defined": {
"description": "Operation `security` values must match a scheme defined in the `components.securitySchemes` object.",
"recommended": true,
Expand Down