Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/core/json-rul…
Browse files Browse the repository at this point in the history
…eset-validation
  • Loading branch information
P0lip committed Apr 14, 2022
2 parents e08ba8b + 4ececf0 commit 49be390
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
@@ -0,0 +1,2 @@
* @stoplightio/oss-spectral
/packages/rulesets/src/asyncapi @magicmatatjahu @smoya @jonaslagoni
6 changes: 5 additions & 1 deletion docs/getting-started/3-rulesets.md
Expand Up @@ -75,7 +75,11 @@ The `extends` keyword can be combined with extra rules in order to extend and ov

Formats are an optional way to specify which API description formats a rule, or ruleset, is applicable to. Currently Spectral supports these formats:

- `asyncapi2` (AsyncAPI v2.0)
- `aas2` (AsyncAPI v2.x)
- `aas2_0` (AsyncAPI v2.0.0)
- `aas2_1` (AsyncAPI v2.1.0)
- `aas2_2` (AsyncAPI v2.2.0)
- `aas2_3` (AsyncAPI v2.3.0)
- `oas2` (OpenAPI v2.0)
- `oas3` (OpenAPI v3.x)
- `oas3.0` (OpenAPI v3.0.x)
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/asyncapi-rules.md
Expand Up @@ -369,3 +369,15 @@ that acts as a library (a container storing reusable objects, leveraged by other
specifications that reference those objects).

**Recommended:** Yes

### asyncapi-unused-components-server

Potential unused reusable `server` entry has been detected.

<!-- theme: warning -->

_Warning:_ This rule may identify false positives when linting a specification
that acts as a library (a container storing reusable objects, leveraged by other
specifications that reference those objects).

**Recommended:** Yes
7 changes: 7 additions & 0 deletions packages/core/CHANGELOG.md
@@ -1,3 +1,10 @@
# [@stoplight/spectral-core-v1.11.1](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-core-v1.11.0...@stoplight/spectral-core-v1.11.1) (2022-03-16)


### Bug Fixes

