Skip to content

Commit fde0df9

Browse files
committed
feat: Provide narrowed type error on invalid inputs
1 parent 0dcd66c commit fde0df9

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/action.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,22 @@ describe("Action", () => {
9191
expect(() => getConfig()).toThrowError();
9292
});
9393

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

98-
expect(() => getConfig()).toThrowError();
98+
expect(() => getConfig()).not.toThrowError();
99+
});
100+
101+
it("should throw if a workflow inputs JSON doesn't contain strings numbers or booleans", () => {
102+
mockEnvConfig.workflow_inputs = '{"pie":{"powerLevel":9001}}';
103+
expect(() => getConfig()).toThrowError('"pie" value is object');
104+
105+
mockEnvConfig.workflow_inputs = '{"vegetable":null}';
106+
expect(() => getConfig()).toThrowError('"vegetable" value is null');
107+
108+
mockEnvConfig.workflow_inputs = '{"fruit":[]}';
109+
expect(() => getConfig()).toThrowError('"fruit" value is Array');
99110
});
100111
});
101112
});

src/action.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getConfig(): ActionConfig {
5959
workflow: getWorkflowValue(core.getInput("workflow", { required: true })),
6060
workflowInputs: getWorkflowInputs(core.getInput("workflow_inputs")),
6161
workflowTimeoutSeconds:
62-
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ||
62+
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ??
6363
WORKFLOW_TIMEOUT_SECONDS,
6464
};
6565
}
@@ -92,7 +92,19 @@ function getWorkflowInputs(
9292
try {
9393
const parsedJson = JSON.parse(workflowInputs);
9494
for (const key of Object.keys(parsedJson)) {
95-
const type = typeof parsedJson[key];
95+
const value = parsedJson[key];
96+
const type = (() => {
97+
switch (true) {
98+
case value === null: {
99+
return "null";
100+
}
101+
case Array.isArray(value): {
102+
return "Array";
103+
}
104+
default:
105+
return typeof value;
106+
}
107+
})();
96108
if (!["string", "number", "boolean"].includes(type)) {
97109
throw new Error(
98110
`Expected value to be string, number, or boolean. "${key}" value is ${type}`,

0 commit comments

Comments
 (0)