Module Files

Module Files

Val content is defined in .val.ts (or .val.js) files. Only these files can export content using the c.define() function. They must be defined in the val.modules.ts file (see below).

NOTE: .val.ts files can also export only schemas without content, useful for shared type definitions. Only files that export c.define() must be registered in val.modules.ts. Val files can only import from other .val.ts files or type definitions (using import type).

Structure

To define content, use c.define() as the default export with three arguments:

  • modulePath - The file path relative to the project root

  • schema - The schema definition using schema types

  • content - The actual content data

Example

// app/page.val.ts
import { s, c } from "@/val.config";

const schema = s.object({
  title: s.string(),
  description: s.string(),
});

export default c.define("/app/page.val.ts", schema, {
  title: "Welcome",
  description: "My first Val content",
});

Requirements

Val module files are evaluated by Val and must follow these rules:

  • The first argument to c.define() must match the file path

  • Must be declared in the val.modules.ts file

  • Can only import Val-related files or types (using import type)

Use the eslint plugin or npx -p @valbuild/cli val validate to enforce these requirements.

Registering modules

Files that export c.define() must be registered in val.modules.ts. Schema-only .val.ts files don't need registration.

import { modules } from "@valbuild/next";
import { config } from "./val.config";

export default modules(config, [
  { def: () => import("./app/page.val") },
]);