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

@orbit-utils/zod-to-model-definition

v0.1.0

Published

Define your RecordSchema using Zod.

Downloads

1

Readme

@orbit-utils/zod-to-model-definition

Define your RecordSchema using Zod.

Before:

const planetModelDefinition = {
    attributes: {
        name: {type: "string"},
        classification: {type: "string"},
        atmosphere: {type: "boolean"},
        mass: {type: "number"},
    },
    relationships: {
        solarSystem: {kind: "hasOne", type: "solarSystem", inverse: "planets"},
        moons: {kind: "hasMany", type: "moon", inverse: "planet"},
    },
};

After:

import {m, zodToModelDefinition} from "@orbit-utils/zod-to-model-definition";
import {z} from "zod";

const planetZodSchema = z.object({
    type: z.literal("planet"),
    id: z.string().optional(),
    attributes: z.object({
        name: z.string(),
        classification: z.enum(["terrestrial", "gaseous"]),
        atmosphere: z.boolean(),
        mass: z.number().positive().finite(),
    }),
    relationships: z.object({
        solarSystem: m.relationship("hasOne", "solarSystem").inverse("planets"),
        moons: m.relationship("hasMany", "moon").inverse("planet"),
    }),
});
const planetModelDefinition = zodToModelDefinition(planetZodSchema);

Usage

Install using your preferred Node.js package manager. For example:

pnpm add @orbit-utils/zod-to-model-definition

Import and enjoy!

import {zodToModelDefinition} from "@orbit-utils/zod-to-model-definition";

For facilitating special model definition attributes (such as inverse, dependent: "delete" and meta) in your Zod schema, the library provides custom ZodTypes through m:

import {m} from "@orbit-utils/zod-to-model-definition";

m.relationship("hasOne", "solarSystem").inverse("planets");
m.relationship("hasMany", "moon").inverse("planet");
m.attribute(z.number().positive().finite()).definitionMeta({unit: "kg"});

Advantages of using Zod to define your RecordSchema

  • You can infer TypeScript types from your Zod schema.
    type Planet = z.infer<typeof planetZodSchema>;
    // same as…
    interface Planet extends UninitializedRecord {
        type: "planet";
        ...
    }
    // …except you don't have to maintain separate interface definitions
  • Zod allows for a more granular schema definition (more granular types, more granular validation).
    classification: {
        type: "string";
    }
    vs.z.enum(["terrestrial", "gaseous"]);
    mass: {
        type: "number";
    }
    vs.z.number().positive().finite();
    • You can configure Orbit to use Zod's validation instead of its own with validateFor.

Additional features

API reference

zodToModelDefinition

import type {ModelDefinition} from "@orbit/records";
import type {AnyZodObject} from "zod";

export function zodToModelDefinition(zodSchema: AnyZodObject): ModelDefinition;

ZodKey

Extends ZodType.

Zod schema counterpart for KeyDefinition.

Can be created by calling m.key.

import type {ZodType} from "zod";

export function key(zodType?: ZodType<string>): ZodKey;

.definitionMeta

ZodKey.definitionMeta: (definitionMeta: {[key: string]: unknown}) => ZodKey

ZodAttribute

Extends ZodType.

Zod schema counterpart for AttributeDefinition.

Can be created by calling m.attribute.

import type {ZodTypeAny} from "zod";

export function attribute(zodType?: ZodTypeAny): ZodAttribute;

.definitionMeta

ZodAttribute.definitionMeta: (definitionMeta: {[key: string]: unknown}) => ZodAttribute

.serialization

ZodAttribute.serialization: (serialization: {[key: string]: unknown}) => ZodAttribute

.deserialization

ZodAttribute.deserialization: (deserialization: {[key: string]: unknown}) => ZodAttribute

ZodRelationship

Extends ZodType.

Zod schema counterpart for RelationshipDefinition.

Can be created by calling m.relationship.

import type {ZodTypeAny} from "zod";

export function relationship(
    kind: "hasOne" | "hasMany",
    type?: string | string[],
): ZodRelationship;

.definitionMeta

ZodRelationship.definitionMeta: (definitionMeta: {[key: string]: unknown}) => ZodRelationship

.inverse

ZodRelationship.inverse: (relationshipName: string) => ZodRelationship

.dependentRemove

ZodRelationship.dependentRemove: () => ZodRelationship

.links

Schema for the links object that can appear on your record. (See RecordRelationship.)

import type {Link} from "@orbit/records";
import type {ZodType} from "zod";

ZodRelationship.links: (schema: ZodType<{[key: string]: Link} | undefined>) => ZodRelationship

.meta

Schema for the meta object that can appear on your record. (See RecordRelationship.)

import type {ZodType} from "zod";

ZodRelationship.meta: (schema: ZodType<{[key: string]: unknown} | undefined>) => ZodRelationship