Open History to see the last commit
1/**
2 * Payload validation against stored endpoint schemas (PLAN.md task 4.4),
3 * using @cfworker/json-schema — built for Workers (ajv's codegen trips
4 * CSP/eval restrictions).
5 */
6import { Validator } from '@cfworker/json-schema'
7import { and, eq, isNull } from 'drizzle-orm'
8import type { Schema } from '@cfworker/json-schema'
9
10import type { Db } from '#/db/index.ts'
11import { schemaVersions } from '#/db/schema.ts'
12import { resolveEndpointAlias } from '#/server/schema-binding.ts'
13
14export interface ValidateRequestBody {
15 provider: string
16 endpointId: string
17 kind?: 'input' | 'output'
18 payload: unknown
19}
20
21export interface ValidationErrorItem {
22 path: string
23 message: string
24 keyword: string
25}
26
27export type ValidateOutcome =
28 | {
29 ok: true
30 result: {
31 valid: boolean
32 errors: Array<ValidationErrorItem>
33 provider: string
34 endpointId: string
35 kind: 'input' | 'output'
36 contentHash: string
37 }
38 }
39 | { ok: false; status: number; code: string; message: string }
40
41export function parseValidateBody(raw: unknown): ValidateRequestBody | null {
42 if (typeof raw !== 'object' || raw === null) return null
43 const body = raw as Record<string, unknown>
44 if (typeof body.provider !== 'string' || typeof body.endpointId !== 'string')
45 return null
46 if (
47 body.kind !== undefined &&
48 body.kind !== 'input' &&
49 body.kind !== 'output'
50 )
51 return null
52 if (!('payload' in body)) return null
53 return {
54 provider: body.provider,
55 endpointId: body.endpointId,
56 kind: body.kind,
57 payload: body.payload,
58 }
59}
60
61export async function validatePayload(
62 db: Db,
63 body: ValidateRequestBody,
64): Promise<ValidateOutcome> {
65 const kind = body.kind ?? 'input'
66 const resolved = await resolveEndpointAlias(
67 db,
68 body.provider,
69 body.endpointId,
70 )
71 if (!resolved) {
72 return {
73 ok: false,
74 status: 404,
75 code: 'unknown_endpoint',
76 message: `Unknown endpoint '${body.endpointId}' for provider '${body.provider}'. See GET /v1/schemas/${body.provider} for valid endpoint ids.`,
77 }
78 }
79
80 const version = await db.query.schemaVersions.findFirst({
81 where: and(
82 eq(schemaVersions.endpointId, resolved.dbId),
83 eq(schemaVersions.kind, kind),
84 isNull(schemaVersions.supersededAt),
85 ),
86 })
87 if (!version) {
88 return {
89 ok: false,
90 status: 404,
91 code: 'no_schema',
92 message: `No current ${kind} schema for endpoint '${resolved.publicId}' (${body.provider}). It may stream binary media or not be synced yet — check GET /v1/schemas/${body.provider}/${resolved.activity}.`,
93 }
94 }
95
96 const schema = JSON.parse(version.schema) as Schema
97 const validator = new Validator(schema, '2020-12', false)
98 const result = validator.validate(body.payload)
99
100 return {
101 ok: true,
102 result: {
103 valid: result.valid,
104 errors: result.errors.map((e) => ({
105 path: e.instanceLocation,
106 message: e.error,
107 keyword: e.keyword,
108 })),
109 provider: body.provider,
110 endpointId: resolved.publicId,
111 kind,
112 contentHash: version.contentHash,
113 },
114 }
115}