dryvPacks

@dryv/zodv0.1.0

zod schemas with field limits, formats and invariants.

Zod schemasValidationInjectPublished 7 days ago2 downloads

dryv pack add @dryv/zod
1.5 KBRaw ↗

Zod schemas pack

Generates export const XSchema = z.object({...}) and export type X = z.infer<typeof XSchema> for every schema, and z.enum([...]) for every enum.

IRGenerated
string formats.uuid(), .ulid(), .email(), .url(), .ip({ version }), .base64(), and regexes for hostname, loose phone, strict e164, slug, hex-color, hex, jwt, semver, country-code, currency-code
length, pattern match.length() / .min() / .max(), .regex(new RegExp(...))
numbersz.number() with .int(), .min(), .max(), .multipleOf()
temporalz.string().date(), .time(), or .datetime({ offset: true })
nested schemasz.lazy(() => OtherSchema), so order and cycles don't matter
arraysz.array(...) with .min() / .max()
nullable, default, optional.nullable(), .default(value) (which also makes the input optional), .optional()
schema invariants.superRefine(...) adding an issue at the first field the rule reads, with the invariant's message

Provides: schema.validation (the XSchema constants). The inferred types are exported too, but a slot carries one symbol, so the pack doesn't claim schema.types.

Test: bun scripts/test-pack.ts inject/validation/zod renders the registration fixture, typechecks it, and parses valid and invalid samples.

Use it

The executable Usage fixture shows the current source collection, destination and pack activation for this pack.

Slots

  • provides schema.validation