-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formats): support 2.1.0, 2.2.0, 2.3.0 AsyncAPI versions (#2067)
- Loading branch information
1 parent
7fdf0e1
commit b0b008d
Showing
2 changed files
with
80 additions
and
21 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'; |