Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Oct 1, 2024
1 parent 690f8fa commit 3ec461d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as github from '@actions/github';
import {Deployment} from '@octokit/graphql-schema';
import {GitHub} from "@actions/github/lib/utils";
import {getWorkflowRunHtmlUrl} from "./github.js";
import YAML from "yaml";

export const context = enhancedContext()

Expand Down Expand Up @@ -104,6 +105,7 @@ export function getInput<T extends ZodSchema>(
name: string, options_schema?: InputOptions | T, schema?: T
): string | z.infer<T> | undefined {
let options: InputOptions | undefined
// noinspection SuspiciousTypeOfGuard
if (options_schema instanceof ZodSchema) {
schema = options_schema
} else {
Expand All @@ -114,10 +116,32 @@ export function getInput<T extends ZodSchema>(
if (!input) return undefined
if (!schema) return input

const parseResult = schema.safeParse(input)
let parseResult = schema.safeParse(input)
if (parseResult.error) {
const initialIssue = parseResult.error.issues.at(0);
if (initialIssue?.code === "invalid_type" &&
initialIssue.received === "string" &&
initialIssue.expected !== "string"
) {
// try parse as yaml/json
parseResult = z.string().transform((val, ctx) => {
try {
return YAML.parse(val);
} catch {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: initialIssue.expected,
received: 'unknown',
})
return z.NEVER;
}
}).pipe(schema).safeParse(input);
}
}

if (parseResult.error) {
const issues = parseResult.error.issues.map(formatZodIssue)
throw new Error(`Invalid value for input '${name}': ${input}\n` +
throw new Error(`Invalid input value for \`${name}\`, received \`${input}\`\n` +
issues.map((it) => ` - ${it}`).join('\n'))
}

Expand Down

0 comments on commit 3ec461d

Please sign in to comment.