* **core:** bump nimma from 0.1.8 to 0.2.0 ([#2088](https://github.com/stoplightio/spectral/issues/2088)) ([36ec40e](https://github.com/stoplightio/spectral/commit/36ec40e92abb6fe0ffbc104722599147dda555fb))

# [@stoplight/spectral-core-v1.11.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-core-v1.10.2...@stoplight/spectral-core-v1.11.0) (2022-03-03)


Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
@@ -1,6 +1,6 @@
{
"name": "@stoplight/spectral-core",
"version": "1.11.0",
"version": "1.11.1",
"sideEffects": false,
"homepage": "https://github.com/stoplightio/spectral",
"bugs": "https://github.com/stoplightio/spectral/issues",
Expand Down Expand Up @@ -50,7 +50,7 @@
"lodash": "~4.17.21",
"lodash.topath": "^4.5.2",
"minimatch": "3.0.4",
"nimma": "0.1.8",
"nimma": "0.2.0",
"pony-cause": "^1.0.0",
"simple-eval": "1.0.0",
"tslib": "^2.3.0"
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/runner/runner.ts
Expand Up @@ -99,6 +99,7 @@ function execute(input: unknown, callbacks: Record<string, Callback[]>, jsonPath
fallback: jsonPathPlus,
unsafe: false,
output: 'auto',
customShorthands: {},
});

nimma.query(
Expand Down
7 changes: 7 additions & 0 deletions packages/functions/CHANGELOG.md
@@ -1,3 +1,10 @@
# [@stoplight/spectral-functions-v1.6.1](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-functions-v1.6.0...@stoplight/spectral-functions-v1.6.1) (2022-03-08)


### Bug Fixes

* **functions:** reset RegExp.lastIndex to zero when using cached RegExp objects ([#2079](https://github.com/stoplightio/spectral/issues/2079)) ([4839527](https://github.com/stoplightio/spectral/commit/48395273d28fc58ab450277681956b1b50f3f8ee))

# [@stoplight/spectral-functions-v1.6.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-functions-v1.5.2...@stoplight/spectral-functions-v1.6.0) (2022-03-03)


Expand Down
2 changes: 1 addition & 1 deletion packages/functions/package.json
@@ -1,6 +1,6 @@
{
"name": "@stoplight/spectral-functions",
"version": "1.6.0",
"version": "1.6.1",
"sideEffects": false,
"homepage": "https://github.com/stoplightio/spectral",
"bugs": "https://github.com/stoplightio/spectral/issues",
Expand Down
6 changes: 6 additions & 0 deletions packages/functions/src/__tests__/pattern.test.ts
Expand Up @@ -18,6 +18,12 @@ describe('Core Functions / Pattern', () => {
expect(await runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]);
});

it('should return same results when given a global (g) marker (pattern cache usecase)', async () => {
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
});

it('given string regex containing invalid flags, should throw an exception', async () => {
await expect(runPattern('aBc', { match: '/[abc]+/invalid' })).rejects.toThrow(
"Invalid flags supplied to RegExp constructor 'invalid'",
Expand Down
1 change: 1 addition & 0 deletions packages/functions/src/pattern.ts
Expand Up @@ -27,6 +27,7 @@ const cache = new Map<string, RegExp>();
function getFromCache(pattern: string): RegExp {
const existingPattern = cache.get(pattern);
if (existingPattern !== void 0) {
existingPattern.lastIndex = 0;
return existingPattern;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/ruleset-migrator/CHANGELOG.md
@@ -1,3 +1,10 @@
# [@stoplight/spectral-ruleset-migrator-v1.7.3](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-ruleset-migrator-v1.7.2...@stoplight/spectral-ruleset-migrator-v1.7.3) (2022-03-10)


### Bug Fixes

* **ruleset-migrator:** validate aliases correctly ([#2085](https://github.com/stoplightio/spectral/issues/2085)) ([1f4ab20](https://github.com/stoplightio/spectral/commit/1f4ab208c21c7a1d1263dfd50cc6da658568e871))

# [@stoplight/spectral-ruleset-migrator-v1.7.2](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-ruleset-migrator-v1.7.1...@stoplight/spectral-ruleset-migrator-v1.7.2) (2022-01-26)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/ruleset-migrator/package.json
@@ -1,6 +1,6 @@
{
"name": "@stoplight/spectral-ruleset-migrator",
"version": "1.7.2",
"version": "1.7.3",
"homepage": "https://github.com/stoplightio/spectral",
"bugs": "https://github.com/stoplightio/spectral/issues",
"author": "Stoplight <support@stoplight.io>",
Expand Down
@@ -1,7 +1,7 @@
module.exports = {
aliases: {
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},
};
@@ -1,7 +1,7 @@
export default {
aliases: {
PathItem: '$.paths[*][*]',
Description: '$..description',
Name: '$..name',
PathItem: ['$.paths[*][*]'],
Description: ['$..description'],
Name: ['$..name'],
},
};
@@ -1,4 +1,7 @@
aliases:
PathItem: $.paths[*][*]
Description: $..description
Name: $..name
PathItem:
- $.paths[*][*]
Description:
- $..description
Name:
- $..name
Expand Up @@ -6,11 +6,11 @@ module.exports = {
targets: [
{
formats: [oas2],
given: '$.definitions[*]',
given: ['$.definitions[*]'],
},
{
formats: [oas3_0, oas3_1],
given: '$.components.schemas[*]',
given: ['$.components.schemas[*]'],
},
],
},
Expand Down
Expand Up @@ -6,11 +6,11 @@ export default {
targets: [
{
formats: [oas2],
given: '$.definitions[*]',
given: ['$.definitions[*]'],
},
{
formats: [oas3_0, oas3_1],
given: '$.components.schemas[*]',
given: ['$.components.schemas[*]'],
},
],
},
Expand Down
Expand Up @@ -4,8 +4,10 @@ aliases:
targets:
- formats:
- oas2
given: $.definitions[*]
given:
- $.definitions[*]
- formats:
- oas3.0
- oas3.1
given: $.components.schemas[*]
given:
- $.components.schemas[*]
10 changes: 8 additions & 2 deletions packages/ruleset-migrator/src/validation/schema.ts
Expand Up @@ -9,7 +9,10 @@ const schema = {
additionalProperties: {
oneOf: [
{
type: 'string',
type: 'array',
items: {
type: 'string',
},
},
{
type: 'object',
Expand All @@ -27,7 +30,10 @@ const schema = {
$ref: '#/properties/formats',
},
given: {
type: 'string',
type: 'array',
items: {
type: 'string',
},
},
},
required: ['formats', 'given'],
Expand Down
6 changes: 3 additions & 3 deletions packages/ruleset-migrator/src/validation/types.ts
Expand Up @@ -3,7 +3,7 @@
export interface Ruleset {
aliases?: {
[k: string]:
| string
| string[]
| {
description?: string;
targets: [
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface Ruleset {
| 'json-schema-2020-12'
)[]
];
given: string;
given: string[];
[k: string]: unknown;
},
...{
Expand Down Expand Up @@ -80,7 +80,7 @@ export interface Ruleset {
| 'json-schema-2020-12'
)[]
];
given: string;
given: string[];
[k: string]: unknown;
}[]
];
Expand Down
7 changes: 7 additions & 0 deletions packages/rulesets/CHANGELOG.md
@@ -1,3 +1,10 @@
# [@stoplight/spectral-rulesets-v1.7.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-rulesets-v1.6.0...@stoplight/spectral-rulesets-v1.7.0) (2022-03-24)


### Features

* **rulesets:** add unused components server rule ([#2097](https://github.com/stoplightio/spectral/issues/2097)) ([71b312e](https://github.com/stoplightio/spectral/commit/71b312e34c85d8b4255832757b4b5afa8c5062a5))

# [@stoplight/spectral-rulesets-v1.6.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-rulesets-v1.5.2...@stoplight/spectral-rulesets-v1.6.0) (2022-03-03)


Expand Down
2 changes: 1 addition & 1 deletion packages/rulesets/package.json
@@ -1,6 +1,6 @@
{
"name": "@stoplight/spectral-rulesets",
"version": "1.6.0",
"version": "1.7.0",
"homepage": "https://github.com/stoplightio/spectral",
"bugs": "https://github.com/stoplightio/spectral/issues",
"author": "Stoplight <support@stoplight.io>",
Expand Down
@@ -0,0 +1,40 @@
import { DiagnosticSeverity } from '@stoplight/types';
import produce from 'immer';
import testRule from './__helpers__/tester';

const document = {
asyncapi: '2.0.0',
servers: {
test: {
$ref: '#/components/servers/externallyDefinedServer',
},
},
channels: {},
components: {
servers: {
externallyDefinedServer: {},
},
},
};

testRule('asyncapi-unused-components-server', [
{
name: 'valid case',
document,
errors: [],
},

{
name: 'components.servers contains unreferenced objects',
document: produce(document, (draft: any) => {
delete draft.servers['test'];
}),
errors: [
{
message: 'Potentially unused components server has been detected.',
path: ['components', 'servers', 'externallyDefinedServer'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
13 changes: 13 additions & 0 deletions packages/rulesets/src/asyncapi/index.ts
Expand Up @@ -381,5 +381,18 @@ export default {
},
},
},
'asyncapi-unused-components-server': {
description: 'Potentially unused components server has been detected.',
recommended: true,
type: 'style',
resolved: false,
given: '$.components.servers',
then: {
function: unreferencedReusableObject,
functionOptions: {
reusableObjectsLocation: '#/components/servers',
},
},
},
},
};
10 changes: 5 additions & 5 deletions yarn.lock
Expand Up @@ -2403,7 +2403,7 @@ __metadata:
lodash: ~4.17.21
lodash.topath: ^4.5.2
minimatch: 3.0.4
nimma: 0.1.8
nimma: 0.2.0
nock: ^13.1.0
pony-cause: ^1.0.0
simple-eval: 1.0.0
Expand Down Expand Up @@ -9337,9 +9337,9 @@ __metadata:
languageName: node
linkType: hard

"nimma@npm:0.1.8":
version: 0.1.8
resolution: "nimma@npm:0.1.8"
"nimma@npm:0.2.0":
version: 0.2.0
resolution: "nimma@npm:0.2.0"
dependencies:
"@jsep-plugin/regex": ^1.0.1
"@jsep-plugin/ternary": ^1.0.2
Expand All @@ -9352,7 +9352,7 @@ __metadata:
optional: true
lodash.topath:
optional: true
checksum: 0e2f832b20f7c4cc4497c82fef1bd1fee6512db5251eb59223445f9ea7749c3baa981389ef5008b46efd0aa4e11472b62e81118b5933aeafd6f234d87dd49df7
checksum: ed404ab3aca227a51e924e2b8d7d9ae75695c6c3fbdbaffee9990b42b5491f3c240ef3cdbbaffd1b9c3564bf062c936cb8b921de6b21ce8aa22733a00597fd2d
languageName: node
linkType: hard

Expand Down

0 comments on commit 49be390

Please sign in to comment.