diff --git a/.eslintrc b/.eslintrc index d8915a8f..baf17b8c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,38 +1,45 @@ { - "parser": "@typescript-eslint/parser", - "extends": [ - "plugin:@typescript-eslint/recommended" - ], - "parserOptions": { - "ecmaVersion": 2018, - "sourceType": "module" - }, - "rules": { - "curly": 2, - "indent": 0, - "no-console": 0, - "max-len": [ - 2, - { - "code": 200 - } - ], - "trailing-comma": 0, - "object-literal-sort-keys": 0, - "no-reference": 0, - "no-trailing-whitespace": 0, - "typedef-whitespace": 0, - "whitespace": 0, - "interface-name": 0, - "ordered-imports": 0, - "variable-name": 0, - "no-var-requires": 0, - "object-literal-shorthand": 0, - "no-empty": 0, - "max-classes-per-file": 0, - "prefer-const": 0, - "no-unused-expression": 0, - "forin": 0, - "object-literal-key-quotes": 0 - } + "parser": "@typescript-eslint/parser", + "extends": ["plugin:@typescript-eslint/recommended"], + "parserOptions": { + "ecmaVersion": 2018, + "sourceType": "module" + }, + "rules": { + "curly": 2, + "indent": 0, + "no-console": 0, + "max-len": [ + 2, + { + "code": 200 + } + ], + "@typescript-eslint/no-explicit-any": "error", + "trailing-comma": 0, + "object-literal-sort-keys": 0, + "no-reference": 0, + "no-trailing-whitespace": 0, + "typedef-whitespace": 0, + "whitespace": 0, + "interface-name": 0, + "ordered-imports": 0, + "variable-name": 0, + "no-var-requires": 0, + "object-literal-shorthand": 0, + "no-empty": 0, + "max-classes-per-file": 0, + "prefer-const": 0, + "no-unused-expression": 0, + "forin": 0, + "object-literal-key-quotes": 0 + }, + "overrides": [ + { + "files": ["*.spec.ts"], + "rules": { + "@typescript-eslint/no-explicit-any": "off" + } + } + ] } diff --git a/src/verifier/argumentMapper/index.ts b/src/verifier/argumentMapper/index.ts index 17417941..aace0dce 100644 --- a/src/verifier/argumentMapper/index.ts +++ b/src/verifier/argumentMapper/index.ts @@ -24,16 +24,12 @@ export const setupVerification = ( switch (validation.status) { case FnValidationStatus.FAIL: logErrorAndThrow( - `the required ffi function '${fn}' failed validation with errors: ${ - validation.messages || [].join(',') - }` + `the required ffi function '${fn}' failed validation with errors: ${validation.messages}` ); break; case FnValidationStatus.IGNORE: logger.debug( - `the optional ffi function '${fn}' was not executed as it had non-fatal validation errors: ${ - validation.messages || [].join(',') - }` + `the optional ffi function '${fn}' was not executed as it had non-fatal validation errors: ${validation.messages}` ); break; case FnValidationStatus.SUCCESS: diff --git a/src/verifier/argumentMapper/types.ts b/src/verifier/argumentMapper/types.ts index b27bd0aa..30b777f9 100644 --- a/src/verifier/argumentMapper/types.ts +++ b/src/verifier/argumentMapper/types.ts @@ -1,17 +1,25 @@ import logger from '../../logger'; +import { Ffi, FfiHandle } from '../../ffi/types'; -export const deprecatedFunction = (_: any, property: string): boolean => { +export const deprecatedFunction = (_: unknown, property: string): boolean => { logger.warn(`${property} is deprecated and no longer has any effect`); return true; }; -import { Ffi, FfiHandle } from '../../ffi/types'; -export type FnObject = { - [key: string]: (...args: any) => any; +type KeyedObject = { + [key: string]: unknown; +}; + +type FnArgumentMapping = { + validateAndExecute: ( + ffi: Ffi, + handle: FfiHandle, + options: O + ) => FnValidationResult; }; -export type FnMapping = { +export type FnMapping = { [Key in keyof T]: FnArgumentMapping; }; @@ -21,15 +29,7 @@ export enum FnValidationStatus { FAIL = 2, } -export type FnValidationResult = { +type FnValidationResult = { status: FnValidationStatus; messages?: string[]; }; - -export type FnArgumentMapping = { - validateAndExecute: ( - ffi: Ffi, - handle: FfiHandle, - options: O - ) => FnValidationResult; -}; diff --git a/src/verifier/validateOptions.ts b/src/verifier/validateOptions.ts index ce2716e1..48538fa3 100644 --- a/src/verifier/validateOptions.ts +++ b/src/verifier/validateOptions.ts @@ -10,7 +10,7 @@ import { export const deprecatedFunction = () => - (_: any, property: string): boolean => { + (_: unknown, property: string): boolean => { logger.warn(`${property} is deprecated and no longer has any effect`); return true; @@ -19,7 +19,7 @@ export const deprecatedFunction = export const deprecatedBy = (preferredOption: string) => () => - (_: any, property: string): boolean => { + (_: unknown, property: string): boolean => { logger.warn(`${property} is deprecated, use ${preferredOption} instead`); return true; @@ -28,7 +28,7 @@ export const deprecatedBy = export const incompatibleWith = (keys: (keyof InternalPactVerifierOptions)[]) => (options: InternalPactVerifierOptions) => - (_: any, property: string): boolean => { + (_: unknown, property: string): boolean => { const incompatibilities = pick(options, keys); if (Object.keys(incompatibilities).length > 0) { @@ -46,7 +46,7 @@ export const incompatibleWith = export const requires = (keys: (keyof InternalPactVerifierOptions)[]) => (options: InternalPactVerifierOptions) => - (_: any, property: string): boolean => { + (_: unknown, property: string): boolean => { const required = pick(options, keys); if (keys.length !== Object.keys(required).length) { @@ -62,7 +62,7 @@ export const requires = export const requiresOneOf = (keys: (keyof InternalPactVerifierOptions)[]) => (options: InternalPactVerifierOptions) => - (_: any, property: string): boolean => { + (_: unknown, property: string): boolean => { const required = pick(options, keys); if (Object.keys(required).length === 0) { @@ -77,25 +77,34 @@ export const requiresOneOf = return true; }; -export type AssertFunction = (a: any, ...args: any) => boolean; +type AssertFunction = (a: unknown, property: string) => boolean; -export const wrapCheckType = (fn: AssertFunction) => (): AssertFunction => fn; +const assertNonEmptyString = + (): AssertFunction => (a: unknown, property: string) => + checkTypes.assert.nonEmptyString(a as string, property); + +const assertBoolean = (): AssertFunction => (a: unknown, property: string) => + checkTypes.assert.boolean(a as boolean, property); + +const assertPositive = (): AssertFunction => (a: unknown, property: string) => + checkTypes.assert.positive(a as number, property); const LogLevels: LogLevel[] = ['debug', 'error', 'info', 'trace', 'warn']; const logLevelValidator = () => - (l: LogLevel): boolean => { - if (LogLevels.includes(l.toLowerCase() as LogLevel)) { - l = l.toLowerCase() as LogLevel; - } else { - throw new Error( - `The logLevel '${l}' is not a valid logLevel. The valid options are: ${LogLevels.join( - ', ' - )}` - ); + (l: unknown): boolean => { + if (typeof l === 'string') { + if (LogLevels.includes(l.toLowerCase() as LogLevel)) { + l = l.toLowerCase() as LogLevel; + return true; + } } - return true; + throw new Error( + `The logLevel '${l}' is not a valid logLevel. The valid options are: ${LogLevels.join( + ', ' + )}` + ); }; const consumerVersionSelectorValidator = @@ -188,19 +197,19 @@ export type ArgumentValidationRules = { export const validationRules: ArgumentValidationRules = { - providerBaseUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)], - buildUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)], + providerBaseUrl: [assertNonEmptyString], + buildUrl: [assertNonEmptyString], consumerVersionSelectors: [consumerVersionSelectorValidator], consumerVersionTags: [consumerVersionTagsValidator], customProviderHeaders: [customProviderHeadersValidator], - disableSslVerification: [wrapCheckType(checkTypes.assert.boolean)], - enablePending: [wrapCheckType(checkTypes.assert.boolean)], + disableSslVerification: [assertBoolean], + enablePending: [assertBoolean], format: [deprecatedFunction], - includeWipPactsSince: [wrapCheckType(checkTypes.assert.nonEmptyString)], - provider: [wrapCheckType(checkTypes.assert.nonEmptyString)], - pactUrls: [wrapCheckType(checkTypes.assert.nonEmptyString)], + includeWipPactsSince: [assertNonEmptyString], + provider: [assertNonEmptyString], + pactUrls: [assertNonEmptyString], pactBrokerUrl: [ - wrapCheckType(checkTypes.assert.nonEmptyString), + assertNonEmptyString, requires(['provider']), requiresOneOf([ 'pactUrls', @@ -209,41 +218,38 @@ export const validationRules: ArgumentValidationRules { @@ -256,7 +262,7 @@ export const validateOptions = (options: VerifierOptions): VerifierOptions => { // get type of parameter (if an array, we apply the rule to each item of the array instead) if (Array.isArray(options[k])) { - (options[k] as Array).map((item) => { + options[k].map((item: unknown) => { rules.map((rule) => { // rule(item) // If the messages aren't clear, we can do this rule(options)(item, k);