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(ns-openapi-3-1): add Security Schema Object predicate #3811

Merged
merged 1 commit into from
Feb 12, 2024
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
1 change: 1 addition & 0 deletions packages/apidom-ns-openapi-3-1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export {
isMediaTypeElement,
isServerElement,
isSecurityRequirementElement,
isSecuritySchemeElement,
isExternalDocumentationElement,
isServerVariableElement,
isContactElement,
Expand Down
11 changes: 11 additions & 0 deletions packages/apidom-ns-openapi-3-1/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 =>
Expand Down
55 changes: 55 additions & 0 deletions packages/apidom-ns-openapi-3-1/test/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isOpenApi3_1Element,
isServerElement,
isServerVariableElement,
isSecuritySchemeElement,
isPathsElement,
isPathItemElement,
isOperationElement,
Expand All @@ -23,6 +24,7 @@ import {
ContactElement,
ServerElement,
ServerVariableElement,
SecuritySchemeElement,
PathsElement,
PathItemElement,
OperationElement,
Expand Down Expand Up @@ -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 () {
Expand Down