feat(01-03): implement BACKEND_SCHEMAS Zod schema builder
- buildZodSchema derives z.object() from BACKEND_REGISTRY field definitions - required fields map to z.string().min(1, label) — optional to z.string().optional() - BACKEND_SCHEMAS exported with entries for azureblob, s3, s3-compatible - BackendFormValues<T> utility type infers TypeScript type from schema - No hardcoded field names — all shapes derived from registry loop - All 8 tests in index.test.ts pass (GREEN)
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
// src/schemas/index.ts
|
||||||
|
// Zod schemas derived programmatically from the Backend Schema Registry.
|
||||||
|
// DO NOT hand-write z.object() calls with hardcoded field names — all shapes come from the registry.
|
||||||
|
// Adding a field to BACKEND_REGISTRY automatically adds it to validation.
|
||||||
|
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { BACKEND_REGISTRY, BackendType } from './registry';
|
||||||
|
|
||||||
|
function buildZodSchema(backendType: BackendType): z.ZodObject<Record<string, z.ZodTypeAny>> {
|
||||||
|
const fields = BACKEND_REGISTRY[backendType];
|
||||||
|
const shape: Record<string, z.ZodTypeAny> = {};
|
||||||
|
for (const field of fields) {
|
||||||
|
shape[field.key] = field.required
|
||||||
|
? z.string().min(1, `${field.label} is required`)
|
||||||
|
: z.string().optional();
|
||||||
|
}
|
||||||
|
return z.object(shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BACKEND_SCHEMAS = {
|
||||||
|
azureblob: buildZodSchema('azureblob'),
|
||||||
|
s3: buildZodSchema('s3'),
|
||||||
|
's3-compatible': buildZodSchema('s3-compatible'),
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
// Utility type: infer TypeScript type from a backend's Zod schema
|
||||||
|
export type BackendFormValues<T extends BackendType> =
|
||||||
|
z.infer<typeof BACKEND_SCHEMAS[T]>;
|
||||||
Reference in New Issue
Block a user