Skip to content

Commit

Permalink
feat(cli): add JSON path to stylish formatter (#1382)
Browse files Browse the repository at this point in the history
* feat: Add JSON path to stylish formatter

* Format with printPath

* Update test harness scenarios

Co-authored-by: Jakub Rożek <jakub@stoplight.io>
  • Loading branch information
aburgel and P0lip authored Nov 20, 2020
1 parent 797b19e commit 83b1d61
Show file tree
Hide file tree
Showing 31 changed files with 130 additions and 124 deletions.
20 changes: 12 additions & 8 deletions src/cli/formatters/__tests__/stylish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ const mixedErrors = sortResults(require('./__fixtures__/mixed-errors.json'));
describe('Stylish formatter', () => {
test('should prefer message for oas-schema errors', () => {
const result = stylish(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result).toContain('oas3-schema should NOT have additional properties: type\n');
expect(result).toContain('oas3-schema should NOT have additional properties: type');
expect(result).toContain('oas3-schema should match exactly one schema in oneOf');
expect(result).toContain("oas3-schema should have required property '$ref'\n");
expect(result).toContain("oas3-schema should have required property '$ref'");
});

test('should display proper severity level', () => {
const result = stylish(mixedErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result).toContain(`
3:10 ${chalk.white('hint')} info-contact Info object should contain \`contact\` object.
3:10 ${chalk.white(
'hint',
)} info-contact Info object should contain \`contact\` object. info
3:10 ${chalk.yellow(
'warning',
)} info-description OpenAPI object info \`description\` must be present and non-empty string.
5:14 ${chalk.red('error')} info-matches-stoplight Info must contain Stoplight
)} info-description OpenAPI object info \`description\` must be present and non-empty string. info
5:14 ${chalk.red(
'error',
)} info-matches-stoplight Info must contain Stoplight info.title
17:13 ${chalk.blue(
'information',
)} operation-description Operation \`description\` must be present and non-empty string.
)} operation-description Operation \`description\` must be present and non-empty string. paths./pets.get
64:14 ${chalk.blue(
'information',
)} operation-description Operation \`description\` must be present and non-empty string.
)} operation-description Operation \`description\` must be present and non-empty string. paths./pets.post
86:13 ${chalk.blue(
'information',
)} operation-description Operation \`description\` must be present and non-empty string.`);
)} operation-description Operation \`description\` must be present and non-empty string. paths./pets/{petId}.get`);
});
});
2 changes: 2 additions & 0 deletions src/cli/formatters/stylish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import * as table from 'text-table';
import { IRuleResult } from '../../types';
import { Formatter } from './types';
import { getColorForSeverity, getHighestSeverity, getSeverityName, getSummary, groupBySource } from './utils';
import { printPath, PrintStyle } from '../../utils';

// -----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -69,6 +70,7 @@ export const stylish: Formatter = results => {
getMessageType(result.severity),
result.code ?? '',
result.message,
printPath(result.path, PrintStyle.Dot),
]);

