From a2e9b59e182255bcefd8e2bcf1973a71f57eebfa Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:32:51 +0200 Subject: [PATCH] Refactor extract utility function for type checking --- .../src/schema/index.ts | 4 +-- .../rules/InputTypeValidationRule.ts | 29 ++++--------------- server/packages/server-common/src/utils.ts | 28 ++++++++++++++++++ 3 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 server/packages/server-common/src/utils.ts diff --git a/server/gx-workflow-ls-format2/src/schema/index.ts b/server/gx-workflow-ls-format2/src/schema/index.ts index a4239af..d1ebff3 100644 --- a/server/gx-workflow-ls-format2/src/schema/index.ts +++ b/server/gx-workflow-ls-format2/src/schema/index.ts @@ -1,5 +1,5 @@ +import { FieldSchemaNode, RecordSchemaNode, SchemaNode } from "./definitions"; import { GalaxyWorkflowFormat2SchemaLoader } from "./schemaLoader"; -import { RecordSchemaNode, FieldSchemaNode, SchemaNode } from "./definitions"; import { SchemaNodeResolver } from "./schemaNodeResolver"; -export { GalaxyWorkflowFormat2SchemaLoader, SchemaNodeResolver, SchemaNode, RecordSchemaNode, FieldSchemaNode }; +export { FieldSchemaNode, GalaxyWorkflowFormat2SchemaLoader, RecordSchemaNode, SchemaNode, SchemaNodeResolver }; diff --git a/server/gx-workflow-ls-format2/src/validation/rules/InputTypeValidationRule.ts b/server/gx-workflow-ls-format2/src/validation/rules/InputTypeValidationRule.ts index 6798f63..1a759bb 100644 --- a/server/gx-workflow-ls-format2/src/validation/rules/InputTypeValidationRule.ts +++ b/server/gx-workflow-ls-format2/src/validation/rules/InputTypeValidationRule.ts @@ -1,4 +1,5 @@ import { ValidationRule } from "@gxwf/server-common/src/languageTypes"; +import { isCompatibleType } from "@gxwf/server-common/src/utils"; import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver-types"; import { GxFormat2WorkflowDocument } from "../../gxFormat2WorkflowDocument"; @@ -13,36 +14,18 @@ export class InputTypeValidationRule implements ValidationRule { const inputNodes = documentContext.getRawInputNodes(); inputNodes.forEach((input) => { - let defaultTypeMatchesValue = true; - const inputName = String(input.keyNode.value); - const inputTypeValue = documentContext.nodeManager.getPropertyValueByName(input, "type"); + const inputType = documentContext.nodeManager.getPropertyValueByName(input, "type") as string; const defaultValueNode = documentContext.nodeManager.getPropertyNodeByName(input, "default"); const defaultValue = defaultValueNode?.valueNode?.value; const defaultValueType = typeof defaultValue; - if (inputTypeValue && defaultValue) { - switch (inputTypeValue) { - case "int": - case "integer": - case "long": - case "double": - case "float": - defaultTypeMatchesValue = defaultValueType === "number"; - break; - case "boolean": - defaultTypeMatchesValue = defaultValueType === "boolean"; - break; - case "string": - defaultTypeMatchesValue = defaultValueType === "string"; - break; - case "null": - defaultTypeMatchesValue = defaultValueType === null; - break; - } + + if (inputType && defaultValue) { + const defaultTypeMatchesValue = isCompatibleType(inputType, defaultValueType); if (!defaultTypeMatchesValue) { result.push({ - message: `Input '${inputName}' default value has invalid type. Expected '${inputTypeValue}' but found '${defaultValueType}'.`, + message: `Input '${inputName}' default value has invalid type. Expected '${inputType}' but found '${defaultValueType}'.`, range: documentContext.nodeManager.getNodeRange(defaultValueNode), severity: this.severity, }); diff --git a/server/packages/server-common/src/utils.ts b/server/packages/server-common/src/utils.ts new file mode 100644 index 0000000..7ae4422 --- /dev/null +++ b/server/packages/server-common/src/utils.ts @@ -0,0 +1,28 @@ +/** + * Check if the actual type can be mapped to the expected type. + * Usefull to validate properties types. + * @param expectedType The expected type. Usually a type supported by the schema. + * @param actualType The actual type. You can use the `typeof` operator to get this value. + */ +export function isCompatibleType(expectedType: string, actualType: string): boolean { + let isCompatible = true; + switch (expectedType) { + case "int": + case "integer": + case "long": + case "double": + case "float": + isCompatible = actualType === "number"; + break; + case "boolean": + isCompatible = actualType === "boolean"; + break; + case "string": + isCompatible = actualType === "string"; + break; + case "null": + isCompatible = actualType === null; + break; + } + return isCompatible; +}