zod-metadata
Metadata support for Zod schemas.
Install
npm install zod-metadata
Register the Zod extension
Automatic
Import zod-metadata/register
at the top of your entry files:
JavaScript
require('zod-metadata/register');
TypeScript
import 'zod-metadata/register';
Manual
JavaScript
const { register } = require('zod-metadata');
const zod = require('zod');
register(zod);
TypeScript
import { register } from 'zod-metadata';
import zod from 'zod';
register(zod);
Preload
JavaScript
node -r zod-metadata/register my-script.js
require('zod-metadata');
TypeScript
ts-node -r zod-metadata/register my-script.js
import {} from 'zod-metadata';
API
The API provices two methods that will read and write to schema._def.meta
.
schema.meta(meta: Record<string, unknown>): this
Accumulates metadata mutating the schema:
schema
.meta({ key1: value1 })
.meta({ key2: value2 });
schema.getMeta(): Record<string, unknown>
Returns the metadata:
schema.getMeta();