Files
sendou.ink/app/utils/action-schemas.ts
2026-08-21 17:36:48 +03:00

27 lines
847 B
TypeScript

import type * as v from "valibot";
import type { AnySchema } from "./schema";
/** `_action` literals of an action schema (union or single branch). */
export type ActionsOf<TSchema extends AnySchema> =
v.InferOutput<TSchema> extends {
_action: infer TAction extends string;
}
? TAction
: never;
/** Non-`_action` fields of the schema branch matching the given action, in parsed types. */
export type FieldsOf<TSchema extends AnySchema, TAction extends string> = Omit<
Extract<v.InferOutput<TSchema>, { _action: TAction }>,
"_action"
>;
/** Serializes a parsed field value into its form data representation. */
export function serializeFieldValue(value: unknown) {
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean") {
return String(value);
}
return JSON.stringify(value);
}