Skip to main content
Module

x/zod_utilz/mod.ts>zu.useURLSearchParams

Framework agnostic utilities for Zod
Go to Latest
function zu.useURLSearchParams
import { zu } from "https://deno.land/x/zod_utilz@0.4.0/mod.ts";
const { useURLSearchParams } = zu;

A way to parse URLSearchParams

Usage:

import { zu } from 'zod_utilz'
const schema = zu.useURLSearchParams(
    z.object( {
        string: z.string(),
        number: z.number(),
        boolean: z.boolean(),
    } )
)

zu.SPR( schema.safeParse(
    new URLSearchParams( {
        string: 'foo',
        number: '42',
        boolean: 'false',
    } )
) ).data
// { string: 'foo', number: 42, boolean: false }

zu.SPR( schema.safeParse(
    new URLSearchParams( {
        string: '42',
        number: 'false',
        boolean: 'foo',
    } )
) ).error?.format()
// {
//     formErrors: [],
//     fieldErrors: {
//         string: [ 'Expected string, received number' ],
//         number: [ 'Expected number, received boolean' ],
//         boolean: [ 'Expected boolean, received string' ],
//     }
// }

Type Parameters

Schema extends z.ZodObject<z.ZodRawShape>