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

feat(core): aliases take only array-ish given #2033

Merged
merged 3 commits into from
Jan 27, 2022
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
31 changes: 20 additions & 11 deletions docs/guides/4-custom-rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ It has a specific syntax known as [JSONPath](https://goessner.net/articles/JsonP
Both of them support all the main JSONPath functionality and a little bit more, but this syntax may differ slightly from other JSONPath implementations.

Your `given` value can be a string containing any valid JSONPath expression, or an array of expressions to apply a rule to multiple parts of a document.
You can also consume your [aliases][#aliases] here if you have some defined.
You can also consume your (aliases)[#aliases] here if you have some defined.

Use the [JSONPath Online Evaluator](http://jsonpath.com/) to determine what `given` path you want.

Expand Down Expand Up @@ -328,17 +328,22 @@ Targeting certain parts of an OpenAPI spec is powerful but it can become cumbers
Define aliases for commonly used JSONPath expressions on a global level which can then be reused across the ruleset.

Aliases can be defined in an array of key-value pairs at the root level of the ruleset.
It's a superset of `given`, with the notable difference being the possibility to distinguish between different formats.
It's similar to `given`, with the notable difference being the possibility to distinguish between different formats.

**Example**

```yaml
aliases:
HeaderNames: "$..parameters.[?(@.in === 'header')].name"
Info: "$..info"
InfoDescription: "#Info.description"
InfoContact: "#Info.contact"
Paths: "$.paths[*]~"
HeaderNames:
- "$..parameters.[?(@.in === 'header')].name"
Info:
- "$..info"
InfoDescription:
- "#Info.description"
InfoContact:
- "#Info.contact"
Paths:
- "$.paths[*]~"
```

If you deal with a variety of different spec, you may find the above approach insufficient, particularly when the shape of the document is notably different.
Expand All @@ -351,10 +356,12 @@ aliases:
targets:
P0lip marked this conversation as resolved.
Show resolved Hide resolved
- formats:
- oas2
given: $.parameters[*]
given:
- $.parameters[*]
- formats:
- oas3
given: $.components.parameters[*]
given:
- $.components.parameters[*]
```

Now, if you referenced `SharedParameterObject` alias, the chosen path would be determined based on the document you use.
Expand All @@ -366,8 +373,10 @@ To make it more feasible and avoid overly complex JSONPath expressions, `given`

```yaml
aliases:
PathItemObject: $.paths[*]
OperationObject: "#PathItem[get,put,post,delete,options,head,patch,trace]"
PathItemObject:
- $.paths[*]
OperationObject:
- "#PathItem[get,put,post,delete,options,head,patch,trace]"
ParameterObject:
description: an optional property describing the purpose of the alias
targets:
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/meta/ruleset.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"$ref": "shared#formats"
},
"given": {
"$ref": "shared#given"
"$ref": "shared#arrayish-given"
}
},
"required": ["formats", "given"],
Expand All @@ -208,7 +208,7 @@
}
},
"else": {
"$ref": "shared#given"
"$ref": "shared#arrayish-given"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/meta/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"type": "array"
},
"then": {
"$anchor": "arrayish-given",
"type": "array",
"items": {
"$ref": "path-expression"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import {falsy, pattern, truthy} from '@stoplight/spectral-functions';
import { falsy, pattern, truthy } from '@stoplight/spectral-functions';
import { RulesetDefinition } from '@stoplight/spectral-core';

export { ruleset as default };

const ruleset: RulesetDefinition = {
aliases: {
Stoplight: '$..stoplight',
Stoplight: ['$..stoplight'],
},
overrides: [
{
Expand All @@ -29,7 +29,7 @@ const ruleset: RulesetDefinition = {
{
files: ['**/*.json'],
aliases: {
Value: '$..value',
Value: ['$..value'],
},
rules: {
'truthy-stoplight-property': {
Expand Down
66 changes: 33 additions & 33 deletions packages/core/src/ruleset/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,10 @@ describe('Ruleset', () => {
print(
new Ruleset({
aliases: {
Info: '$.info',
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
Info: ['$.info'],
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},

rules: {
Expand Down Expand Up @@ -1066,10 +1066,10 @@ describe('Ruleset', () => {
JSON.stringify(
new Ruleset({
aliases: {
Info: '$.info',
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
Info: ['$.info'],
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},

rules: {
Expand Down Expand Up @@ -1108,10 +1108,10 @@ describe('Ruleset', () => {
incompatibleValues: DiagnosticSeverity.Error,
},
aliases: {
Info: '$.info',
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
Info: ['$.info'],
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},
rules: {
'valid-path': {
Expand Down Expand Up @@ -1179,10 +1179,10 @@ describe('Ruleset', () => {
print(
new Ruleset({
aliases: {
Info: '$.info',
InfoDescription: '#Info.description',
InfoContact: '#Info.contact',
InfoContactName: '#InfoContact.name',
Info: ['$.info'],
InfoDescription: ['#Info.description'],
InfoContact: ['#Info.contact'],
InfoContactName: ['#InfoContact.name'],
},

rules: {
Expand Down Expand Up @@ -1241,7 +1241,7 @@ describe('Ruleset', () => {
new Ruleset({
extends: {
aliases: {
PathItem: '$.paths[*][*]',
PathItem: ['$.paths[*][*]'],
},
rules: {},
},
Expand All @@ -1262,10 +1262,10 @@ describe('Ruleset', () => {
() =>
new Ruleset({
aliases: {
Root: '#Info',
Info: '#Root.test',
Contact: '#Info',
Test: '#Contact.test',
Root: ['#Info'],
Info: ['#Root.test'],
Contact: ['#Info'],
Test: ['#Contact.test'],
},
rules: {
'valid-path': {
Expand All @@ -1287,9 +1287,9 @@ describe('Ruleset', () => {
new Ruleset({
extends: {
aliases: {
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},
rules: {},
},
Expand Down Expand Up @@ -1331,17 +1331,17 @@ describe('Ruleset', () => {
targets: [
{
formats: [draft4],
given: '$..id',
given: ['$..id'],
},
{
formats: [draft6, draft7],
given: '$..$id',
given: ['$..$id'],
},
],
},

PathItem: '$.paths[*]',
OperationObject: '#PathItem[get,put,post,delete,options,head,patch,trace]',
PathItem: ['$.paths[*]'],
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
ParametersDefinitionsObject: {
targets: [
{ formats: [oas2], given: ['$.parameters'] },
Expand Down Expand Up @@ -1461,7 +1461,7 @@ describe('Ruleset', () => {
targets: [
{
formats: [draft6],
given: '$..$id',
given: ['$..$id'],
},
],
},
Expand Down Expand Up @@ -1500,11 +1500,11 @@ describe('Ruleset', () => {
targets: [
{
formats: [draft4],
given: '$..id',
given: ['$..id'],
},
{
formats: [draft6, draft7],
given: '$..$id',
given: ['$..$id'],
},
],
},
Expand Down Expand Up @@ -1536,11 +1536,11 @@ describe('Ruleset', () => {
targets: [
{
formats: ['draft4'],
given: '$..id',
given: ['$..id'],
},
{
formats: ['draft6', 'draft7'],
given: '$..$id',
given: ['$..$id'],
},
],
},
Expand Down
Loading