feat(auth,foundation): Wave 2 flag-gated Zitadel JWT verify + validation + eslint + tests
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
844220d9f5
commit
1d76d50f3d
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* `0pt/validation/0pt-schemas.ts` -- Wave 2 input validation for the 0pt
|
||||
* capability API's MUTATING routes.
|
||||
*
|
||||
* Before Wave 2 every POST/PUT handler did `const body = await request.json()`
|
||||
* and read fields straight off `body` (an implicit `any`), so a malformed
|
||||
* payload reached the service layer untyped and surfaced as an opaque 500/400.
|
||||
* These Zod schemas validate the request body at the edge and produce a typed,
|
||||
* structured 400. This is VALIDATION ONLY -- it re-encodes the shape each
|
||||
* handler already destructures; it does not add or change business logic.
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/** A structured 400 parse result. */
|
||||
export type ParseResult<T> =
|
||||
| { ok: true; data: T }
|
||||
| { ok: false; status: 400; body: { error: 'validation_error'; issues: { path: string; message: string }[] } };
|
||||
|
||||
/** Validate `body` against `schema`, returning parsed data or a 400 payload. */
|
||||
export function parseBody<S extends z.ZodTypeAny>(schema: S, body: unknown): ParseResult<z.infer<S>> {
|
||||
const result = schema.safeParse(body);
|
||||
if (result.success) return { ok: true, data: result.data };
|
||||
return {
|
||||
ok: false,
|
||||
status: 400,
|
||||
body: {
|
||||
error: 'validation_error',
|
||||
issues: result.error.issues.map((i) => ({
|
||||
path: i.path.join('.'),
|
||||
message: i.message,
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// A permissive JSON object (used for opaque config blobs whose inner shape is
|
||||
// owned by a downstream adapter, not this validation layer).
|
||||
const jsonObject = z.record(z.string(), z.unknown());
|
||||
|
||||
/** POST /api/0pt/documents/capture */
|
||||
export const documentCaptureSchema = z.object({
|
||||
mailbox: z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
client_id: z.string().min(1),
|
||||
client_secret: z.string().min(1),
|
||||
mailbox_address: z.string().min(1),
|
||||
folder: z.string().min(1).optional(),
|
||||
}),
|
||||
since_iso: z.string().min(1).optional(),
|
||||
top: z.number().int().positive().optional(),
|
||||
max_pages: z.number().int().positive().optional(),
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
actor_id: z.string().min(1).optional(),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
/** POST /api/0pt/documents/[documentId]/extraction */
|
||||
export const documentExtractionSchema = z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
actor_id: z.string().min(1).optional(),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
model_id: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
/** POST /api/0pt/sources/[sourceId]/query */
|
||||
export const sourceQuerySchema = z.object({
|
||||
query: z.string().trim().min(1),
|
||||
params: z.unknown().optional(),
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
actor_id: z.string().min(1).optional(),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
max_rows: z.number().int().positive().optional(),
|
||||
timeout_ms: z.number().int().positive().optional(),
|
||||
connection_config: jsonObject,
|
||||
});
|
||||
|
||||
/** POST /api/0pt/sources/rate-policies -- mirrors CreateSourceRatePolicyInput. */
|
||||
export const createSourceRatePolicySchema = z.object({
|
||||
source_rate_policy_id: z.string().min(1),
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
source_id: z.string().min(1),
|
||||
rate_limit_per_minute: z.number().int().nonnegative(),
|
||||
crawl_budget: z.number().int().nonnegative(),
|
||||
compliance_notes: z.string().optional(),
|
||||
domain: z.string().min(1).optional(),
|
||||
robots_ok: z.boolean().optional(),
|
||||
access: z.enum(['api', 'bulk', 'authorized_web', 'licensed']).optional(),
|
||||
status: z.enum(['active', 'paused', 'deprecated']).optional(),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
created_by: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
/** POST /api/0pt/lakehouse/mappings -- mirrors MapDatasetToIcebergTableInput. */
|
||||
export const mapDatasetToIcebergTableSchema = z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
dataset_id: z.string().min(1),
|
||||
dataset_iceberg_mapping_id: z.string().min(1),
|
||||
namespace: z.string().min(1),
|
||||
table_name: z.string().min(1),
|
||||
storage_uri: z.string().min(1),
|
||||
catalog_ref: z.string().min(1),
|
||||
table_format: z.string().min(1).optional(),
|
||||
actor_id: z.string().min(1),
|
||||
correlation_id: z.string().min(1),
|
||||
});
|
||||
|
||||
/** POST /api/0pt/lakehouse/mappings/[mappingId]/writes -- RequestIcebergWriteInput. */
|
||||
export const requestIcebergWriteSchema = z.object({
|
||||
actor_id: z.string().min(1),
|
||||
correlation_id: z.string().min(1),
|
||||
snapshot_type: z.string().min(1).optional(),
|
||||
schema_version: z.string().min(1),
|
||||
manifest_uri: z.string().min(1),
|
||||
record_count: z.number().int().nonnegative().optional(),
|
||||
size_bytes: z.number().int().nonnegative().optional(),
|
||||
heavy: z.boolean().optional(),
|
||||
});
|
||||
Reference in New Issue
Block a user