Skip to content

Commit

Permalink
feat: Provide narrowed type error on invalid inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Nov 23, 2023
1 parent 0dcd66c commit fde0df9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,22 @@ describe("Action", () => {
expect(() => getConfig()).toThrowError();
});

it("should throw if a workflow inputs JSON is contains non-strings", () => {
it("should handle workflow inputs JSON containing strings numbers or booleans", () => {
mockEnvConfig.workflow_inputs =
'{"cake":"delicious","pie":{"powerLevel":9001}}';
'{"cake":"delicious","pie":9001,"parfait":false}';

expect(() => getConfig()).toThrowError();
expect(() => getConfig()).not.toThrowError();
});

it("should throw if a workflow inputs JSON doesn't contain strings numbers or booleans", () => {
mockEnvConfig.workflow_inputs = '{"pie":{"powerLevel":9001}}';
expect(() => getConfig()).toThrowError('"pie" value is object');

mockEnvConfig.workflow_inputs = '{"vegetable":null}';
expect(() => getConfig()).toThrowError('"vegetable" value is null');

mockEnvConfig.workflow_inputs = '{"fruit":[]}';
expect(() => getConfig()).toThrowError('"fruit" value is Array');
});
});
});
16 changes: 14 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function getConfig(): ActionConfig {
workflow: getWorkflowValue(core.getInput("workflow", { required: true })),
workflowInputs: getWorkflowInputs(core.getInput("workflow_inputs")),
workflowTimeoutSeconds:
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ||
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ??
WORKFLOW_TIMEOUT_SECONDS,
};
}
Expand Down Expand Up @@ -92,7 +92,19 @@ function getWorkflowInputs(
try {
const parsedJson = JSON.parse(workflowInputs);
for (const key of Object.keys(parsedJson)) {
const type = typeof parsedJson[key];
const value = parsedJson[key];
const type = (() => {
switch (true) {
case value === null: {
return "null";
}
case Array.isArray(value): {
return "Array";
}
default:
return typeof value;
}
})();
if (!["string", "number", "boolean"].includes(type)) {
throw new Error(
`Expected value to be string, number, or boolean. "${key}" value is ${type}`,
Expand Down

0 comments on commit fde0df9

Please sign in to comment.