export const FACT_STATUSES = ["public_confirmed", "internal_confirmed", "hypothesis", "outdated", "forbidden"] as const; type FieldType = "string" | "int" | "bool"; export interface Resource { route: string; table: string; fields: Record; /** Дополнительное условие публичного вывода. */ publicWhere?: string; } const common: Resource["fields"] = { status: { type: "string" }, confidence: { type: "int" }, source_evidence_id: { type: "int" }, }; export const RESOURCES: Resource[] = [ { route: "sources", table: "source_evidence", fields: { title: { type: "string", required: true }, url: { type: "string" }, note: { type: "string" }, status: { type: "string" }, confidence: { type: "int" } } }, { route: "profile", table: "person_profile", fields: { full_name: { type: "string", required: true }, headline: { type: "string" }, bio: { type: "string" }, ...common } }, { route: "career", table: "career_history", fields: { organization: { type: "string", required: true }, position: { type: "string" }, start_year: { type: "int" }, end_year: { type: "int" }, is_current: { type: "bool" }, ...common } }, { route: "credentials", table: "credentials", fields: { title: { type: "string", required: true }, issuer: { type: "string" }, year: { type: "int" }, document_url: { type: "string" }, ...common } }, { route: "cases", table: "cases", fields: { title: { type: "string", required: true }, problem: { type: "string" }, role: { type: "string" }, solution: { type: "string" }, scale: { type: "string" }, result: { type: "string" }, metrics: { type: "string" }, public_use_permitted: { type: "bool" }, ...common }, publicWhere: "public_use_permitted = true", }, ]; /** Возвращает очищенные значения или текст ошибки. */ export function validate(res: Resource, body: unknown, partial: boolean): { values: Record } | { error: string } { if (typeof body !== "object" || body === null || Array.isArray(body)) return { error: "body must be an object" }; const input = body as Record; const values: Record = {}; for (const [name, spec] of Object.entries(res.fields)) { const v = input[name]; if (v === undefined) { if (spec.required && !partial) return { error: `${name} is required` }; continue; } if (v === null && !spec.required) { values[name] = null; continue; } if (spec.type === "string" && typeof v !== "string") return { error: `${name} must be a string` }; if (spec.type === "int" && !Number.isInteger(v)) return { error: `${name} must be an integer` }; if (spec.type === "bool" && typeof v !== "boolean") return { error: `${name} must be a boolean` }; if (name === "status" && !(FACT_STATUSES as readonly string[]).includes(v as string)) return { error: `status must be one of ${FACT_STATUSES.join(", ")}` }; if (name === "confidence" && ((v as number) < 0 || (v as number) > 100)) return { error: "confidence must be 0..100" }; values[name] = v; } return { values }; }