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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-mapper

v1.1.0

Published

Tiny library to easily create Zod based data mappers

Downloads

9

Readme

↔️ Zod Mapper

npm version npm downloads License

Tiny library to easily create Zod based data mappers.

Declare the following to get a full typed data with runtime validations 🤩

  • 🛡 Zod schema
  • ➡️ Transform function to map data from EXTERNAL to INTERNAL
  • ⬅️ Transform function to map data from INTERNAL to EXTERNAL

Install

npm i zod-mapper zod

Usage

import { z } from 'zod';
import { createZodMapper } from 'zod-mapper';

const myMapper = createZodMapper({
  // Zod schema for EXTERNAL data
  schema: z.string(),

  // Transform Data from EXTERNAL to INTERNAL
  transformIn: (data) => Number(data),

  // Transform Data from INTERNAL to EXTERNAL
  transformOut: (data) => String(data),
});

// Pass EXTERNAL data to the `in()` method to get INTERNAL formatted data.
const internal = myMapper.in('2') // 2

// Pass INTERNAL data to the `out()` method to get the EXTERNAL formatted data.
const external = myMapper.out(internal) // '2'

Safe transform

If you don't want your mapper to throw errors when transformation fails, use .safeIn and .safeOut instead of in and out. This method returns an object containing either the successfully parsed data or a error containing information about the transformation problems.

myMapper.safeIn('2')
myMapper.safeOut(2)

The result is a discriminated union, so you can handle errors very conveniently

import { z } from 'zod';
import { createZodMapper } from 'zod-mapper';

const myMapper = createZodMapper({
  schema: z.string(),
  transformIn: (data) => Number(data),
  transformOut: (data) => String(data),
});

const internal = myMapper.safeIn('2')
if (!internal.success) {
  // handle error then return
  internal.error
} else {
  // do something
  internal.data
}

Output schema

If the output schema is not same as the input, you can provide a schemaOut.

import { z } from 'zod';
import { createZodMapper } from 'zod-mapper';

const myMapper = createZodMapper({
  schema: z.string(),

  // Specify a different schema for the output
  schemaOut: z.boolean(),

  transformIn: (data) => Number(data),
  transformOut: (data) => Boolean(data),
});

const internal = myMapper.in('2') // 2
const external = myMapper.out(internal) // true

Real life example

import { z } from 'zod';
import { createZodMapper } from 'zod-mapper';

/* Example data */
const exampleFetchedUsersList = [
  {
    firstName: "John",
    lastName: "Doe",
    noSo_good_Label: "something",
  },
  {
    firstName: "Jeanne",
    lastName: "Doe",
  },
]

/*  1. Create a mapper */
const userMapper = createZodMapper({
  // Zod schema for EXTERNAL data
  schema: z.object({
    firstName: z.string(),
    lastName: z.string(),
    noSo_good_Label: z.string().optional(),
  }),

  // Transform Data from EXTERNAL to INTERNAL
  transformIn: ({ noSo_good_Label, ...data }) => ({
    ...data,
    fullName: `${data.firstName} ${data.lastName}`,
    goodLabel: noSo_good_Label,
  }),

  // Transform Data from INTERNAL to EXTERNAL
  transformOut: ({ goodLabel, ...data }) => ({
    ...data,
    noSo_good_Label: goodLabel,
  }),
})

/**
 *  2. Use the mapper to transform the data
 * from the  EXTERNAL format to the INTERNAL format.
 *
 * ℹ️ Each user will HAVE the `fullName` property and the `goodLabel`
 */
const users = exampleFetchedUsersList.map(userMapper.in)

/**
 *  3. Use the mapper to transform the data
 * from the INTERNAL format to the EXTERNAL format.
 *
 * ℹ️ Each user will NOT HAVE the `fullName` property and will HAVE the `noSo_good_Label`
 */
const payload = users.map(userMapper.out)

License

MIT © ivan-dalmet