Skip to content

Commit

Permalink
New type assertion function, largely from current GH Issues (#33)
Browse files Browse the repository at this point in the history
* new type assertion function, from /zodix issues

* use scoreless parse
  • Loading branch information
abelsj60 authored May 30, 2023
1 parent 515bd9c commit 2d15ee3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z, ZodType } from 'zod';
import { z } from 'zod';
import { createErrorResponse } from './errors';
import type { LoaderArgs } from '@remix-run/server-runtime';
import type {
Expand All @@ -20,6 +20,14 @@ type Options<Parser = SearchParamsParser> = {
parser?: Parser;
};

/**
* Type assertion function avoids problems with some bundlers when
* using `instanceof` to check the type of a `schema` param.
*/
const isZodType = (input: ZodRawShape | ZodTypeAny): input is ZodTypeAny => {
return typeof input.parse === 'function';
}

/**
* Generic return type for parseX functions.
*/
Expand Down Expand Up @@ -50,7 +58,7 @@ export function parseParams<T extends ZodRawShape | ZodTypeAny>(
options?: Options
): ParsedData<T> {
try {
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.parse(params);
} catch (error) {
throw createErrorResponse(options);
Expand All @@ -67,7 +75,7 @@ export function parseParamsSafe<T extends ZodRawShape | ZodTypeAny>(
params: Params,
schema: T
): SafeParsedData<T> {
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.safeParse(params) as SafeParsedData<T>;
}

Expand All @@ -87,7 +95,7 @@ export function parseQuery<T extends ZodRawShape | ZodTypeAny>(
? request
: getSearchParamsFromRequest(request);
const params = parseSearchParams(searchParams, options?.parser);
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.parse(params);
} catch (error) {
throw createErrorResponse(options);
Expand All @@ -109,7 +117,7 @@ export function parseQuerySafe<T extends ZodRawShape | ZodTypeAny>(
? request
: getSearchParamsFromRequest(request);
const params = parseSearchParams(searchParams, options?.parser);
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.safeParse(params) as SafeParsedData<T>;
}

Expand All @@ -132,7 +140,7 @@ export async function parseForm<
? request
: await request.clone().formData();
const data = await parseFormData(formData, options?.parser);
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.parseAsync(data);
} catch (error) {
throw createErrorResponse(options);
Expand All @@ -157,7 +165,7 @@ export async function parseFormSafe<
? request
: await request.clone().formData();
const data = await parseFormData(formData, options?.parser);
const finalSchema = schema instanceof ZodType ? schema : z.object(schema);
const finalSchema = isZodType(schema) ? schema : z.object(schema);
return finalSchema.safeParseAsync(data) as Promise<SafeParsedData<T>>;
}

Expand Down

0 comments on commit 2d15ee3

Please sign in to comment.