Validation

The global Run verb helpers declare HTTP handlers and attach validation options that parse, transform, and type path parameters, query strings, and request bodies.

Verb Helpers

Each verb helper (Run.GET, Run.HEAD, Run.POST, Run.PUT, Run.DELETE, Run.PATCH, Run.OPTIONS, Run.QUERY, and Run.ALL) accepts a handler function or array of functions, with optional validation options first:

Run.POST(handler); // single handler
Run.POST([auth, handler]); // composed handlers
Run.POST(options, handler); // validation options + handler
Run.POST(options, [auth, handler]); // validation options + composed handlers
Run.POST(options); // options only, validates and passes through

Export verb helpers by method name in +handler files or as the default export in +middleware files.

Note

Run is global in route files with no imports needed. See TypeScript.

Request Bodies

POST, PUT, PATCH, and QUERY handlers declare body parsing and validation with json or form. When either option configures a validator, ctx.body is a lazy promise for the parsed and validated body; without a validator, ctx.body is undefined (even if size limits are passed; use an identity validator such as json: (value) => value to accept raw parsed data).

import * as v from "valibot";

const NoteSchema = v.object({ text: v.pipe(v.string(), v.nonEmpty()) });

export const POST = Run.POST({ json: NoteSchema }, async (ctx) => {
  const [note, issues] = await ctx.body;
  if (issues) {
    return Response.json({ issues }, { status: 422 });
  }
  return Response.json(createNote(note), { status: 201 });
});

The body is read and parsed only when ctx.body is awaited (or .then is called), so early rejections avoid reading the request stream. Malformed bodies fail with 400, bodies exceeding configured limits fail with 413, and mismatched Content-Type headers fail with 415.

Note

ctx.body is a promise, not a function. Read it with await ctx.body; there is nothing to call.

Warning

Calling ctx.request.json() or ctx.request.formData() directly bypasses validation and size limits. Configure json or form on the verb helper and read await ctx.body instead.

json

Handles application/json requests. Accepts a validator directly or an options object:

OptionDefaultDescription
validatorFunction or Standard Schema for the parsed JSON
maxBytes1048576 (1MiB)Maximum request body size in bytes

form

Handles application/x-www-form-urlencoded and multipart/form-data requests. Repeated field names (for both text fields and uploaded files) collapse into arrays. Uploaded multipart files arrive in the parsed record as File instances carrying an extra fieldName property, or as the return value of onFile when provided.

OptionDefaultDescription
validatorFunction or Standard Schema for the parsed form
maxBytesmaxFiles * maxFileBytesMaximum request body size in bytes
maxParts1000Maximum number of parts in a multipart request
maxFiles20Maximum number of files in a multipart request
maxFileBytes1048576 (1MiB)Maximum size of each uploaded file in bytes
onFile(ctx, file) => any called per file; return value replaces file

Multipart upload example:

+page.marko
<form method="post" enctype="multipart/form-data">
  <input name="title" required>
  <input name="avatar" type="file" accept="image/*" required>
  <button type="submit">Upload</button>
</form>
form method="post" enctype="multipart/form-data"
  input name="title" required
  input name="avatar" type="file" accept="image/*" required
  button type="submit" -- Upload
+handler.ts
import * as v from "valibot";

const UploadSchema = v.object({
  title: v.pipe(v.string(), v.nonEmpty()),
  avatar: v.instance(File),
});

export const POST = Run.POST(
  {
    form: {
      validator: UploadSchema,
      maxFiles: 1,
      maxFileBytes: 5 * 1024 * 1024,
    },
  },
  async (ctx) => {
    const [upload, issues] = await ctx.body;
    if (issues) {
      return Response.json({ issues }, { status: 422 });
    }
    const bytes = await upload.avatar.arrayBuffer();
    await storeAvatar(upload.title, upload.avatar.name, bytes);
    return ctx.redirect("/profile", 303);
  },
);

Standard Schema

Standard Schema validators resolve to a [value, issues] discriminated tuple: on success, issues is undefined and value has the parsed type; on failure, value holds the raw input and issues lists validation errors. Checking issues narrows value for that read:

export const POST = Run.POST({ form: signupSchema }, async (ctx, next) => {
  const [signup, issues] = await ctx.body;
  if (issues) {
    return next({ issues });
  }
  return next({ signup }); // downstream receives the narrowed type
});
Tip

Check issues once in the handler and pass narrowed values to the page via next so templates receive validated data directly.

Merging Options

Options declared across middleware and handlers for the same route merge shallowly: the last validator wins, while object options combine (for example, maxBytes in middleware merges with validator in a handler). Options-only middleware (Run.ALL({ search: ... })) applies shared validation to every nested route.

Next Steps


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.