Skip to content

Commit

Permalink
removed mod.ts entrypoint, refacotred zod utils
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-oloughlin committed Nov 14, 2023
1 parent 84d0327 commit d1c1055
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 75 deletions.
6 changes: 0 additions & 6 deletions mod.ts

This file was deleted.

92 changes: 23 additions & 69 deletions zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,30 @@ import { z } from "https://deno.land/x/[email protected]/mod.ts"
* @param schema
* @returns
*/
export function parseSearchParams<const T extends z.ZodType>(
export function safeParseSearchParams<T extends z.ZodObject<z.ZodRawShape>>(
url: string | URL,
schema: T,
): z.SafeParseReturnType<z.infer<T>, z.infer<T>> {
const searchParams = Object.fromEntries(new URL(url).searchParams.entries())
return schema.safeParse(searchParams)
}

/**
* Parse search parameters from a url using a zod schema.
*
* @param url
* @param schema
* @returns
*/
export async function parseSearchParamsAsync<const T extends z.ZodType>(
url: string | URL,
schema: T,
): Promise<z.SafeParseReturnType<z.infer<T>, z.infer<T>>> {
const searchParams = Object.fromEntries(new URL(url).searchParams.entries())
return await schema.safeParseAsync(searchParams)
}

/**
* Coerce and parse a Zod schema of any primitive type.
*
* @param value - Value to be coerced and parsed according to schema.
* @param schema - Primitive Zod schema.
* @returns A parse result or error.
*/
export function parseCoercedPrimitive<
const T extends number | boolean | bigint | string | Date,
>(
value: unknown,
schema: z.ZodType<T>,
): z.SafeParseSuccess<T> | { success: false } {
// Get schema type
const type = typeof schema._type
const searchParams = Object.fromEntries(new URL(url).searchParams.entries())

// Select appropriate coerce schema
const coerceSchema = type === "number"
? z.coerce.number()
: type === "boolean"
? z.coerce.boolean()
: type === "bigint"
? z.coerce.bigint()
: type === "string"
? z.coerce.string()
: schema._type instanceof Date
? z.coerce.date()
: null
const coercedEntries = Object
.entries(searchParams)
.map(([key, value]) =>
[key, safeCoercePrimitive(value, schema.shape[key])] as const
)
.map(([key, value]) => [key, value.success ? value.data : null] as const)

// If no coerce schema selected, return error
if (!coerceSchema) {
if (coercedEntries.some(([_, value]) => value === null)) {
return {
success: false,
}
}

// Parse using coerce schema
const coerceParsed = coerceSchema.safeParse(value)
const coercedSearchParams = Object.fromEntries(coercedEntries)

// Return error if not successful
if (!coerceParsed.success) {
return coerceParsed
return schema.safeParse(coercedSearchParams) as z.SafeParseSuccess<T> | {
success: false
}

// Return parse result of schema on coerced data
return schema.safeParse(coerceParsed.data)
}

/**
Expand All @@ -85,25 +40,24 @@ export function parseCoercedPrimitive<
* @param schema - Primitive Zod schema.
* @returns A parse result or error.
*/
export async function parseCoercedPrimitiveAsync<
export function safeCoercePrimitive<
const T extends number | boolean | bigint | string | Date,
>(
value: unknown,
schema: z.ZodType<T>,
): Promise<z.SafeParseSuccess<T> | { success: false }> {
// Get schema type
const type = typeof schema._type
): z.SafeParseSuccess<T> | { success: false } {
// deno-lint-ignore no-explicit-any
const schemaType = (schema._def as any).typeName

// Select appropriate coerce schema
const coerceSchema = type === "number"
const coerceSchema = schemaType === "ZodNumber"
? z.coerce.number()
: type === "boolean"
: schemaType === "ZodBoolean"
? z.coerce.boolean()
: type === "bigint"
: schemaType === "ZodBigInt"
? z.coerce.bigint()
: type === "string"
: schemaType === "ZodString"
? z.coerce.string()
: schema._type instanceof Date
: schemaType === "ZodDate"
? z.coerce.date()
: null

Expand All @@ -115,13 +69,13 @@ export async function parseCoercedPrimitiveAsync<
}

// Parse using coerce schema
const coerceParsed = await coerceSchema.safeParseAsync(value)
const coerceParsed = coerceSchema.safeParse(value)

// Return error if not successful
if (!coerceParsed.success) {
return coerceParsed
}

// Return parse result of schema on coerced data
return await schema.safeParseAsync(coerceParsed.data)
return schema.safeParse(coerceParsed.data)
}

0 comments on commit d1c1055

Please sign in to comment.