Skip to content

Commit

Permalink
Validate openapi operation in the requestpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
brettimus committed Dec 3, 2024
1 parent df590aa commit 34b5105
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/types/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const OpenAPIRequestBodySchema = z.object({
content: z.record(ContentSchema),
});

const OpenAPIOperationSchema = z.object({
export const OpenAPIOperationSchema = z.object({
title: z.string().optional(),
summary: z.string().optional(),
description: z.string().optional(),
Expand Down
13 changes: 3 additions & 10 deletions studio/src/pages/RequestorPage/RequestPanel/RequestPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button } from "@/components/ui/button";
import { Tabs } from "@/components/ui/tabs";
import { useToast } from "@/components/ui/use-toast";
import { cn, objectWithKey } from "@/utils";
import { cn } from "@/utils";
import { EraserIcon, InfoCircledIcon } from "@radix-ui/react-icons";
import { type Dispatch, type SetStateAction, memo, useMemo } from "react";
import { FormDataForm } from "../FormDataForm";
Expand All @@ -22,6 +22,7 @@ import {
} from "@/components/CodeMirrorEditor";
import { useRequestorStore } from "../store";
import { RouteDocumentation } from "./RouteDocumentation/RouteDocumentation";
import { isOpenApiOperation } from "./RouteDocumentation/openapi";

type RequestPanelProps = {
aiEnabled: boolean;
Expand All @@ -37,14 +38,6 @@ type RequestPanelProps = {
onSubmit: () => void;
};

function isValidOpenApiSpec(openApiSpec: unknown): boolean {
return (
objectWithKey(openApiSpec, "parameters") ||
objectWithKey(openApiSpec, "requestBody") ||
objectWithKey(openApiSpec, "responses")
);
}

export const RequestPanel = memo(function RequestPanel(
props: RequestPanelProps,
) {
Expand Down Expand Up @@ -116,7 +109,7 @@ export const RequestPanel = memo(function RequestPanel(
return null;
}
}, [activeRoute?.openApiSpec]);
const shouldShowDocs = isValidOpenApiSpec(openApiSpec);
const shouldShowDocs = isOpenApiOperation(openApiSpec);

return (
<Tabs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type OpenApiSpec, OpenApiSpecSchema } from "@fiberplane/fpx-types";
import {
type OpenAPIOperation,
OpenAPIOperationSchema,
type OpenApiSpec,
OpenApiSpecSchema,
} from "@fiberplane/fpx-types";

export type { OpenAPIOperation } from "@fiberplane/fpx-types";
export type { OpenAPISchema } from "@fiberplane/fpx-types";
Expand All @@ -14,3 +19,8 @@ export function isOpenApiSpec(value: unknown): value is OpenApiSpec {
}
return result.success;
}

export function isOpenApiOperation(value: unknown): value is OpenAPIOperation {
const result = OpenAPIOperationSchema.safeParse(value);
return result.success;
}

0 comments on commit 34b5105

Please sign in to comment.