zod-metadata
Advanced tools
Comparing version 1.0.3 to 1.1.0
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 } | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5485
43
88