diff --git a/packages/apidom-ns-openapi-3-1/src/index.ts b/packages/apidom-ns-openapi-3-1/src/index.ts index 979588a4b..bc398f36c 100644 --- a/packages/apidom-ns-openapi-3-1/src/index.ts +++ b/packages/apidom-ns-openapi-3-1/src/index.ts @@ -443,6 +443,7 @@ export { isMediaTypeElement, isServerElement, isSecurityRequirementElement, + isSecuritySchemeElement, isExternalDocumentationElement, isServerVariableElement, isContactElement, diff --git a/packages/apidom-ns-openapi-3-1/src/predicates.ts b/packages/apidom-ns-openapi-3-1/src/predicates.ts index f83a7eaa1..d548a9414 100644 --- a/packages/apidom-ns-openapi-3-1/src/predicates.ts +++ b/packages/apidom-ns-openapi-3-1/src/predicates.ts @@ -29,6 +29,7 @@ import ResponseElement from './elements/Response'; import ResponsesElement from './elements/Responses'; import SchemaElement from './elements/Schema'; import SecurityRequirementElement from './elements/SecurityRequirement'; +import SecuritySchemeElement from './elements/SecurityScheme'; import ServerElement from './elements/Server'; import ServerVariableElement from './elements/ServerVariable'; import MediaTypeElement from './elements/MediaType'; @@ -303,6 +304,16 @@ export const isSecurityRequirementElement = createPredicate( }, ); +export const isSecuritySchemeElement = createPredicate( + ({ hasBasicElementProps, isElementType, primitiveEq }) => { + return (element: unknown): element is SecuritySchemeElement => + element instanceof SecuritySchemeElement || + (hasBasicElementProps(element) && + isElementType('securityScheme', element) && + primitiveEq('object', element)); + }, +); + export const isServerElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { return (element: unknown): element is ServerElement => diff --git a/packages/apidom-ns-openapi-3-1/test/predicates.ts b/packages/apidom-ns-openapi-3-1/test/predicates.ts index 3da6913a0..385e768a4 100644 --- a/packages/apidom-ns-openapi-3-1/test/predicates.ts +++ b/packages/apidom-ns-openapi-3-1/test/predicates.ts @@ -11,6 +11,7 @@ import { isOpenApi3_1Element, isServerElement, isServerVariableElement, + isSecuritySchemeElement, isPathsElement, isPathItemElement, isOperationElement, @@ -23,6 +24,7 @@ import { ContactElement, ServerElement, ServerVariableElement, + SecuritySchemeElement, PathsElement, PathItemElement, OperationElement, @@ -515,6 +517,59 @@ describe('predicates', function () { }); }); + context('isSecuritySchemeElement', function () { + context('given SecuritySchemeElement instance value', function () { + specify('should return true', function () { + const element = new SecuritySchemeElement(); + + assert.isTrue(isSecuritySchemeElement(element)); + }); + }); + + context('given subtype instance value', function () { + specify('should return true', function () { + class SecuritySchemeSubElement extends SecuritySchemeElement {} + + assert.isTrue(isSecuritySchemeElement(new SecuritySchemeSubElement())); + }); + }); + + context('given non SecuritySchemeElement instance value', function () { + specify('should return false', function () { + assert.isFalse(isSecuritySchemeElement(1)); + assert.isFalse(isSecuritySchemeElement(null)); + assert.isFalse(isSecuritySchemeElement(undefined)); + assert.isFalse(isSecuritySchemeElement({})); + assert.isFalse(isSecuritySchemeElement([])); + assert.isFalse(isSecuritySchemeElement('string')); + }); + }); + + specify('should support duck-typing', function () { + const securitySchemeElementDuck = { + _storedElement: 'securityScheme', + _content: [], + primitive() { + return 'object'; + }, + get element() { + return this._storedElement; + }, + }; + + const securitySchemeElementSwan = { + _storedElement: undefined, + _content: undefined, + primitive() { + return 'swan'; + }, + }; + + assert.isTrue(isSecuritySchemeElement(securitySchemeElementDuck)); + assert.isFalse(isSecuritySchemeElement(securitySchemeElementSwan)); + }); + }); + context('isPathsElement', function () { context('given PathsElement instance value', function () { specify('should return true', function () {