Skip to content

Censkh/zod-meta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zod-meta

Get/set metadata on Zod schemas

Get Started

npm install zod-meta
  1. Create your first meta type, for example a primary key data:
import { createMetaType } from "zod-meta";

export interface PrimaryKeyData {
  autoIncrement?: boolean;
}

export const primaryKey = createMetaType<PrimaryKeydata>({
  id: "primaryKey",
});
  1. Add some meta to your schema:
import * as z from "zod";
import {primaryKey} from "./primaryKey";

const schema = z.object({
  id: z.number().describe(meta([
    primaryKey({ autoIncrement: true })
  ])),
  name: z.string(),
});
  1. Retrieve the meta from the schema:
import { findFieldMetaItem } from "zod-meta";
import { primaryKey } from "./primaryKey";

const primaryKeyField = findFieldMetaItem(schema, primaryKey);

console.log(primaryKeyField.key); // "id"
console.log(primaryKeyField.data.autoIncrement); // true
console.log(primaryKeyField.schema); // ZodNumber

API

getMetaItem<T>(schema: ZodSchema, type: ZodMetaType<T>): ZodMetaItem<T> | undefined

Get a metadata item from a schema

setMetaItem<T>(schema: ZodSchema, type: ZodMetaType<T>, data: T): void

Set a metadata item on a schema

findFieldMetaItem<T>(schema: ZodSchema, type: ZodMetaType<T>): FindFieldMetaResult<T> | undefined

Find the first field in an object that has a metadata item of a certain type

Returns the field schema, the key and the data

findFieldMetaItems<T>(schema: ZodSchema, type: ZodMetaType<T>): FindFieldMetaResult<T>[]

Find all fields in an object that have a metadata item of a certain type

removeMetaItem<T>(schema: ZodSchema, type: ZodMetaType<T>): void

Remove a metadata item from a schema

getMetaStore(schema: ZodSchema): ZodMetaStore

Get the metadata store for a schema which contains all metadata items

ensureMetaStore(schema: ZodSchema): ZodMetaStore

Gets or creates the metadata store for a schema

About

Get/set metadata on Zod schemas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published