Skip to content

Commit

Permalink
Refactor extract utility function for type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Jun 19, 2024
1 parent e598454 commit a2e9b59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions server/gx-workflow-ls-format2/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
});
Expand Down
28 changes: 28 additions & 0 deletions server/packages/server-common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a2e9b59

Please sign in to comment.