import type { StandardSchemaV1 } from "@standard-schema/spec"; export type Maybe = T | unknown; export type MaybePromise = T | Promise; export type Shape = StandardSchemaV1; export type InferShapeInput, Fallback = unknown> = S extends Shape ? I : Fallback; export type InferShapeOutput, Fallback = unknown> = S extends Shape ? O : Fallback; export type MaybeShapeArgs> = S extends Shape ? [ input: InferShapeInput, ] : []; export type MaybeShapeListener> = S extends Shape ? (input: InferShapeOutput) => void : () => void; export interface ShapeValidResult { valid: true; data: unknown; } export interface ShapeInvalidResult { valid: false; issues: readonly StandardSchemaV1.Issue[]; } export type ShapeValidateResult = ShapeValidResult | ShapeInvalidResult; export const validateShape = async (shape?: Maybe, data?: unknown): Promise => { if (!shape) { return { valid: true, data, }; } const result = await (shape as StandardSchemaV1)["~standard"].validate(data); if (result.issues) { return { valid: false, issues: result.issues, }; } return { valid: true, data: result.value, }; };