@evrdk/utils
v0.0.13
Published
Shared browser and server utilities for EVR projects.
Downloads
2,014
Readme
@evrdk/utils
Shared ESM utilities for schema-validated functions, with runtime-neutral, browser, and server entry points.
Installation
bun add @evrdk/utilsInstall a Standard Schema library such as Zod, Valibot, or ArkType to define schemas. The TypeBox adapter needs the
optional @sinclair/typebox peer.
Features
- Typed input and output validation through Standard Schema V1.
- Synchronous or asynchronous handlers and validators, always returning a promise.
- Schema transformations, defaults, and structured validation errors.
- Lazy TypeBox/Elysia adapter with separate runtime entry points.
Usage
With Zod installed (bun add zod):
import { createValidatedFn } from "@evrdk/utils";
import { z } from "zod";
const double = createValidatedFn({
input: z.object({ x: z.number() }),
output: z.object({ result: z.number() }),
handler: ({ x }) => ({ result: x * 2 })
});
const result = await double({ x: 5 }); // { result: 10 }For TypeBox, install bun add @sinclair/typebox and adapt schemas before passing them to createValidatedFn:
import { typeboxSchema } from "@evrdk/utils/typebox";
import { Type } from "@sinclair/typebox";
const input = typeboxSchema(Type.Object({ x: Type.Number() }));The adapter also accepts Elysia's t schemas. Import elysia when using its custom types or formats to initialize
its registry.
API and configuration
| API or entry point | Purpose |
| ----------------------------------------------- | ------------------------------------------------------------------------------------ |
| createValidatedFn({ input, output, handler }) | Validate input, run the handler, then validate output; returns an async function. |
| SchemaValidationError | Validation failure with phase ("input" or "output") and structured issues. |
| typeboxSchema(schema) | Adapt TypeBox schemas to Standard Schema; also exported from @evrdk/utils/typebox. |
| @evrdk/utils | Runtime-neutral exports of the three APIs above. |
| @evrdk/utils/browser, /server | Same validation APIs plus browserLog or serverLog. |
The handler receives the input schema's transformed output and returns the output schema's input. Callers receive the validated, transformed output. Handler and validator exceptions propagate unchanged.
TypeBox defaults apply to a clone, leaving caller data unchanged. Coercion must be defined by a schema transformation; there is no blanket conversion. Each adapter compiles its validator once on first validation.
Development
See AGENTS.md for runtime boundaries, adapter invariants, and type/behavior tests.
