Files
Alberto Schiabel 61aafbc9d3 refactor(docs): parse untyped data with zod and forbid explicit any (#3967)
This PR:

- builds on top of https://github.com/ComposioHQ/composio/pull/3966
- enables `typescript/no-explicit-any` (error, `fixToUnknown`) for docs
in `docs/.oxlintrc.json` and removes every remaining explicit `any` in
docs code
- parses untyped/external data once at the boundary with zod v4 schemas
and lets `z.infer` types flow downstream — no hand-rolled `'x' in obj`
guard chains, no `as`-casts of untyped page data, no
`docs/lib/unknown-value.ts`
- adds domain schema modules: `docs/lib/toolkit-schema.ts` (recursive
JSON-Schema node, raw tool/trigger payloads, list envelopes) and
`docs/lib/reference-page-data.ts` (fumadocs reference page data for both
reference routes)
- rewrites `validate-links.ts`, `generate-toolkits.ts`,
`generate-meta-tools.ts`, the `llms.mdx` route, and
`deprecated-api-sidebar.tsx` on those schemas with identical validation
outcomes
- carries the pure typing improvements from the earlier attempt (typed
reference source in `source.ts`, `LLMPage`/`PageLike`, typed
`ClientLogo[]` in `logo-bar.tsx`, component `any` removals)
- adds `tests/static/toolkit-schema.test.ts` and
`tests/static/generate-toolkits.test.ts` pinning the transform shapes;
no changeset (docs is not published)

## Context

Second of the three-PR split of #3958, replacing its rejected
structural-guard approach with zod schemas at the data boundaries.
Verified with `bun run lint`, `bun run types:check`, `bun test
tests/static/` (125 pass), `bun run lint:links`, and a full `next
build`.

## Review follow-up

Addressed the regressions reported in [the Zod boundary
re-review](https://github.com/ComposioHQ/composio/pull/3967#issuecomment-5105279224):
scalar JSON Schema enums and auth defaults are preserved as strings,
malformed toolkit page envelopes abort generation, and the pre-refactor
empty-string fallbacks are restored. Regression coverage lives in
`tests/static/toolkit-schema.test.ts` and
`tests/static/generate-toolkits.test.ts`.

Verified on `2f26c40be` with `bun test tests/static/` (125 pass), `bun
run types:check`, and `bun run lint` (0 errors; existing warnings only).
2026-08-03 19:52:04 +05:30

34 lines
1.5 KiB
TypeScript

import type { ReactNode } from 'react';
import type { TOCItemType } from 'fumadocs-core/toc';
import type { OpenAPIPageProps } from 'fumadocs-openapi/ui';
import { z } from 'zod';
import type { OpenApiSchemaPageData } from '@/lib/api-deprecation';
import type { ReferenceMdxPageData } from '@/lib/source';
// The combined reference source mixes MDX and fumadocs-openapi pages, so page
// data is parsed once against the shape each renderer needs instead of being
// narrowed with `in` guards and casts. Function-valued members use z.custom
// with a typeof check because zod cannot otherwise validate functions.
export const openApiReferencePageDataSchema = z.object({
title: z.string(),
getOpenAPIPageProps: z.custom<() => OpenAPIPageProps>(value => typeof value === 'function'),
getSchema: z.custom<OpenApiSchemaPageData['getSchema']>(value => typeof value === 'function'),
});
// A TOC entry's `title` is a ReactNode, which admits nearly any runtime value
// (string, number, element, fragment, null, ...), so it is not runtime-checkable;
// the structural fields are validated. looseObject keeps extra members such as
// remark-steps' `_step` intact.
const tocItemSchema: z.ZodType<TOCItemType> = z.looseObject({
title: z.custom<ReactNode>(() => true),
url: z.string(),
depth: z.number(),
});
export const referenceMdxPageDataSchema = z.object({
title: z.string(),
full: z.boolean().optional().catch(undefined),
toc: z.array(tocItemSchema),
body: z.custom<ReferenceMdxPageData['body']>(value => typeof value === 'function'),
});