zod-metadata
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

zod-metadata

Metadata support for Zod schemas.

Installation

npm install zod-metadata
yarn add zod-metadata
pnpm add zod-metadata

Basic Usage

import 'zod-metadata/register';
import { z } from 'zod';

const schema = z.string().meta({
  example: 'John',
  number: 42,
});

schema.getMeta(); // => { example: 'John', number: 42 }

Registration

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
// my-script.js
require('zod-metadata'); // make type declarations available

TypeScript

ts-node -r zod-metadata/register my-script.js
// my-script.ts
import {} from 'zod-metadata'; // make type declarations available

Via NODE_OPTIONS

NODE_OPTIONS='-r zod-metadata/register' node my-script.js

Specifying the metadata shape

Since you can re-declare an interface to add stuff to it, you can redeclare the ZodMeta interface to add fields to it. This may be helpful if you have handful, pre-defined keys you're going to use project-wide.

Overwritting the zod metadata interface

Package Sidebar

Install

npm i zod-metadata

Weekly Downloads

12,352

Version

1.1.1

License

MIT

Unpacked Size

5.96 kB

Total Files

7

Last publish

Collaborators

  • marred