npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@laysdragon/zod-schema-mapper

v0.1.10

Published

schema type mapper for zod

Downloads

4

Readme

Zod Schema Mapper

zod schema type mapper,its can create new schema base on existed zod schema with condition type tester.

Example

Schema

export const dataSchema = z.object({
  id: z.string(),
  age: z.number(),
  createdAt: z.date(),
  arrayDate: z.date().array(),
  arrayNumber: z.number().array(),
  testUnion: z.number().or(z.date()),
});

export type Data = z.infer<typeof dataSchema>;
// type Data = {
//   id: string;
//   age: number;
//   createdAt: Date;
//   arrayDate: Date[];
//   arrayNumber: number[];
//   testUnion: number | Date;
// }

export const data: Data = {
  age: 1,
  id: "1",
  createdAt: new Date("2024-12-31T10:51:52.587Z"),
  arrayDate: [new Date("2024-12-31T10:51:52.587Z")],
  arrayNumber: [1, 2, 3],
  testUnion: 1,
};

Single Converter

const jsonSchema = convertSchema(
  dataSchema,
  (schema) => schema instanceof z.ZodDate,
  (schema) => schema.transform((value) => value.toISOString())
)
  .convert(
    (schema) => schema instanceof z.ZodNumber,
    (schema) => schema.transform((value) => value.toString())
  )
  .schema();

type Json = z.infer<typeof jsonSchema>;
// type Json = {
//   id: string;
//   age: string;
//   createdAt: string;
//   arrayDate: string[];
//   arrayNumber: string[];
//   testUnion: string;
// }

console.log(jsonSchema.parse(data));
// {
//   id: '1',
//   age: '1',
//   createdAt: '2024-12-31T10:51:52.587Z',
//   arrayDate: [ '2024-12-31T10:51:52.587Z' ],
//   arrayNumber: [ '1', '2', '3' ],
//   testUnion: '1'
// }

Mapper

const jsonMapper = createMapper(
  dataSchema,
  (schema) => schema instanceof z.ZodDate,
  (schema) => schema.transform((value) => value.toISOString()),
  (schema) =>
    z
      .string()
      .datetime()
      .transform((value) => new Date(value))
      .pipe(schema)
)
  .create(
    (schema) => schema instanceof z.ZodNumber,
    (schema) => schema.transform((value) => value.toString()),
    (schema) =>
      z
        .string()
        .transform((value) => Number(value))
        .pipe(schema)
  )
  .mapper();

type Json = z.infer<typeof jsonMapper.encoderSchema>;
// type Json = {
//   id: string;
//   age: string;
//   createdAt: string;
//   arrayDate: string[];
//   arrayNumber: string[];
//   testUnion: string;
// }

const jsonData: Json = {
  age: "1",
  id: "1",
  createdAt: "2024-12-31T10:51:52.587Z",
  arrayDate: ["2024-12-31T10:51:52.587Z"],
  arrayNumber: ["1", "2", "3"],
  testUnion: "1",
};

console.log(jsonMapper.encode(data));
//   {
//     id: '1',
//     age: '1',
//     createdAt: '2024-12-31T10:51:52.587Z',
//     arrayDate: [ '2024-12-31T10:51:52.587Z' ],
//     arrayNumber: [ '1', '2', '3' ],
//     testUnion: '1'
//   }
console.log(jsonMapper.decode(jsonData));
//   {
//     id: '1',
//     age: 1,
//     createdAt: 2024-12-31T10:51:52.587Z,
//     arrayDate: [ 2024-12-31T10:51:52.587Z ],
//     arrayNumber: [ 1, 2, 3 ],
//     testUnion: 1
//   }

custom and instanceof

  • z.custom
  • z.instanceof

~~Since z.custom & z.instanceOf just combined ZodAny with superRefine,doesnt have existed ZodType class. I have no way to indetify their schema in runtime.:(~~

ok,appearent there is workaround. But can only indetify schema base on instance instead of runtime class type or more general way. check example

z.instanceof can replaced with instanceOfClass(ZodInstaceOfClass) class come with package provide some helper functions.
instanceOfClass support private constructor.

example with ZodInstaceOfClass

export class Test {
  name: string | undefined;
  constructor(name: string) {
    this.name = name;
  }
}

export const dataSchema = z.object({
//   value: z.instanceof(Test),
  value: instanceOfClass(Test),
});

export type Data = z.infer<typeof dataSchema>;
// type Data = {
//   value: Test;
// }

export const data: Data = {
  value: new Test("hello world"),
};

const jsonMapper = createMapper(
  dataSchema,
  (schema) => ZodInstaceOfClass.isSchema(Test, schema),
  (schema) => schema.transform((value) => value.name),
  (schema) =>
    z
      .string()
      .transform((value) => new Test(value))
      .pipe(schema)
).mapper();

type Json = z.infer<typeof jsonMapper.encoderSchema>;
// type Json = {
//   value: string | undefined;
// }

const jsonData: Json = {
  value: "hello world",
};

console.log(jsonMapper.encode(data));
// { value: 'hello world' }
console.log(jsonMapper.decode(jsonData));
// { value: Test { name: 'hello world' } }

example with zod schema

const testSchema = z.instanceof(Test);

export const dataSchema = z.object({
  //   value: z.instanceof(Test),
  value: testSchema,
});

export type Data = z.infer<typeof dataSchema>;
// type Data = {
//   value: Test;
// }

export const data: Data = {
  value: new Test("hello world"),
};

const jsonMapper = createMapper(
  dataSchema,
  (schema) => isSchema(testSchema, schema), // type prediction helper method come with package
  (schema) => schema.transform((value) => value.name),
  (schema) =>
    z
      .string()
      .transform((value) => new Test(value))
      .pipe(schema)
).mapper();

Support Type

  • [x] ZodString

  • [x] ZodNumber

  • [x] ZodNaN

  • [x] ZodBigInt

  • [x] ZodBoolean

  • [x] ZodDate

  • [x] ZodLiteral

  • [x] ZodNull

  • [x] ZodEnum

  • [x] ZodNativeEnum

  • [x] ZodUndefined

  • [x] ZodVoid

  • [x] ZodObject

  • [x] ZodArray

  • [x] ZodUnion

  • [x] ZodNullable

  • [x] ZodOptional

WIP

  • [ ] ZodLazy
  • [ ] ZodPromise
  • [ ] ZodFunction
  • [ ] ZodMap
  • [ ] ZodSet
  • [ ] ZodRecord
  • [ ] ~~ZodNever~~
  • [ ] ZodTuple