Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an alternative mechanism for checking ZodType in parsers #32

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type SafeParsedData<T extends ZodRawShape | ZodTypeAny> = T extends ZodTypeAny
? SafeParseReturnType<ZodObject<T>, ParsedData<T>>
: never;

/**
* A Type Guard to check object is a ZodType.
* `instanceof` is not always reliable when bundled.
* `parse` is the only method we require to determine it is a Zod object.
*/
const isZodType = (input: ZodRawShape | ZodTypeAny): input is ZodType =>
typeof input.parse === 'function';

/**
* Parse and validate Params from LoaderArgs or ActionArgs. Throws an error if validation fails.
* @param params - A Remix Params object.
Expand All @@ -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