formAsyncParse function is the async version of formParse, designed for validating form data against Zod schemas that include asynchronous validation rules. This is essential when your schema uses async refinements like database lookups, API calls, or DNS verification (e.g., validateEmail).ts
import { formAsyncParse } from "@arkyn/server";
formData (required){ [k: string]: any }schema (required)ZodTypesuccess boolean.typescript
import { z } from "zod";import { formAsyncParse, validateEmail } from "@arkyn/server";// Schema with async email validation (DNS verification)const schema = z.object({name: z.string().min(1, "Name is required"),email: z.string().refine(async (email) => await validateEmail(email),"Invalid email or domain doesn't exist",),username: z.string().refine(async (username) => {const exists = await checkUsernameExists(username);return !exists;}, "Username already taken"),});const formData = {name: "John",email: "user@invalid-domain.xyz",username: "john",};const result = await formAsyncParse([formData, schema]);if (!result.success) {console.log(result.fieldErrors);// { email: "Invalid email or domain doesn't exist" }console.log(result.fields);// { name: "John", email: "user@invalid-domain.xyz", username: "john" }} else {console.log(result.data);// Validated data with proper types}
success: truedata: The parsed and validated data (typed according to the schema)success: falsefieldErrors: Object mapping field names to error messagesfields: The original form data (useful for repopulating the form)formAsyncParse instead of formParse when your schema includes:validateEmail.refine() or .superRefine() with async callbackssafeParseAsync internally, which properly awaits all async refinements before returning the result.address.city for nested objects).formParse as it has no async overhead.SchemaValidator class, which provides formAsyncValidate that throws UnprocessableEntity on failure.