output += `${table(pathTableData, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ rules:
OpenAPI 3.x detected

{document}
10:15 warning response-order Responses should be in alphabetical order
10:15 warning response-order Responses should be in alphabetical order paths./foo.get.responses[400]

✖ 1 problem (0 errors, 1 warning, 0 infos, 0 hints)
12 changes: 6 additions & 6 deletions test-harness/scenarios/asyncapi2-streetlights.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ components:
type: integer
minimum: 0
maximum: 100

operationTraits:
kafka:
bindings:
Expand All @@ -217,10 +217,10 @@ AsyncAPI 2.x detected

{document}
1:1 warning asyncapi-tags AsyncAPI object should have non-empty `tags` array.
2:6 warning asyncapi-info-contact Info object should contain `contact` object.
45:13 warning asyncapi-operation-description Operation `description` must be present and non-empty string.
57:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string.
68:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string.
79:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string.
2:6 warning asyncapi-info-contact Info object should contain `contact` object. info
45:13 warning asyncapi-operation-description Operation `description` must be present and non-empty string. channels.smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured.publish
57:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/on.subscribe
68:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/off.subscribe
79:15 warning asyncapi-operation-description Operation `description` must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/dim.subscribe

✖ 6 problems (0 errors, 6 warnings, 0 infos, 0 hints)
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ OpenAPI 3.x detected

{document}
1:1 warning oas3-api-servers OpenAPI `servers` must be present and non-empty array.
2:6 warning info-contact Info object should contain `contact` object.
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string.
11:11 warning unique-tag-names Tags should have distinct names: Duplicate tag name 'a-tag'
13:11 warning unique-tag-names Tags should have distinct names: Duplicate tag name 'a-tag'
2:6 warning info-contact Info object should contain `contact` object. info
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string. info
11:11 warning unique-tag-names Tags should have distinct names: Duplicate tag name 'a-tag' tags[2].name
13:11 warning unique-tag-names Tags should have distinct names: Duplicate tag name 'a-tag' tags[3].name

✖ 5 problems (0 errors, 5 warnings, 0 infos, 0 hints)
6 changes: 3 additions & 3 deletions test-harness/scenarios/enabled-rules-amount.oas3.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Linting {document}
OpenAPI 3.x detected

{document}
5:9 warning oas3-parameter-description Parameter objects should have a `description`.
9:11 warning oas3-parameter-description Parameter objects should have a `description`.
16:15 warning oas3-parameter-description Parameter objects should have a `description`.
5:9 warning oas3-parameter-description Parameter objects should have a `description`. paths./pets.parameters[0]
9:11 warning oas3-parameter-description Parameter objects should have a `description`. paths./pets.get.parameters[0]
16:15 warning oas3-parameter-description Parameter objects should have a `description`. components.parameters.skipParam

✖ 3 problems (0 errors, 3 warnings, 0 infos, 0 hints)
16 changes: 8 additions & 8 deletions test-harness/scenarios/examples.oas2.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ definitions:
OpenAPI 2.0 (Swagger) detected

{document}
83:26 error oas2-valid-schema-example `completed` property type should be boolean
91:26 error oas2-valid-schema-example `example` property type should be boolean
95:26 error oas2-valid-schema-example `example` property should match format `date-time`
99:26 error oas2-valid-schema-example `example` property should match format `url`
106:11 warning operation-tag-defined Operation tags should be defined in global tags.
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: `foo`, `bar`
177:30 error oas2-valid-media-example `application/json` property should have required property `name`
194:30 error oas2-valid-media-example `application/json` property should have required property `user`
83:26 error oas2-valid-schema-example `completed` property type should be boolean paths./schema-example.get.responses[500].schema.example.completed
91:26 error oas2-valid-schema-example `example` property type should be boolean paths./schema-example.get.responses[501].schema.properties.some-bool.example
95:26 error oas2-valid-schema-example `example` property should match format `date-time` paths./schema-example.get.responses[501].schema.properties.some-date.example
99:26 error oas2-valid-schema-example `example` property should match format `url` paths./schema-example.get.responses[501].schema.properties.some-url.example
106:11 warning operation-tag-defined Operation tags should be defined in global tags. paths./param-examples.get.tags[0]
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: `foo`, `bar` paths./param-examples.get.parameters[1].x-example
177:30 error oas2-valid-media-example `application/json` property should have required property `name` paths./response-examples.get.responses[500].examples.application/json
194:30 error oas2-valid-media-example `application/json` property should have required property `user` paths./response-examples-via-$ref.get.responses[200].examples.application/json

✖ 8 problems (7 errors, 1 warning, 0 infos, 0 hints)
20 changes: 10 additions & 10 deletions test-harness/scenarios/examples.oas3.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,15 @@ components:
OpenAPI 3.x detected

{document}
126:25 error oas3-valid-schema-example `example` property should have required property `name`
138:30 error oas3-valid-schema-example `example` property type should be boolean
142:30 error oas3-valid-schema-example `example` property should match format `date-time`
146:30 error oas3-valid-schema-example `example` property should match format `url`
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: `foo`, `bar`
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: `foo`, `bar`
197:22 error oas3-valid-media-example `value` property type should be string
199:21 error oas3-valid-media-example `value` property type should be string
262:25 error oas3-valid-media-example `value` property should have required property `id`
282:25 error oas3-valid-media-example `value` property should have required property `user`
126:25 error oas3-valid-schema-example `example` property should have required property `name` paths./schema-example.get.responses[500].content.application/json.schema.example
138:30 error oas3-valid-schema-example `example` property type should be boolean paths./schema-example.get.responses[501].content.application/json.schema.properties.some-bool.example
142:30 error oas3-valid-schema-example `example` property should match format `date-time` paths./schema-example.get.responses[501].content.application/json.schema.properties.some-date.example
146:30 error oas3-valid-schema-example `example` property should match format `url` paths./schema-example.get.responses[501].content.application/json.schema.properties.some-url.example
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: `foo`, `bar` paths./param-examples.get.parameters[1].example
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: `foo`, `bar` paths./param-examples.get.parameters[3].schema.example
197:22 error oas3-valid-media-example `value` property type should be string paths./param-examples.get.parameters[4].examples.the-bad.value
199:21 error oas3-valid-media-example `value` property type should be string paths./param-examples.get.parameters[4].examples.the-ugly.value
262:25 error oas3-valid-media-example `value` property should have required property `id` paths./response-examples.get.responses[500].content.application/json.examples.response.value
282:25 error oas3-valid-media-example `value` property should have required property `user` paths./response-examples-via-$ref.get.responses[200].content.application/json.examples.response.value

✖ 10 problems (10 errors, 0 warnings, 0 infos, 0 hints)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ info:
title: Circular circurlarity
description: This is a description
version: draft
contact:
contact:
email: me@me.com
paths:
/self_referencing_graphs:
Expand Down Expand Up @@ -76,11 +76,11 @@ OpenAPI 3.x detected

{document}
1:1 warning oas3-api-servers OpenAPI `servers` must be present and non-empty array.
10:9 warning operation-operationId Operation should have an `operationId`.
10:9 warning operation-tags Operation should have non-empty `tags` array.
22:9 warning operation-operationId Operation should have an `operationId`.
22:9 warning operation-tags Operation should have non-empty `tags` array.
34:9 warning operation-operationId Operation should have an `operationId`.
34:9 warning operation-tags Operation should have non-empty `tags` array.
10:9 warning operation-operationId Operation should have an `operationId`. paths./self_referencing_graphs.get
10:9 warning operation-tags Operation should have non-empty `tags` array. paths./self_referencing_graphs.get
22:9 warning operation-operationId Operation should have an `operationId`. paths./cyclic_graphs.get
22:9 warning operation-tags Operation should have non-empty `tags` array. paths./cyclic_graphs.get
34:9 warning operation-operationId Operation should have an `operationId`. paths./looping_graphs.get
34:9 warning operation-tags Operation should have non-empty `tags` array. paths./looping_graphs.get

✖ 7 problems (0 errors, 7 warnings, 0 infos, 0 hints)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ info:
title: Circular circurlarity
description: This is a description
version: draft
contact:
contact:
email: me@me.com
paths:
/self_referencing_graphs:
Expand Down Expand Up @@ -87,11 +87,11 @@ OpenAPI 3.x detected

{document}
1:1 warning oas3-api-servers OpenAPI `servers` must be present and non-empty array.
10:9 warning operation-operationId Operation should have an `operationId`.
10:9 warning operation-tags Operation should have non-empty `tags` array.
22:9 warning operation-operationId Operation should have an `operationId`.
22:9 warning operation-tags Operation should have non-empty `tags` array.
34:9 warning operation-operationId Operation should have an `operationId`.
34:9 warning operation-tags Operation should have non-empty `tags` array.
10:9 warning operation-operationId Operation should have an `operationId`. paths./self_referencing_graphs.get
10:9 warning operation-tags Operation should have non-empty `tags` array. paths./self_referencing_graphs.get
22:9 warning operation-operationId Operation should have an `operationId`. paths./cyclic_graphs.get
22:9 warning operation-tags Operation should have non-empty `tags` array. paths./cyclic_graphs.get
34:9 warning operation-operationId Operation should have an `operationId`. paths./looping_graphs.get
34:9 warning operation-tags Operation should have non-empty `tags` array. paths./looping_graphs.get

✖ 7 problems (0 errors, 7 warnings, 0 infos, 0 hints)
2 changes: 1 addition & 1 deletion test-harness/scenarios/exceptions.oas3.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ except:
OpenAPI 3.x detected

{document}
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string.
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string. info

✖ 1 problem (0 errors, 1 warning, 0 infos, 0 hints)
4 changes: 2 additions & 2 deletions test-harness/scenarios/exceptions.oas3.stdin.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ OpenAPI 3.x detected
<STDIN>
1:1 warning except-but-stdin The ruleset contains `except` entries. However, they cannot be enforced when the input is passed through stdin.
1:1 warning oas3-api-servers OpenAPI `servers` must be present and non-empty array.
2:6 warning info-contact Info object should contain `contact` object.
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string.
2:6 warning info-contact Info object should contain `contact` object. info
2:6 warning info-description OpenAPI object info `description` must be present and non-empty string. info

✖ 4 problems (0 errors, 4 warnings, 0 infos, 0 hints)
4 changes: 2 additions & 2 deletions test-harness/scenarios/external-schemas-ruleset.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ info:
OpenAPI 3.x detected

{document}
3:10 error info-title `title` property should be equal to one of the allowed values: `Stoplight`, `Stoplight.io`, `StoplightIO`. Did you mean `Stoplight`?
4:16 error info-description `description` property should be equal to one of the allowed values: `foo`, `foo-bar`, `bar-foo`
3:10 error info-title `title` property should be equal to one of the allowed values: `Stoplight`, `Stoplight.io`, `StoplightIO`. Did you mean `Stoplight`? info.title
4:16 error info-description `description` property should be equal to one of the allowed values: `foo`, `foo-bar`, `bar-foo` info.description

✖ 2 problems (2 errors, 0 warnings, 0 infos, 0 hints)
4 changes: 2 additions & 2 deletions test-harness/scenarios/oas2-valid-media-example.oas2.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ rules:
OpenAPI 2.0 (Swagger) detected

{document}
11:31 error oas2-valid-media-example `application/json` property type should be boolean
26:32 error oas2-valid-media-example `application/yaml` property should have required property `user`
11:31 error oas2-valid-media-example `application/json` property type should be boolean paths./pets.get.responses[200].examples.application/json
26:32 error oas2-valid-media-example `application/yaml` property should have required property `user` paths./pets.get.post.responses[200].examples.application/yaml

✖ 2 problems (2 errors, 0 warnings, 0 infos, 0 hints)
14 changes: 7 additions & 7 deletions test-harness/scenarios/oas3-schema.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ rules:
OpenAPI 3.x detected

{document}
5:11 error oas3-schema Property `foo` is not expected to be here.
12:11 error oas3-schema Property `type` is not expected to be here.
23:18 error oas3-schema `type` property type should be string.
32:28 error oas3-schema `user_id` property type should be object.
36:28 error oas3-schema `properties` property type should be object.
38:23 error oas3-schema `description` property type should be string.
41:17 error oas3-schema `responses` property should not have fewer than 1 properties.
5:11 error oas3-schema Property `foo` is not expected to be here. info.contact
12:11 error oas3-schema Property `type` is not expected to be here. paths./user.get.parameters[0]
23:18 error oas3-schema `type` property type should be string. paths./user.get.parameters[1].schema.type
32:28 error oas3-schema `user_id` property type should be object. paths./user.get.responses[200].content.application/json.schema.properties.user_id
36:28 error oas3-schema `properties` property type should be object. paths./user.get.responses[200].content.application/yaml.schema.properties
38:23 error oas3-schema `description` property type should be string. paths./user.get.responses[400].description
41:17 error oas3-schema `responses` property should not have fewer than 1 properties. paths./address.get.responses

✖ 7 problems (7 errors, 0 warnings, 0 infos, 0 hints)
Loading

0 comments on commit 83b1d61

Please sign in to comment.