-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rulesets): support 2.1.0, 2.2.0, 2.3.0 AsyncAPI versions
- Loading branch information
1 parent
6b21eb1
commit 15a667a
Showing
11 changed files
with
562 additions
and
1,528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import type { Format } from '@stoplight/spectral-core'; | ||
import { isPlainObject } from '@stoplight/json'; | ||
|
||
type MaybeAsyncApi2 = Partial<{ asyncapi: unknown }>; | ||
type MaybeAAS2 = { asyncapi: unknown } & Record<string, unknown>; | ||
|
||
const bearsAStringPropertyNamed = (document: unknown, propertyName: string): boolean => { | ||
return isPlainObject(document) && propertyName in document && typeof document[propertyName] === 'string'; | ||
}; | ||
const aas2Regex = /^2\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/; | ||
const aas2_0Regex = /^2\.0(?:\.[0-9]*)?$/; | ||
const aas2_1Regex = /^2\.1(?:\.[0-9]*)?$/; | ||
const aas2_2Regex = /^2\.2(?:\.[0-9]*)?$/; | ||
const aas2_3Regex = /^2\.3(?:\.[0-9]*)?$/; | ||
|
||
const version2Regex = /^2\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/; | ||
const isAas2 = (document: unknown): document is { asyncapi: string } & Record<string, unknown> => | ||
isPlainObject(document) && 'asyncapi' in document && aas2Regex.test(String((document as MaybeAAS2).asyncapi)); | ||
|
||
export const asyncApi2: Format = document => { | ||
if (!bearsAStringPropertyNamed(document, 'asyncapi')) { | ||
return false; | ||
} | ||
export const aas2: Format = isAas2; | ||
aas2.displayName = 'AsyncAPI 2.x'; | ||
|
||
const version = String((document as MaybeAsyncApi2).asyncapi); | ||
// for backward compatibility | ||
export const asyncApi2 = aas2; | ||
export const asyncapi2 = aas2; | ||
|
||
return version2Regex.test(version); | ||
}; | ||
export const aas2_0: Format = (document: unknown): boolean => | ||
isAas2(document) && aas2_0Regex.test(String((document as MaybeAAS2).asyncapi)); | ||
aas2_0.displayName = 'AsyncAPI 2.0.x'; | ||
|
||
asyncApi2.displayName = 'AsyncAPI 2.x'; | ||
export const aas2_1: Format = (document: unknown): boolean => | ||
isAas2(document) && aas2_1Regex.test(String((document as MaybeAAS2).asyncapi)); | ||
aas2_1.displayName = 'AsyncAPI 2.1.x'; | ||
|
||
export { asyncApi2 as asyncapi2 }; | ||
export const aas2_2: Format = (document: unknown): boolean => | ||
isAas2(document) && aas2_2Regex.test(String((document as MaybeAAS2).asyncapi)); | ||
aas2_2.displayName = 'AsyncAPI 2.2.x'; | ||
|
||
export const aas2_3: Format = (document: unknown): boolean => | ||
isAas2(document) && aas2_3Regex.test(String((document as MaybeAAS2).asyncapi)); | ||
aas2_3.displayName = 'AsyncAPI 2.3.x'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/rulesets/src/asyncapi/__tests__/__helpers__/tester.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import testRule from '../../../__tests__/__helpers__/tester'; | ||
import testRule, { createWithRules } from '../../../__tests__/__helpers__/tester'; | ||
|
||
export { testRule as default }; | ||
export { testRule as default, createWithRules }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.