-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed mod.ts entrypoint, refacotred zod utils
- Loading branch information
1 parent
84d0327
commit d1c1055
Showing
2 changed files
with
23 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
/** | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
} |