diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts index 8727f706..1c9112c5 100644 --- a/apps/api/src/controllers/v1/types.ts +++ b/apps/api/src/controllers/v1/types.ts @@ -186,6 +186,10 @@ export const scrapeOptions = z export type ScrapeOptions = z.infer; +import Ajv from "ajv"; + +const ajv = new Ajv(); + export const extractV1Options = z .object({ urls: url @@ -193,7 +197,20 @@ export const extractV1Options = z .max(10, "Maximum of 10 URLs allowed per request while in beta."), prompt: z.string().optional(), systemPrompt: z.string().optional(), - schema: z.any().optional(), + schema: z + .any() + .optional() + .refine((val) => { + if (!val) return true; // Allow undefined schema + try { + const validate = ajv.compile(val); + return typeof validate === "function"; + } catch (e) { + return false; + } + }, { + message: "Invalid JSON schema.", + }), limit: z.number().int().positive().finite().safe().optional(), ignoreSitemap: z.boolean().default(false), includeSubdomains: z.boolean().default(true),