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

zodkit

v0.3.1

Published

Zod utilities for SvelteKit

Downloads

2

Readme

Zodkit

Zodkit is a collection of Zod utilities for SvelteKit actions, load functions, hooks and endpoints. It abstracts the complexity of parsing and validating FormData, URLSearchParams and RouteParams so they stay clean and are strongly typed. It is heavily based on Zodix by Riley Tomasek

Table of Contents

Installing

Using npm:

$ npm install zodkit zod

Using yarn:

$ yarn add zodkit zod

Using pnpm:

$ pnpm add zodkit zod

Usage

You can either import the zk object that contains all of the functions

import { zk } from 'zodkit';

or import the functions seperately

import {
    parseSearchParams,
    parseSearchParamsSafe,
    parseFormData,
    parseFormDataSafe,
    parseRouteParams,
    parseRouteParamsSafe,
} from 'zodkit';

Functions

parseSearchParams

parseSearchParams(data: URLSearchParams | RequestEvent, schema: Schema)

Parses and validates URLSearchParams. If the parsing/validation fails a 400 error will be thrown with the errors from zod, otherwise the parsed data from the schema will be returned.

import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const GET = ((event) => {
    const { myNumber } = zk.parseSearchParams(event, { myNumber: z.number({ coerce: true }) });
    return new Response(String(myNumber));
}) satisfies RequestHandler;

parseSearchParamsSafe

parseSearchParamsSafe(data: URLSearchParams | RequestEvent, schema: Schema)

Parses and validates URLSearchParams using .safeParse. If the parsing/validation fails a SafeParseFailure object will be returned, otherwise an object in the shape { success: true; data: T; } will be returned

import { fail } from '@sveltejs/kit';
import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const GET = ((event) => {
    const result = zk.parseSearchParamsSafe(event, { myNumber: z.number({ coerce: true }) });

    if (!result.success) {
        return fail(400, {
            errors: result.error.flatten().fieldErrors,
        });
    }

    return new Response(String(result.data.myNumber));
}) satisfies RequestHandler;

parseFormData

parseFormData(data: FormData | RequestEvent, schema: Schema)

Parses and validates FormData. If the parsing/validation fails a 400 error will be thrown with the errors from zod, otherwise the parsed data from the schema will be returned.

import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const POST = (async (event) => {
    const { myNumber } = await zk.parseFormData(event, { myNumber: z.number({ coerce: true }) });
    return new Response(String(myNumber));
}) satisfies RequestHandler;

parseFormDataSafe

async parseFormDataSafe(data: FormData | RequestEvent, schema: Schema)

Parses and validates FormData using .safeParse. If the parsing/validation fails a SafeParseFailure object will be returned, otherwise an object in the shape { success: true; data: T; } will be returned

import { fail } from '@sveltejs/kit';
import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const POST = (async (event) => {
    const result = await zk.parseFormDataSafe(event, { myNumber: z.number({ coerce: true }) });

    if (!result.success) {
        return fail(400, {
            errors: result.error.flatten().fieldErrors,
        });
    }

    return new Response(String(result.data.myNumber));
}) satisfies RequestHandler;

parseRouteParams

parseRouteParams(data: RouteParams | RequestEvent, schema: Schema)

Parses and validates RouteParams from event.params. If the parsing/validation fails a 400 error will be thrown with the errors from zod, otherwise the parsed data from the schema will be returned.

import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const GET = ((event) => {
    const { myNumber } = zk.parseRouteParams(event, { myNumber: z.number({ coerce: true }) });
    return new Response(String(myNumber));
}) satisfies RequestHandler;

parseRouteParamsSafe

parseRouteParamsSafe(data: RouteParams | RequestEvent, schema: Schema)

Parses and validates RouteParams from event.params using .safeParse. If the parsing/validation fails a SafeParseFailure object will be returned, otherwise an object in the shape { success: true; data: T; } will be returned

import { fail } from '@sveltejs/kit';
import type { RequestHandler } from './$types.js';
import { zk } from 'zodkit';
import { z } from 'zod';

export const GET = ((event) => {
    const result = zk.parseRouteParamsSafe(event, { myNumber: z.number({ coerce: true }) });

    if (!result.success) {
        return fail(400, {
            errors: result.error.flatten().fieldErrors,
        });
    }

    return new Response(String(result.data.myNumber));
}) satisfies RequestHandler;

Types

Schema

Schema is equal to ZodTypeAny | ZodRawShape; which allows us to pass in both a Zod schema:

const schema: Schema = z.object({ a: z.number(), b: z.string() });

and regular objects

const schema: Schema = { a: z.number(), b: z.string() };

RouteParams

RouteParams is equal to Partial<Record<string, string>>. It is the same format that RequestEvent.params is.

SafeParseFailure

SafeParseFailure has the shape

{
    success: false;
    errors: { [key: string] : string[] };
    response: ActionFailure<{ errors: [key: string] : string[] }>;
}

The response property can be used to return errors from a form action.

import { z } from 'zod';
import { zk } from 'zodkit';
import type { Actions } from './$types';

export const actions = {
    default: async (event) => {
        const result = await zk.parseFormDataSafe(event, {
            name: z.string().min(5);
        });

        if (!result.success) {
            return result.response
        }
    }
} satisfies Actions;