/** * Checks the body of `PATCH /api/users/me/settings` before the users route * trusts it. * * @module */ import type { UpdateSettingsRequest } from "../../shared/settings.ts"; import { isColorScheme, isStyleName } from "../../shared/theme.ts"; const VALIDATORS: Record boolean> = { style: (v) => v === null || isStyleName(v), colorScheme: (v) => v === null || isColorScheme(v), digestWeekly: (v) => typeof v === "boolean", digestMonthly: (v) => typeof v === "boolean", }; /** * Checks whether `obj` is an {@link UpdateSettingsRequest}: an object whose * every key is a setting, each with a value that setting takes. An unknown * key fails, so a typo doesn't pass for a change. */ export function isUpdateSettingsRequest( obj: unknown, ): obj is UpdateSettingsRequest { if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false; return Object.entries(obj).every(([key, value]) => key in VALIDATORS && VALIDATORS[key as keyof UpdateSettingsRequest](value) ); }