Files
skills/docs/foundation/bitpacker/bitpacker-compressed/src/compressed-bit-packer/compressed-bit-packer.schema.md
2026-05-29 08:20:56 +01:00

2.9 KiB

Compressed BitPacker Options Schema

Purpose

Zod schema for CompressedBitPackerOptions, the constructor options bundle accepted by CompressedBitPacker. Validates the shape at the package boundary so a typo in the algorithm name (alogrithm), a non-CompressionRegistry value passed as the registry (a plain object that "quacks like" one), or a stray unknown key all fail at parse time rather than producing a packer that silently runs without the consumer's intended compression configuration.

The schema composes compressionConfigSchema for the compression field and uses an instanceof check for compressionRegistry. Identity of the registry is preserved so the constructor receives the same instance the caller built.

Features

  • compressedBitPackerOptionsSchema — strict, two optional fields (compression, compressionRegistry)
  • Composes compressionConfigSchema for nested validation
  • instanceof CompressionRegistry check with identity preservation
  • as unknown as z.ZodType<T> cast for the EOPT mismatch

Usage: Basic Example

import {
    CompressedBitPacker,
    compressedBitPackerOptionsSchema,
} from "@suaveplan/bitpacker-compressed";

const opts = compressedBitPackerOptionsSchema.parse({
    compression: { algorithm: "gzip", enabled: true },
});
const packer = new CompressedBitPacker({ x: 255 }, opts);

Usage: Advanced Example

import {
    CompressedBitPacker,
    compressedBitPackerOptionsSchema,
    createCompressionRegistry,
    GzipCompressionAdapter,
} from "@suaveplan/bitpacker-compressed";

const registry = createCompressionRegistry();
registry.register(new GzipCompressionAdapter());

const opts = compressedBitPackerOptionsSchema.parse({
    compression: {
        algorithm: "gzip",
        options: { level: 9 },
        enabled: true,
    },
    compressionRegistry: registry,
});

const packer = new CompressedBitPacker({ x: 255 }, opts);

API Reference

compressedBitPackerOptionsSchema: z.ZodType<CompressedBitPackerOptions>

Strict schema. compression is an optional CompressionConfig (validated by compressionConfigSchema). compressionRegistry is an optional CompressionRegistry instance (validated by instanceof with identity preservation).

Implementation Notes

  • Composition over duplication. The nested compression field is validated by compressionConfigSchema from the same package. Composing rather than inlining the shape keeps the per-field error messages consistent across all entry points that accept a CompressionConfig.
  • instanceof over duck-typing. A duck-typed registry would compile but would not pass the prototype-aware checks the registry's lookup methods perform internally. Refusing duck-types prevents an entire class of "registry doesn't find my adapter" bugs.
  • .strict() is non-negotiable. Same rationale as elsewhere in this change: typo'd flags would otherwise silently default and hide configuration bugs.