Skip to content

Commit

Permalink
feat(ns-openapi-3-0): add Security Schema Object predicate (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Feb 9, 2024
1 parent 444a4ba commit 9b09658
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/apidom-ns-openapi-3-0/src/index.ts
Expand Up @@ -44,6 +44,7 @@ export {
isSchemaElement,
isBooleanJsonSchemaElement,
isSecurityRequirementElement,
isSecuritySchemeElement,
isServerElement,
isServerVariableElement,
isMediaTypeElement,
Expand Down
11 changes: 11 additions & 0 deletions packages/apidom-ns-openapi-3-0/src/predicates.ts
Expand Up @@ -22,6 +22,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 @@ -246,6 +247,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-0/test/predicates.ts
Expand Up @@ -11,6 +11,7 @@ import {
isOpenApi3_0Element,
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

0 comments on commit 9b09658

Please sign in to comment.