3.2 KiB
Register Default Strategies — Schema
(no submodules)
Purpose
Zod schema for the manager argument of registerDefaultStrategies. Validates that an external caller hands the function a real PackingStrategyManager instance (not a plain object that "quacks like" one) so the chain ordering invariants the function depends on are guaranteed at the boundary. Identity is preserved — the parsed value is the same instance the caller passed in, so registerDefaultStrategies(manager) mutates the caller's manager rather than a clone.
This package's strategy classes are themselves parameterless and therefore expose no per-strategy options to validate. The single boundary that needs a schema is registerDefaultStrategies(manager). Strategy shape (canHandle/pack/unpack are functions) is already validated upstream by packingStrategySchema in @suaveplan/bitpacker.
Features
packingStrategyManagerSchema—z.custom<PackingStrategyManager>instanceofcheck- Identity preservation (no wrapping)
- Rejects null, undefined, and plain objects that share the manager API surface
Usage: Basic Example
import {
PackingStrategyManager,
BitPackerError,
BITPACKER_CODES,
} from "@suaveplan/bitpacker";
import {
packingStrategyManagerSchema,
registerDefaultStrategies,
} from "@suaveplan/bitpacker-strategies";
const manager = packingStrategyManagerSchema.parse(new PackingStrategyManager());
registerDefaultStrategies(manager);
Usage: Advanced Example
import {
PackingStrategyManager,
BitPackerError,
BITPACKER_CODES,
} from "@suaveplan/bitpacker";
import {
packingStrategyManagerSchema,
registerDefaultStrategies,
} from "@suaveplan/bitpacker-strategies";
function bootstrap(rawManager: unknown): PackingStrategyManager {
const result = packingStrategyManagerSchema.safeParse(rawManager);
if (!result.success) {
throw new BitPackerError(
BITPACKER_CODES.INVALID_SCHEMA,
"registerDefaultStrategies received a non-manager value",
{ cause: result.error }
);
}
registerDefaultStrategies(result.data);
return result.data;
}
API Reference
packingStrategyManagerSchema: z.ZodType<PackingStrategyManager>
Strict instanceof validator. Returns the input value unchanged on success; throws ZodError with the message "manager must be a PackingStrategyManager instance" on failure.
Implementation Notes
- No per-strategy schemas. Every shipped strategy in this package is parameterless —
new ArrayPackingStrategy(),new ObjectPackingStrategy(), etc. There is nothing per-strategy to validate at construction time. Strategy shape (canHandle/pack/unpack) is enforced bypackingStrategySchemain@suaveplan/bitpackerat the registration boundary, so this package does not duplicate that validation. instanceofover duck-typing. A plain object withadd/registermethods would compile, but the order-sensitive insertion logic inPackingStrategyManager.register(insert before catch-all) depends on the class internals. Refusing duck-types prevents subtle ordering bugs that would only surface deep into a pack/unpack chain.