From c958d82c4c92f270db3930afccb60191b5719d8d Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Wed, 18 Oct 2023 09:44:07 +0200 Subject: [PATCH 1/2] feat(predicates): implement TypeScript type guards Refs #3280 --- packages/apidom-core/src/index.ts | 1 + .../apidom-core/src/predicates/helpers.ts | 80 +++++++++++++++---- packages/apidom-core/src/predicates/index.ts | 46 +++++++---- packages/apidom-core/test/predicates/index.ts | 2 +- packages/apidom-core/test/traversal/filter.ts | 13 ++- packages/apidom-core/test/traversal/find.ts | 6 +- .../src/predicates.ts | 16 ++-- .../apidom-ns-asyncapi-2/src/predicates.ts | 54 +++++++------ .../src/predicates.ts | 8 +- .../src/predicates.ts | 4 +- .../src/predicates.ts | 4 +- packages/apidom-ns-openapi-2/README.md | 1 + .../apidom-ns-openapi-2/src/predicates.ts | 38 ++++----- .../apidom-ns-openapi-3-0/src/predicates.ts | 66 ++++++++------- .../apidom-ns-openapi-3-1/src/predicates.ts | 68 +++++++++------- .../elements/Schema/embedded-resources-$id.ts | 12 +-- .../Schema/embedded-resources-$schema.ts | 10 +-- .../strategies/openapi-3-0/visitor.ts | 3 +- .../strategies/openapi-3-1/visitor.ts | 3 +- 19 files changed, 267 insertions(+), 168 deletions(-) diff --git a/packages/apidom-core/src/index.ts b/packages/apidom-core/src/index.ts index 019218ef04..a1598c2e59 100644 --- a/packages/apidom-core/src/index.ts +++ b/packages/apidom-core/src/index.ts @@ -43,6 +43,7 @@ export { includesSymbols, includesClasses, } from './predicates/index'; +export type { ElementPredicate } from './predicates/helpers'; export { default as createPredicate } from './predicates/helpers'; export { filter, reject, find, findAtOffset, some, traverse, parents } from './traversal/index'; diff --git a/packages/apidom-core/src/predicates/helpers.ts b/packages/apidom-core/src/predicates/helpers.ts index 58b470eb04..d0399b2359 100644 --- a/packages/apidom-core/src/predicates/helpers.ts +++ b/packages/apidom-core/src/predicates/helpers.ts @@ -1,16 +1,4 @@ -const hasMethod = (name: string, obj: Record): boolean => - typeof obj?.[name] === 'function'; - -const hasBasicElementProps = (element: any) => - element != null && - Object.prototype.hasOwnProperty.call(element, '_storedElement') && - Object.prototype.hasOwnProperty.call(element, '_content'); - -const primitiveEq = (val: unknown, obj: any): boolean => obj?.primitive?.() === val; - -const hasClass = (cls: string, obj: any): boolean => obj?.classes?.includes?.(cls) || false; - -export const isElementType = (name: string, element: any): boolean => element?.element === name; +import { Element, ArrayElement } from 'minim'; interface PredicateHelpers { hasMethod: typeof hasMethod; @@ -20,10 +8,70 @@ interface PredicateHelpers { hasClass: typeof hasClass; } -type PredicateCreator = (helpers: PredicateHelpers) => (element: any) => boolean; +type BasicElementShape = { + _storedElement: string; + _content: unknown; +}; + +type PrimitiveShape = { + primitive: () => unknown; +}; + +type ElementShape = { + element: T; +}; + +type ElementClassesShape = { + classes: ArrayElement | Array; +}; + +type PredicateCreator = (helpers: PredicateHelpers) => ElementPredicate; + +export type ElementPredicate = (element: unknown) => element is T; + +const hasMethod = ( + name: T, + element: unknown, +): element is { [key in T]: (...args: unknown[]) => unknown } => { + return ( + typeof element === 'object' && + element !== null && + name in element && + typeof (element as Record)[name] === 'function' + ); +}; + +const hasBasicElementProps = (element: unknown): element is BasicElementShape => + typeof element === 'object' && + element != null && + '_storedElement' in element && + typeof element._storedElement === 'string' && // eslint-disable-line no-underscore-dangle + '_content' in element; + +const primitiveEq = (val: unknown, element: unknown): element is PrimitiveShape => { + if (typeof element === 'object' && element !== null && 'primitive' in element) { + return typeof element.primitive === 'function' && element.primitive() === val; + } + return false; +}; + +const hasClass = (cls: string, element: unknown): element is ElementClassesShape => { + return ( + typeof element === 'object' && + element !== null && + 'classes' in element && + (Array.isArray(element.classes) || element.classes instanceof ArrayElement) && + element.classes.includes(cls) + ); +}; + +export const isElementType = (name: string, element: unknown): element is ElementShape => + typeof element === 'object' && + element !== null && + 'element' in element && + element.element === name; -const createPredicate = (predicateCreator: PredicateCreator) => { - // @ts-ignore +const createPredicate = (predicateCreator: PredicateCreator) => { return predicateCreator({ hasMethod, hasBasicElementProps, diff --git a/packages/apidom-core/src/predicates/index.ts b/packages/apidom-core/src/predicates/index.ts index 70d6cbfb04..cc8cc78e61 100644 --- a/packages/apidom-core/src/predicates/index.ts +++ b/packages/apidom-core/src/predicates/index.ts @@ -18,40 +18,41 @@ import CommentElement from '../elements/Comment'; import ParserResultElement from '../elements/ParseResult'; import SourceMapElement from '../elements/SourceMap'; import createPredicate, { isElementType as isElementTypeHelper } from './helpers'; +import type { ElementPredicate } from './helpers'; export const isElement = createPredicate(({ hasBasicElementProps, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is Element => element instanceof Element || (hasBasicElementProps(element) && primitiveEq(undefined, element)); }); export const isStringElement = createPredicate(({ hasBasicElementProps, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is StringElement => element instanceof StringElement || (hasBasicElementProps(element) && primitiveEq('string', element)); }); export const isNumberElement = createPredicate(({ hasBasicElementProps, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is NumberElement => element instanceof NumberElement || (hasBasicElementProps(element) && primitiveEq('number', element)); }); export const isNullElement = createPredicate(({ hasBasicElementProps, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is NullElement => element instanceof NullElement || (hasBasicElementProps(element) && primitiveEq('null', element)); }); export const isBooleanElement = createPredicate(({ hasBasicElementProps, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is BooleanElement => element instanceof BooleanElement || (hasBasicElementProps(element) && primitiveEq('boolean', element)); }); export const isObjectElement = createPredicate( ({ hasBasicElementProps, primitiveEq, hasMethod }) => { - return (element: any) => + return (element: unknown): element is ObjectElement => element instanceof ObjectElement || (hasBasicElementProps(element) && primitiveEq('object', element) && @@ -63,7 +64,7 @@ export const isObjectElement = createPredicate( export const isArrayElement = createPredicate( ({ hasBasicElementProps, primitiveEq, hasMethod }) => { - return (element: any) => + return (element: unknown): element is ArrayElement => (element instanceof ArrayElement && !(element instanceof ObjectElement)) || (hasBasicElementProps(element) && primitiveEq('array', element) && @@ -76,7 +77,7 @@ export const isArrayElement = createPredicate( export const isMemberElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is MemberElement => element instanceof MemberElement || (hasBasicElementProps(element) && isElementType('member', element) && @@ -86,7 +87,7 @@ export const isMemberElement = createPredicate( export const isLinkElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkElement => element instanceof LinkElement || (hasBasicElementProps(element) && isElementType('link', element) && @@ -96,7 +97,7 @@ export const isLinkElement = createPredicate( export const isRefElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is RefElement => element instanceof RefElement || (hasBasicElementProps(element) && isElementType('ref', element) && @@ -106,7 +107,7 @@ export const isRefElement = createPredicate( export const isAnnotationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is AnnotationElement => element instanceof AnnotationElement || (hasBasicElementProps(element) && isElementType('annotation', element) && @@ -116,7 +117,7 @@ export const isAnnotationElement = createPredicate( export const isCommentElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is CommentElement => element instanceof CommentElement || (hasBasicElementProps(element) && isElementType('comment', element) && @@ -126,7 +127,7 @@ export const isCommentElement = createPredicate( export const isParseResultElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ParserResultElement => element instanceof ParserResultElement || (hasBasicElementProps(element) && isElementType('parseResult', element) && @@ -136,7 +137,7 @@ export const isParseResultElement = createPredicate( export const isSourceMapElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SourceMapElement => element instanceof SourceMapElement || (hasBasicElementProps(element) && isElementType('sourceMap', element) && @@ -144,7 +145,18 @@ export const isSourceMapElement = createPredicate( }, ); -export const isPrimitiveElement = (element: any): boolean => { +type PrimitiveElement = + | ObjectElement + | ArrayElement + | BooleanElement + | NumberElement + | StringElement + | NullElement + | MemberElement; + +export const isPrimitiveElement: ElementPredicate = ( + element: unknown, +): element is PrimitiveElement => { return ( isElementTypeHelper('object', element) || isElementTypeHelper('array', element) || @@ -156,8 +168,8 @@ export const isPrimitiveElement = (element: any): boolean => { ); }; -export const hasElementSourceMap = (element: any): boolean => { - return isSourceMapElement(element?.meta?.get?.('sourceMap')); +export const hasElementSourceMap = (element: T): boolean => { + return isSourceMapElement(element.meta.get('sourceMap')); }; export const includesSymbols = (symbols: string[], element: T): boolean => { diff --git a/packages/apidom-core/test/predicates/index.ts b/packages/apidom-core/test/predicates/index.ts index 84cfcda2d7..0e216cc55d 100644 --- a/packages/apidom-core/test/predicates/index.ts +++ b/packages/apidom-core/test/predicates/index.ts @@ -63,7 +63,7 @@ describe('predicates', function () { specify('should support duck-typing', function () { const elementDuck = { - _storedElement: undefined, + _storedElement: 'element', _content: undefined, primitive() { return undefined; diff --git a/packages/apidom-core/test/traversal/filter.ts b/packages/apidom-core/test/traversal/filter.ts index 1e768a642f..40361eae48 100644 --- a/packages/apidom-core/test/traversal/filter.ts +++ b/packages/apidom-core/test/traversal/filter.ts @@ -1,6 +1,13 @@ import { assert } from 'chai'; -import { filter, createNamespace, isMemberElement, ArraySlice, ObjectElement } from '../../src'; +import { + filter, + createNamespace, + isMemberElement, + isElement, + ArraySlice, + ObjectElement, +} from '../../src'; const namespace = createNamespace(); @@ -17,8 +24,8 @@ describe('traversal', function () { }); specify('should find content matching the predicate', function () { - const predicate = (element: any): boolean => - isMemberElement(element) && element.key.equals('a'); + const predicate = (element: unknown): boolean => + isMemberElement(element) && isElement(element.key) && element.key.equals('a'); const filtered = filter(predicate, objElement); assert.lengthOf(filtered, 1); diff --git a/packages/apidom-core/test/traversal/find.ts b/packages/apidom-core/test/traversal/find.ts index 65b6ee4626..6e1cbe029e 100644 --- a/packages/apidom-core/test/traversal/find.ts +++ b/packages/apidom-core/test/traversal/find.ts @@ -1,7 +1,7 @@ import { assert } from 'chai'; import { F as stubFalse } from 'ramda'; -import { createNamespace, find, isMemberElement, MemberElement } from '../../src'; +import { createNamespace, find, isMemberElement, isElement, MemberElement } from '../../src'; const namespace = createNamespace(); @@ -12,8 +12,8 @@ describe('traversal', function () { const objElement = new namespace.elements.Object({ a: 'b', c: 'd' }); specify('should return first match', function () { - const predicate = (element: any): boolean => - isMemberElement(element) && element.key.equals('c'); + const predicate = (element: unknown): boolean => + isMemberElement(element) && isElement(element.key) && element.key.equals('c'); // @ts-ignore const found = find(predicate, objElement) as MemberElement; diff --git a/packages/apidom-ns-api-design-systems/src/predicates.ts b/packages/apidom-ns-api-design-systems/src/predicates.ts index f430378ee1..334f830815 100644 --- a/packages/apidom-ns-api-design-systems/src/predicates.ts +++ b/packages/apidom-ns-api-design-systems/src/predicates.ts @@ -11,7 +11,7 @@ import StandardIdentifierElement from './elements/StandardIdentifier'; export const isMainElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is MainElement => element instanceof MainElement || (hasBasicElementProps(element) && isElementType('main', element) && @@ -21,7 +21,7 @@ export const isMainElement = createPredicate( export const isInfoElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is InfoElement => element instanceof InfoElement || (hasBasicElementProps(element) && isElementType('info', element) && @@ -31,7 +31,7 @@ export const isInfoElement = createPredicate( export const isPrincipleElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is PrincipleElement => element instanceof PrincipleElement || (hasBasicElementProps(element) && isElementType('principle', element) && @@ -41,7 +41,7 @@ export const isPrincipleElement = createPredicate( export const isRequirementElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is RequirementElement => element instanceof RequirementElement || (hasBasicElementProps(element) && isElementType('requirement', element) && @@ -51,7 +51,7 @@ export const isRequirementElement = createPredicate( export const isRequirementLevelElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is RequirementLevelElement => element instanceof RequirementLevelElement || (hasBasicElementProps(element) && isElementType('requirementLevel', element) && @@ -61,7 +61,7 @@ export const isRequirementLevelElement = createPredicate( export const isScenarioElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ScenarioElement => element instanceof ScenarioElement || (hasBasicElementProps(element) && isElementType('scenario', element) && @@ -71,7 +71,7 @@ export const isScenarioElement = createPredicate( export const isStandardElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is StandardElement => element instanceof StandardElement || (hasBasicElementProps(element) && isElementType('standard', element) && @@ -81,7 +81,7 @@ export const isStandardElement = createPredicate( export const isStandardIdentifierElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is StandardIdentifierElement => element instanceof StandardIdentifierElement || (hasBasicElementProps(element) && isElementType('standardIdentifier', element) && diff --git a/packages/apidom-ns-asyncapi-2/src/predicates.ts b/packages/apidom-ns-asyncapi-2/src/predicates.ts index d326bc820d..a1a126f2de 100644 --- a/packages/apidom-ns-asyncapi-2/src/predicates.ts +++ b/packages/apidom-ns-asyncapi-2/src/predicates.ts @@ -1,9 +1,11 @@ import { + BooleanElement, createPredicate, isBooleanElement, isStringElement, toValue, } from '@swagger-api/apidom-core'; +import type { ElementPredicate } from '@swagger-api/apidom-core'; import AsyncApi2Element from './elements/AsyncApi2'; import AsyncApiVersionElement from './elements/AsyncApiVersion'; @@ -28,7 +30,7 @@ import ServerVariableElement from './elements/ServerVariable'; export const isAsyncApi2Element = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq, hasClass }) => { - return (element: any) => + return (element: unknown): element is AsyncApi2Element => element instanceof AsyncApi2Element || (hasBasicElementProps(element) && isElementType('asyncApi2', element) && @@ -39,7 +41,7 @@ export const isAsyncApi2Element = createPredicate( export const isAsyncApiVersionElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is AsyncApiVersionElement => element instanceof AsyncApiVersionElement || (hasBasicElementProps(element) && isElementType('asyncApiVersion', element) && @@ -49,7 +51,7 @@ export const isAsyncApiVersionElement = createPredicate( export const isChannelBindingsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ChannelBindingsElement => element instanceof ChannelBindingsElement || (hasBasicElementProps(element) && isElementType('channelBindings', element) && @@ -59,7 +61,7 @@ export const isChannelBindingsElement = createPredicate( export const isChannelItemElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ChannelItemElement => element instanceof ChannelItemElement || (hasBasicElementProps(element) && isElementType('channelItem', element) && @@ -67,7 +69,9 @@ export const isChannelItemElement = createPredicate( }, ); -export const isChannelItemElementExternal = (element: any): element is ChannelItemElement => { +export const isChannelItemElementExternal: ElementPredicate = ( + element: unknown, +): element is ChannelItemElement => { if (!isChannelItemElement(element)) { return false; } @@ -82,7 +86,7 @@ export const isChannelItemElementExternal = (element: any): element is ChannelIt export const isChannelsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ChannelsElement => element instanceof ChannelsElement || (hasBasicElementProps(element) && isElementType('channels', element) && @@ -92,7 +96,7 @@ export const isChannelsElement = createPredicate( export const isComponentsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ComponentsElement => element instanceof ComponentsElement || (hasBasicElementProps(element) && isElementType('components', element) && @@ -102,7 +106,7 @@ export const isComponentsElement = createPredicate( export const isContactElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ContactElement => element instanceof ContactElement || (hasBasicElementProps(element) && isElementType('contact', element) && @@ -112,7 +116,7 @@ export const isContactElement = createPredicate( export const isIdentifierElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is IdentifierElement => element instanceof IdentifierElement || (hasBasicElementProps(element) && isElementType('identifier', element) && @@ -122,7 +126,7 @@ export const isIdentifierElement = createPredicate( export const isInfoElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is InfoElement => element instanceof InfoElement || (hasBasicElementProps(element) && isElementType('info', element) && @@ -132,7 +136,7 @@ export const isInfoElement = createPredicate( export const isLicenseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LicenseElement => element instanceof LicenseElement || (hasBasicElementProps(element) && isElementType('license', element) && @@ -142,7 +146,7 @@ export const isLicenseElement = createPredicate( export const isOperationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is OperationElement => element instanceof OperationElement || (hasBasicElementProps(element) && isElementType('operation', element) && @@ -152,7 +156,7 @@ export const isOperationElement = createPredicate( export const isParameterElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ParameterElement => element instanceof ParameterElement || (hasBasicElementProps(element) && isElementType('parameter', element) && @@ -162,7 +166,7 @@ export const isParameterElement = createPredicate( export const isParametersElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ParametersElement => element instanceof ParametersElement || (hasBasicElementProps(element) && isElementType('parameters', element) && @@ -172,7 +176,7 @@ export const isParametersElement = createPredicate( export const isReferenceElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ReferenceElement => element instanceof ReferenceElement || (hasBasicElementProps(element) && isElementType('reference', element) && @@ -180,7 +184,9 @@ export const isReferenceElement = createPredicate( }, ); -export const isReferenceElementExternal = (element: any): element is ReferenceElement => { +export const isReferenceElementExternal: ElementPredicate = ( + element: unknown, +): element is ReferenceElement => { if (!isReferenceElement(element)) { return false; } @@ -192,7 +198,7 @@ export const isReferenceElementExternal = (element: any): element is ReferenceEl export const isSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SchemaElement => element instanceof SchemaElement || (hasBasicElementProps(element) && isElementType('schema', element) && @@ -200,13 +206,15 @@ export const isSchemaElement = createPredicate( }, ); -export const isBooleanJsonSchemaElement = (element: any) => { +export const isBooleanJsonSchemaElement: ElementPredicate = ( + element: unknown, +): element is BooleanElement => { return isBooleanElement(element) && element.classes.includes('boolean-json-schema'); }; export const isSecurityRequirementElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SecurityRequirementElement => element instanceof SecurityRequirementElement || (hasBasicElementProps(element) && isElementType('securityRequirement', element) && @@ -216,7 +224,7 @@ export const isSecurityRequirementElement = createPredicate( export const isServerElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerElement => element instanceof ServerElement || (hasBasicElementProps(element) && isElementType('server', element) && @@ -226,7 +234,7 @@ export const isServerElement = createPredicate( export const isServerBindingsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerBindingsElement => element instanceof ServerBindingsElement || (hasBasicElementProps(element) && isElementType('serverBindings', element) && @@ -236,7 +244,7 @@ export const isServerBindingsElement = createPredicate( export const isServersElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServersElement => element instanceof ServersElement || (hasBasicElementProps(element) && isElementType('servers', element) && @@ -246,7 +254,7 @@ export const isServersElement = createPredicate( export const isServerVariableElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerVariableElement => element instanceof ServerVariableElement || (hasBasicElementProps(element) && isElementType('serverVariable', element) && diff --git a/packages/apidom-ns-json-schema-draft-4/src/predicates.ts b/packages/apidom-ns-json-schema-draft-4/src/predicates.ts index c4fc9f44df..9f87994b26 100644 --- a/packages/apidom-ns-json-schema-draft-4/src/predicates.ts +++ b/packages/apidom-ns-json-schema-draft-4/src/predicates.ts @@ -7,7 +7,7 @@ import LinkDescriptionElement from './elements/LinkDescription'; export const isJSONSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is JSONSchemaElement => element instanceof JSONSchemaElement || (hasBasicElementProps(element) && isElementType('JSONSchemaDraft4', element) && @@ -17,7 +17,7 @@ export const isJSONSchemaElement = createPredicate( export const isJSONReferenceElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is JSONReferenceElement => element instanceof JSONReferenceElement || (hasBasicElementProps(element) && isElementType('JSONReference', element) && @@ -27,7 +27,7 @@ export const isJSONReferenceElement = createPredicate( export const isMediaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is MediaElement => element instanceof MediaElement || (hasBasicElementProps(element) && isElementType('media', element) && @@ -37,7 +37,7 @@ export const isMediaElement = createPredicate( export const isLinkDescriptionElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkDescriptionElement => element instanceof LinkDescriptionElement || (hasBasicElementProps(element) && isElementType('linkDescription', element) && diff --git a/packages/apidom-ns-json-schema-draft-6/src/predicates.ts b/packages/apidom-ns-json-schema-draft-6/src/predicates.ts index ea873e6ae8..6faabb6a9b 100644 --- a/packages/apidom-ns-json-schema-draft-6/src/predicates.ts +++ b/packages/apidom-ns-json-schema-draft-6/src/predicates.ts @@ -7,7 +7,7 @@ export { isJSONReferenceElement, isMediaElement } from '@swagger-api/apidom-ns-j export const isJSONSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is JSONSchemaElement => element instanceof JSONSchemaElement || (hasBasicElementProps(element) && isElementType('JSONSchemaDraft6', element) && @@ -17,7 +17,7 @@ export const isJSONSchemaElement = createPredicate( export const isLinkDescriptionElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkDescriptionElement => element instanceof LinkDescriptionElement || (hasBasicElementProps(element) && isElementType('linkDescription', element) && diff --git a/packages/apidom-ns-json-schema-draft-7/src/predicates.ts b/packages/apidom-ns-json-schema-draft-7/src/predicates.ts index f251b28764..d7b2465934 100644 --- a/packages/apidom-ns-json-schema-draft-7/src/predicates.ts +++ b/packages/apidom-ns-json-schema-draft-7/src/predicates.ts @@ -7,7 +7,7 @@ export { isJSONReferenceElement } from '@swagger-api/apidom-ns-json-schema-draft export const isJSONSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is JSONSchemaElement => element instanceof JSONSchemaElement || (hasBasicElementProps(element) && isElementType('JSONSchemaDraft7', element) && @@ -17,7 +17,7 @@ export const isJSONSchemaElement = createPredicate( export const isLinkDescriptionElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkDescriptionElement => element instanceof LinkDescriptionElement || (hasBasicElementProps(element) && isElementType('linkDescription', element) && diff --git a/packages/apidom-ns-openapi-2/README.md b/packages/apidom-ns-openapi-2/README.md index b6dc0b70bf..59cdb97f82 100644 --- a/packages/apidom-ns-openapi-2/README.md +++ b/packages/apidom-ns-openapi-2/README.md @@ -201,6 +201,7 @@ Only fully implemented specification objects should be checked here. - [x] [Header Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-header-object) - [x] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-tag-object) - [x] [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-reference-object) +- [x] [JSON Reference Object](https://datatracker.ietf.org/doc/html/draft-pbryan-zyp-json-ref-02) - [x] [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-schema-object) - [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-xml-object) - [ ] [Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-definitions-object) diff --git a/packages/apidom-ns-openapi-2/src/predicates.ts b/packages/apidom-ns-openapi-2/src/predicates.ts index c541178850..8e0f330926 100644 --- a/packages/apidom-ns-openapi-2/src/predicates.ts +++ b/packages/apidom-ns-openapi-2/src/predicates.ts @@ -3,7 +3,7 @@ import { createPredicate } from '@swagger-api/apidom-core'; import InfoElement from './elements/Info'; import LicenseElement from './elements/License'; import ContactElement from './elements/Contact'; -import ExternalDocumentation from './elements/ExternalDocumentation'; +import ExternalDocumentationElement from './elements/ExternalDocumentation'; import ParameterElement from './elements/Parameter'; import ItemsElement from './elements/Items'; import ExampleElement from './elements/Example'; @@ -20,7 +20,7 @@ import ScopesElement from './elements/Scopes'; export const isInfoElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is InfoElement => element instanceof InfoElement || (hasBasicElementProps(element) && isElementType('info', element) && @@ -30,7 +30,7 @@ export const isInfoElement = createPredicate( export const isLicenseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is LicenseElement => element instanceof LicenseElement || (hasBasicElementProps(element) && isElementType('license', element) && @@ -40,7 +40,7 @@ export const isLicenseElement = createPredicate( export const isContactElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ContactElement => element instanceof ContactElement || (hasBasicElementProps(element) && isElementType('contact', element) && @@ -49,8 +49,8 @@ export const isContactElement = createPredicate( ); export const isExternalDocumentationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => - element instanceof ExternalDocumentation || + return (element: unknown): element is ExternalDocumentationElement => + element instanceof ExternalDocumentationElement || (hasBasicElementProps(element) && isElementType('externalDocumentation', element) && primitiveEq('object', element)); @@ -59,7 +59,7 @@ export const isExternalDocumentationElement = createPredicate( export const isParameterElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ParameterElement => element instanceof ParameterElement || (hasBasicElementProps(element) && isElementType('parameter', element) && @@ -69,7 +69,7 @@ export const isParameterElement = createPredicate( export const isItemsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ItemsElement => element instanceof ItemsElement || (hasBasicElementProps(element) && isElementType('items', element) && @@ -79,7 +79,7 @@ export const isItemsElement = createPredicate( export const isHeadersElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is HeadersElement => element instanceof HeadersElement || (hasBasicElementProps(element) && isElementType('headers', element) && @@ -89,7 +89,7 @@ export const isHeadersElement = createPredicate( export const isExampleElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ExampleElement => element instanceof ExampleElement || (hasBasicElementProps(element) && isElementType('example', element) && @@ -99,7 +99,7 @@ export const isExampleElement = createPredicate( export const isHeaderElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is HeaderElement => element instanceof HeaderElement || (hasBasicElementProps(element) && isElementType('header', element) && @@ -109,7 +109,7 @@ export const isHeaderElement = createPredicate( export const isTagElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is TagElement => element instanceof TagElement || (hasBasicElementProps(element) && isElementType('tag', element) && @@ -119,7 +119,7 @@ export const isTagElement = createPredicate( export const isReferenceElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ReferenceElement => element instanceof ReferenceElement || (hasBasicElementProps(element) && isElementType('reference', element) && @@ -129,7 +129,7 @@ export const isReferenceElement = createPredicate( export const isSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is SchemaElement => element instanceof SchemaElement || (hasBasicElementProps(element) && isElementType('schema', element) && @@ -139,7 +139,7 @@ export const isSchemaElement = createPredicate( export const isXmlElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is XmlElement => element instanceof XmlElement || (hasBasicElementProps(element) && isElementType('xml', element) && @@ -149,7 +149,7 @@ export const isXmlElement = createPredicate( export const isSecurityDefinitionsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is SecurityDefinitionsElement => element instanceof SecurityDefinitionsElement || (hasBasicElementProps(element) && isElementType('securityDefinitions', element) && @@ -159,7 +159,7 @@ export const isSecurityDefinitionsElement = createPredicate( export const isSecuritySchemeElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is SecuritySchemeElement => element instanceof SecuritySchemeElement || (hasBasicElementProps(element) && isElementType('securityScheme', element) && @@ -169,7 +169,7 @@ export const isSecuritySchemeElement = createPredicate( export const isScopesElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is ScopesElement => element instanceof ScopesElement || (hasBasicElementProps(element) && isElementType('scopes', element) && @@ -179,7 +179,7 @@ export const isScopesElement = createPredicate( export const isSecurityRequirementElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: unknown) => + return (element: unknown): element is SecurityRequirementElement => element instanceof SecurityRequirementElement || (hasBasicElementProps(element) && isElementType('securityRequirement', element) && diff --git a/packages/apidom-ns-openapi-3-0/src/predicates.ts b/packages/apidom-ns-openapi-3-0/src/predicates.ts index 875485f847..b899a52b83 100644 --- a/packages/apidom-ns-openapi-3-0/src/predicates.ts +++ b/packages/apidom-ns-openapi-3-0/src/predicates.ts @@ -1,9 +1,11 @@ import { + BooleanElement, createPredicate, isBooleanElement, isStringElement, toValue, } from '@swagger-api/apidom-core'; +import type { ElementPredicate } from '@swagger-api/apidom-core'; import CallbackElement from './elements/Callback'; import ComponentsElement from './elements/Components'; @@ -32,7 +34,7 @@ import MediaTypeElement from './elements/MediaType'; export const isCallbackElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is CallbackElement => element instanceof CallbackElement || (hasBasicElementProps(element) && isElementType('callback', element) && @@ -42,7 +44,7 @@ export const isCallbackElement = createPredicate( export const isComponentsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ComponentsElement => element instanceof ComponentsElement || (hasBasicElementProps(element) && isElementType('components', element) && @@ -52,7 +54,7 @@ export const isComponentsElement = createPredicate( export const isContactElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ContactElement => element instanceof ContactElement || (hasBasicElementProps(element) && isElementType('contact', element) && @@ -62,7 +64,7 @@ export const isContactElement = createPredicate( export const isExampleElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ExampleElement => element instanceof ExampleElement || (hasBasicElementProps(element) && isElementType('example', element) && @@ -72,7 +74,7 @@ export const isExampleElement = createPredicate( export const isExternalDocumentationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ExternalDocumentationElement => element instanceof ExternalDocumentationElement || (hasBasicElementProps(element) && isElementType('externalDocumentation', element) && @@ -82,7 +84,7 @@ export const isExternalDocumentationElement = createPredicate( export const isHeaderElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is HeaderElement => element instanceof HeaderElement || (hasBasicElementProps(element) && isElementType('header', element) && @@ -92,7 +94,7 @@ export const isHeaderElement = createPredicate( export const isInfoElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is InfoElement => element instanceof InfoElement || (hasBasicElementProps(element) && isElementType('info', element) && @@ -102,7 +104,7 @@ export const isInfoElement = createPredicate( export const isLicenseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LicenseElement => element instanceof LicenseElement || (hasBasicElementProps(element) && isElementType('license', element) && @@ -112,7 +114,7 @@ export const isLicenseElement = createPredicate( export const isLinkElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkElement => element instanceof LinkElement || (hasBasicElementProps(element) && isElementType('link', element) && @@ -120,7 +122,9 @@ export const isLinkElement = createPredicate( }, ); -export const isLinkElementExternal = (element: any): element is LinkElement => { +export const isLinkElementExternal: ElementPredicate = ( + element: unknown, +): element is LinkElement => { if (!isLinkElement(element)) { return false; } @@ -135,7 +139,7 @@ export const isLinkElementExternal = (element: any): element is LinkElement => { export const isOpenapiElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is OpenapiElement => element instanceof OpenapiElement || (hasBasicElementProps(element) && isElementType('openapi', element) && @@ -146,7 +150,7 @@ export const isOpenapiElement = createPredicate( // eslint-disable-next-line @typescript-eslint/naming-convention export const isOpenApi3_0Element = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq, hasClass }) => { - return (element: any) => + return (element: unknown): element is OpenApi3_0Element => element instanceof OpenApi3_0Element || (hasBasicElementProps(element) && isElementType('openApi3_0', element) && @@ -157,7 +161,7 @@ export const isOpenApi3_0Element = createPredicate( export const isOperationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is OperationElement => element instanceof OperationElement || (hasBasicElementProps(element) && isElementType('operation', element) && @@ -167,7 +171,7 @@ export const isOperationElement = createPredicate( export const isParameterElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ParameterElement => element instanceof ParameterElement || (hasBasicElementProps(element) && isElementType('parameter', element) && @@ -177,7 +181,7 @@ export const isParameterElement = createPredicate( export const isPathItemElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is PathItemElement => element instanceof PathItemElement || (hasBasicElementProps(element) && isElementType('pathItem', element) && @@ -185,7 +189,9 @@ export const isPathItemElement = createPredicate( }, ); -export const isPathItemElementExternal = (element: any): element is PathItemElement => { +export const isPathItemElementExternal: ElementPredicate = ( + element: unknown, +): element is PathItemElement => { if (!isPathItemElement(element)) { return false; } @@ -200,7 +206,7 @@ export const isPathItemElementExternal = (element: any): element is PathItemElem export const isPathsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is PathsElement => element instanceof PathsElement || (hasBasicElementProps(element) && isElementType('paths', element) && @@ -210,7 +216,7 @@ export const isPathsElement = createPredicate( export const isReferenceElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ReferenceElement => element instanceof ReferenceElement || (hasBasicElementProps(element) && isElementType('reference', element) && @@ -218,7 +224,9 @@ export const isReferenceElement = createPredicate( }, ); -export const isReferenceElementExternal = (element: any): element is ReferenceElement => { +export const isReferenceElementExternal: ElementPredicate = ( + element: unknown, +): element is ReferenceElement => { if (!isReferenceElement(element)) { return false; } @@ -233,7 +241,7 @@ export const isReferenceElementExternal = (element: any): element is ReferenceEl export const isRequestBodyElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is RequestBodyElement => element instanceof RequestBodyElement || (hasBasicElementProps(element) && isElementType('requestBody', element) && @@ -243,7 +251,7 @@ export const isRequestBodyElement = createPredicate( export const isResponseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ResponseElement => element instanceof ResponseElement || (hasBasicElementProps(element) && isElementType('response', element) && @@ -253,7 +261,7 @@ export const isResponseElement = createPredicate( export const isResponsesElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ResponsesElement => element instanceof ResponsesElement || (hasBasicElementProps(element) && isElementType('responses', element) && @@ -263,7 +271,7 @@ export const isResponsesElement = createPredicate( export const isSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SchemaElement => element instanceof SchemaElement || (hasBasicElementProps(element) && isElementType('schema', element) && @@ -271,13 +279,15 @@ export const isSchemaElement = createPredicate( }, ); -export const isBooleanJsonSchemaElement = (element: any) => { +export const isBooleanJsonSchemaElement: ElementPredicate = ( + element: unknown, +): element is BooleanElement => { return isBooleanElement(element) && element.classes.includes('boolean-json-schema'); }; export const isSecurityRequirementElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SecurityRequirementElement => element instanceof SecurityRequirementElement || (hasBasicElementProps(element) && isElementType('securityRequirement', element) && @@ -287,7 +297,7 @@ export const isSecurityRequirementElement = createPredicate( export const isServerElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerElement => element instanceof ServerElement || (hasBasicElementProps(element) && isElementType('server', element) && @@ -297,7 +307,7 @@ export const isServerElement = createPredicate( export const isServerVariableElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerVariableElement => element instanceof ServerVariableElement || (hasBasicElementProps(element) && isElementType('serverVariable', element) && @@ -307,7 +317,7 @@ export const isServerVariableElement = createPredicate( export const isMediaTypeElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is MediaTypeElement => element instanceof MediaTypeElement || (hasBasicElementProps(element) && isElementType('mediaType', element) && diff --git a/packages/apidom-ns-openapi-3-1/src/predicates.ts b/packages/apidom-ns-openapi-3-1/src/predicates.ts index 751a523a78..432098f1af 100644 --- a/packages/apidom-ns-openapi-3-1/src/predicates.ts +++ b/packages/apidom-ns-openapi-3-1/src/predicates.ts @@ -1,9 +1,11 @@ import { + BooleanElement, createPredicate, isBooleanElement, isStringElement, toValue, } from '@swagger-api/apidom-core'; +import type { ElementPredicate } from '@swagger-api/apidom-core'; import CallbackElement from './elements/Callback'; import ComponentsElement from './elements/Components'; @@ -33,7 +35,7 @@ import MediaTypeElement from './elements/MediaType'; export const isCallbackElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is CallbackElement => element instanceof CallbackElement || (hasBasicElementProps(element) && isElementType('callback', element) && @@ -43,7 +45,7 @@ export const isCallbackElement = createPredicate( export const isComponentsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ComponentsElement => element instanceof ComponentsElement || (hasBasicElementProps(element) && isElementType('components', element) && @@ -53,7 +55,7 @@ export const isComponentsElement = createPredicate( export const isContactElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ContactElement => element instanceof ContactElement || (hasBasicElementProps(element) && isElementType('contact', element) && @@ -63,7 +65,7 @@ export const isContactElement = createPredicate( export const isExampleElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ExampleElement => element instanceof ExampleElement || (hasBasicElementProps(element) && isElementType('example', element) && @@ -73,7 +75,7 @@ export const isExampleElement = createPredicate( export const isExternalDocumentationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ExternalDocumentationElement => element instanceof ExternalDocumentationElement || (hasBasicElementProps(element) && isElementType('externalDocumentation', element) && @@ -83,7 +85,7 @@ export const isExternalDocumentationElement = createPredicate( export const isHeaderElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is HeaderElement => element instanceof HeaderElement || (hasBasicElementProps(element) && isElementType('header', element) && @@ -93,7 +95,7 @@ export const isHeaderElement = createPredicate( export const isInfoElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is InfoElement => element instanceof InfoElement || (hasBasicElementProps(element) && isElementType('info', element) && @@ -103,7 +105,7 @@ export const isInfoElement = createPredicate( export const isJsonSchemaDialectElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is JsonSchemaDialectElement => element instanceof JsonSchemaDialectElement || (hasBasicElementProps(element) && isElementType('jsonSchemaDialect', element) && @@ -113,7 +115,7 @@ export const isJsonSchemaDialectElement = createPredicate( export const isLicenseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LicenseElement => element instanceof LicenseElement || (hasBasicElementProps(element) && isElementType('license', element) && @@ -123,7 +125,7 @@ export const isLicenseElement = createPredicate( export const isLinkElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is LinkElement => element instanceof LinkElement || (hasBasicElementProps(element) && isElementType('link', element) && @@ -131,7 +133,9 @@ export const isLinkElement = createPredicate( }, ); -export const isLinkElementExternal = (element: any): element is LinkElement => { +export const isLinkElementExternal: ElementPredicate = ( + element: unknown, +): element is LinkElement => { if (!isLinkElement(element)) { return false; } @@ -146,7 +150,7 @@ export const isLinkElementExternal = (element: any): element is LinkElement => { export const isOpenapiElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is OpenapiElement => element instanceof OpenapiElement || (hasBasicElementProps(element) && isElementType('openapi', element) && @@ -157,7 +161,7 @@ export const isOpenapiElement = createPredicate( // eslint-disable-next-line @typescript-eslint/naming-convention export const isOpenApi3_1Element = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq, hasClass }) => { - return (element: any) => + return (element: unknown): element is OpenApi3_1Element => element instanceof OpenApi3_1Element || (hasBasicElementProps(element) && isElementType('openApi3_1', element) && @@ -168,7 +172,7 @@ export const isOpenApi3_1Element = createPredicate( export const isOperationElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is OperationElement => element instanceof OperationElement || (hasBasicElementProps(element) && isElementType('operation', element) && @@ -178,7 +182,7 @@ export const isOperationElement = createPredicate( export const isParameterElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ParameterElement => element instanceof ParameterElement || (hasBasicElementProps(element) && isElementType('parameter', element) && @@ -188,7 +192,7 @@ export const isParameterElement = createPredicate( export const isPathItemElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is PathItemElement => element instanceof PathItemElement || (hasBasicElementProps(element) && isElementType('pathItem', element) && @@ -196,7 +200,9 @@ export const isPathItemElement = createPredicate( }, ); -export const isPathItemElementExternal = (element: any): element is PathItemElement => { +export const isPathItemElementExternal: ElementPredicate = ( + element: unknown, +): element is PathItemElement => { if (!isPathItemElement(element)) { return false; } @@ -211,7 +217,7 @@ export const isPathItemElementExternal = (element: any): element is PathItemElem export const isPathsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is PathsElement => element instanceof PathsElement || (hasBasicElementProps(element) && isElementType('paths', element) && @@ -221,7 +227,7 @@ export const isPathsElement = createPredicate( export const isReferenceElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ReferenceElement => element instanceof ReferenceElement || (hasBasicElementProps(element) && isElementType('reference', element) && @@ -229,7 +235,9 @@ export const isReferenceElement = createPredicate( }, ); -export const isReferenceElementExternal = (element: any): element is ReferenceElement => { +export const isReferenceElementExternal: ElementPredicate = ( + element: unknown, +): element is ReferenceElement => { if (!isReferenceElement(element)) { return false; } @@ -244,7 +252,7 @@ export const isReferenceElementExternal = (element: any): element is ReferenceEl export const isRequestBodyElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is RequestBodyElement => element instanceof RequestBodyElement || (hasBasicElementProps(element) && isElementType('requestBody', element) && @@ -254,7 +262,7 @@ export const isRequestBodyElement = createPredicate( export const isResponseElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ResponseElement => element instanceof ResponseElement || (hasBasicElementProps(element) && isElementType('response', element) && @@ -264,7 +272,7 @@ export const isResponseElement = createPredicate( export const isResponsesElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ResponsesElement => element instanceof ResponsesElement || (hasBasicElementProps(element) && isElementType('responses', element) && @@ -274,7 +282,7 @@ export const isResponsesElement = createPredicate( export const isSchemaElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SchemaElement => element instanceof SchemaElement || (hasBasicElementProps(element) && isElementType('schema', element) && @@ -282,13 +290,15 @@ export const isSchemaElement = createPredicate( }, ); -export const isBooleanJsonSchemaElement = (element: any) => { +export const isBooleanJsonSchemaElement: ElementPredicate = ( + element: unknown, +): element is BooleanElement => { return isBooleanElement(element) && element.classes.includes('boolean-json-schema'); }; export const isSecurityRequirementElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is SecurityRequirementElement => element instanceof SecurityRequirementElement || (hasBasicElementProps(element) && isElementType('securityRequirement', element) && @@ -298,7 +308,7 @@ export const isSecurityRequirementElement = createPredicate( export const isServerElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerElement => element instanceof ServerElement || (hasBasicElementProps(element) && isElementType('server', element) && @@ -308,7 +318,7 @@ export const isServerElement = createPredicate( export const isServerVariableElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is ServerVariableElement => element instanceof ServerVariableElement || (hasBasicElementProps(element) && isElementType('serverVariable', element) && @@ -318,7 +328,7 @@ export const isServerVariableElement = createPredicate( export const isMediaTypeElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { - return (element: any) => + return (element: unknown): element is MediaTypeElement => element instanceof MediaTypeElement || (hasBasicElementProps(element) && isElementType('mediaType', element) && diff --git a/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$id.ts b/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$id.ts index 824c644738..21167e685a 100644 --- a/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$id.ts +++ b/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$id.ts @@ -1,5 +1,5 @@ import { assert } from 'chai'; -import { ObjectElement, find, toValue } from '@swagger-api/apidom-core'; +import { ObjectElement, find, toValue, isElement } from '@swagger-api/apidom-core'; import { isSchemaElement, OpenApi3_1Element } from '../../../../src'; @@ -49,7 +49,7 @@ describe('refractor', function () { }); const openApiElement = OpenApi3_1Element.refract(genericObjectElement); const schemaElement = find( - (e) => isSchemaElement(e) && e.$anchor && e.$anchor.equals('1'), + (e) => isSchemaElement(e) && isElement(e.$anchor) && e.$anchor.equals('1'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$id')); @@ -91,7 +91,7 @@ describe('refractor', function () { specify('should annotate Schema Object($anchor=1) with inherited$id', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$anchor.equals('1'), + (e) => isSchemaElement(e) && isElement(e.$anchor) && e.$anchor.equals('1'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$id')); @@ -101,7 +101,7 @@ describe('refractor', function () { specify('should annotate Schema Object($anchor=2) with inherited$id', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$anchor.equals('2'), + (e) => isSchemaElement(e) && isElement(e.$anchor) && e.$anchor.equals('2'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$id')); @@ -111,7 +111,7 @@ describe('refractor', function () { specify('should annotate Schema Object($anchor=3) with inherited$id', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$anchor.equals('3'), + (e) => isSchemaElement(e) && isElement(e.$anchor) && e.$anchor.equals('3'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$id')); @@ -121,7 +121,7 @@ describe('refractor', function () { specify('should not annotate Schema Object($anchor=4) with inherited$id', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$anchor.equals('4'), + (e) => isSchemaElement(e) && isElement(e.$anchor) && e.$anchor.equals('4'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$id')); diff --git a/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$schema.ts b/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$schema.ts index 862d7807cd..e07785c512 100644 --- a/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$schema.ts +++ b/packages/apidom-ns-openapi-3-1/test/refractor/elements/Schema/embedded-resources-$schema.ts @@ -1,5 +1,5 @@ import { assert } from 'chai'; -import { ObjectElement, find, toValue } from '@swagger-api/apidom-core'; +import { ObjectElement, find, toValue, isElement } from '@swagger-api/apidom-core'; import { parse } from '@swagger-api/apidom-parser-adapter-json'; import { @@ -135,7 +135,7 @@ describe('refractor', function () { specify('should annotate Schema Object($id=1) with appropriate dialect', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$id.equals('1'), + (e) => isSchemaElement(e) && isElement(e.$id) && e.$id.equals('1'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$schema')); @@ -146,7 +146,7 @@ describe('refractor', function () { specify('should not annotate Schema Object($id=2) with any dialect', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$id.equals('2'), + (e) => isSchemaElement(e) && isElement(e.$id) && e.$id.equals('2'), openApiElement, ); // @ts-ignore @@ -159,7 +159,7 @@ describe('refractor', function () { specify('should annotate Schema Object($id=3) with appropriate dialect', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$id.equals('3'), + (e) => isSchemaElement(e) && isElement(e.$id) && e.$id.equals('3'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$schema')); @@ -170,7 +170,7 @@ describe('refractor', function () { specify('should annotate Schema Object($id=4) with appropriate dialect', function () { const schemaElement = find( - (e) => isSchemaElement(e) && e.$id.equals('4'), + (e) => isSchemaElement(e) && isElement(e.$id) && e.$id.equals('4'), openApiElement, ); const actual = toValue(schemaElement?.meta.get('inherited$schema')); diff --git a/packages/apidom-reference/src/dereference/strategies/openapi-3-0/visitor.ts b/packages/apidom-reference/src/dereference/strategies/openapi-3-0/visitor.ts index 4c2109bbe9..2f2b637c50 100644 --- a/packages/apidom-reference/src/dereference/strategies/openapi-3-0/visitor.ts +++ b/packages/apidom-reference/src/dereference/strategies/openapi-3-0/visitor.ts @@ -368,7 +368,8 @@ const OpenApi3_0DereferenceVisitor = stampit({ const operationId = toValue(linkElement.operationId); const reference = await this.toReference(url.unsanitize(this.reference.uri)); operationElement = find( - (e) => isOperationElement(e) && e.operationId.equals(operationId), + (e) => + isOperationElement(e) && isElement(e.operationId) && e.operationId.equals(operationId), reference.value.result, ); // OperationElement not found by its operationId diff --git a/packages/apidom-reference/src/dereference/strategies/openapi-3-1/visitor.ts b/packages/apidom-reference/src/dereference/strategies/openapi-3-1/visitor.ts index 22194efdea..918f7c5af2 100644 --- a/packages/apidom-reference/src/dereference/strategies/openapi-3-1/visitor.ts +++ b/packages/apidom-reference/src/dereference/strategies/openapi-3-1/visitor.ts @@ -400,7 +400,8 @@ const OpenApi3_1DereferenceVisitor = stampit({ const operationId = toValue(linkElement.operationId); const reference = await this.toReference(url.unsanitize(this.reference.uri)); operationElement = find( - (e) => isOperationElement(e) && e.operationId.equals(operationId), + (e) => + isOperationElement(e) && isElement(e.operationId) && e.operationId.equals(operationId), reference.value.result, ); // OperationElement not found by its operationId From f604477debe9290f59a8c5ac48b571c205971ff2 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Wed, 18 Oct 2023 10:00:54 +0200 Subject: [PATCH 2/2] style: naming --- .../apidom-core/src/predicates/helpers.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/apidom-core/src/predicates/helpers.ts b/packages/apidom-core/src/predicates/helpers.ts index d0399b2359..a5ff9e8a81 100644 --- a/packages/apidom-core/src/predicates/helpers.ts +++ b/packages/apidom-core/src/predicates/helpers.ts @@ -8,22 +8,22 @@ interface PredicateHelpers { hasClass: typeof hasClass; } -type BasicElementShape = { +interface ElementBasicsTrait { _storedElement: string; _content: unknown; -}; +} -type PrimitiveShape = { +interface ElementPrimitiveBehavior { primitive: () => unknown; -}; +} -type ElementShape = { +interface ElementTypeTrait { element: T; -}; +} -type ElementClassesShape = { +interface ElementClassesTrait { classes: ArrayElement | Array; -}; +} type PredicateCreator = (helpers: PredicateHelpers) => ElementPredicate; @@ -41,21 +41,21 @@ const hasMethod = ( ); }; -const hasBasicElementProps = (element: unknown): element is BasicElementShape => +const hasBasicElementProps = (element: unknown): element is ElementBasicsTrait => typeof element === 'object' && element != null && '_storedElement' in element && typeof element._storedElement === 'string' && // eslint-disable-line no-underscore-dangle '_content' in element; -const primitiveEq = (val: unknown, element: unknown): element is PrimitiveShape => { +const primitiveEq = (val: unknown, element: unknown): element is ElementPrimitiveBehavior => { if (typeof element === 'object' && element !== null && 'primitive' in element) { return typeof element.primitive === 'function' && element.primitive() === val; } return false; }; -const hasClass = (cls: string, element: unknown): element is ElementClassesShape => { +const hasClass = (cls: string, element: unknown): element is ElementClassesTrait => { return ( typeof element === 'object' && element !== null && @@ -65,7 +65,7 @@ const hasClass = (cls: string, element: unknown): element is ElementClassesShape ); }; -export const isElementType = (name: string, element: unknown): element is ElementShape => +export const isElementType = (name: string, element: unknown): element is ElementTypeTrait => typeof element === 'object' && element !== null && 'element' in element &&