From 877066d8dadf71e57cec15781846a2308263ad53 Mon Sep 17 00:00:00 2001 From: Zach Kirsch Date: Tue, 10 Jan 2023 20:56:45 -0800 Subject: [PATCH] Add validation for inputs --- src/main.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 24753cf..8fb6cdf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,11 +4,13 @@ import { Collection, CollectionDefinition } from "postman-collection"; import { FernPostmanClient } from "@fern-fern/postman-sdk"; import { readFile } from "fs/promises"; +void run(); + async function run(): Promise { try { - const postmanApiKey: string = core.getInput("api-key"); - const postmanWorkspaceId: string = core.getInput("workspace-id"); - const postmanCollectionPath: string = core.getInput("collection-path"); + const postmanApiKey = getStringInputOrThrow("api-key"); + const postmanWorkspaceId = getStringInputOrThrow("workspace-id"); + const postmanCollectionPath = getStringInputOrThrow("collection-path"); const rawPostmanCollection = JSON.parse( (await readFile(postmanCollectionPath)).toString() @@ -137,4 +139,13 @@ async function run(): Promise { } } -run(); +function getStringInputOrThrow(key: string): string { + const input: unknown = core.getInput(key); + if (input == null) { + throw new Error(`${key} is not defined.`); + } + if (typeof input !== "string") { + throw new Error(`${key} is not a string.`); + } + return input; +}