Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zod-metadata

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-metadata - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

19

dist/index.d.ts
import z from 'zod';
declare module 'zod' {
interface ZodMeta {
[k: string | number | symbol]: unknown;
}
interface ZodTypeDef {
meta?: Record<string, unknown>;
meta?: ZodMeta;
}
interface ZodType {
getMeta(): Record<string, unknown>;
meta(meta: Record<string, unknown>): this;
interface ZodType<Output = any, Def extends z.ZodTypeDef = z.ZodTypeDef, Input = Output> {
getMeta(): this['_def'] extends {
meta: infer M;
} ? M : ZodMeta | undefined;
meta<T extends ZodMeta = ZodMeta>(meta: T): ZodType<Output, Def extends {
meta: infer M;
} ? Def & {
meta: M & T;
} : Def & {
meta: T;
}, Input>;
}
}
export declare function register(zod: typeof z): void;
{
"name": "zod-metadata",
"version": "1.0.3",
"version": "1.1.0",
"description": "Extends Zod with metadata",

@@ -30,3 +30,3 @@ "main": "dist/index.js",

"build": "npm run clean && tsc -p tsconfig.build.json",
"format": "prettier \"**/*.[jt]s\" --write",
"format": "prettier \"**/*.{js,ts,md}\" --write",
"lint": "eslint . --ext .ts --fix",

@@ -33,0 +33,0 @@ "test": "jest",

# zod-metadata
Metadata support for [Zod](https://www.npmjs.com/package/zod) schemas.
## Install
## Installation
```bash
npm install zod-metadata
yarn add zod-metadata
pnpn add zod-metadata
```
## Register the Zod extension
## Basic Usage
```typescript
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**
```javascript

@@ -20,2 +40,3 @@ require('zod-metadata/register');

**TypeScript**
```typescript

@@ -26,3 +47,5 @@ import 'zod-metadata/register';

### Manual
**JavaScript**
```javascript

@@ -36,2 +59,3 @@ const { register } = require('zod-metadata');

**TypeScript**
```typescript

@@ -45,3 +69,5 @@ import { register } from 'zod-metadata';

### Preload
**JavaScript**
```bash

@@ -57,2 +83,3 @@ node -r zod-metadata/register my-script.js

**TypeScript**
```bash

@@ -66,20 +93,1 @@ ts-node -r zod-metadata/register my-script.js

```
## 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:
```javascript
schema
.meta({ key1: value1 })
.meta({ key2: value2 });
```
### `schema.getMeta(): Record<string, unknown>`
Returns the metadata:
```javascript
schema.getMeta(); // => { key1: value1, key2: value2 }
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc