36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
|
* Checks the shape of the category request bodies, for the category routes to
|
|
* answer a 400. The category service checks the slug and name themselves.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type {
|
|
CreateCategoryRequest,
|
|
UpdateCategoryRequest,
|
|
} from "../../shared/category.ts";
|
|
|
|
/** Checks whether `obj` has the fields of a {@link CreateCategoryRequest}. */
|
|
export function isCreateCategoryRequest(
|
|
obj: unknown,
|
|
): obj is CreateCategoryRequest {
|
|
if (!obj || typeof obj !== "object") return false;
|
|
const o = obj as Record<string, unknown>;
|
|
if (typeof o.slug !== "string") return false;
|
|
if (typeof o.name !== "string") return false;
|
|
if ("position" in o && typeof o.position !== "number") return false;
|
|
return true;
|
|
}
|
|
|
|
/** Checks whether `obj` has the fields of an {@link UpdateCategoryRequest}. */
|
|
export function isUpdateCategoryRequest(
|
|
obj: unknown,
|
|
): obj is UpdateCategoryRequest {
|
|
if (!obj || typeof obj !== "object") return false;
|
|
const o = obj as Record<string, unknown>;
|
|
if ("slug" in o && typeof o.slug !== "string") return false;
|
|
if ("name" in o && typeof o.name !== "string") return false;
|
|
if ("position" in o && typeof o.position !== "number") return false;
|
|
return true;
|
|
}
|