Overview

Files Schema

s.files()

A collection of files that is the whole module - the same idea as s.images(), for any file type rather than images. Where s.file() defines a single field, s.files() defines a module that holds many files at once, keyed by their file path.

The content is a record: each key is the path of a file in the collection's directory, and each value is the mimeType Val read off that file. Entries appear when a file is uploaded through Val Studio, the CLI or the VS Code extension - you do not write them by hand.

Fields elsewhere pick from a collection with s.file(collectionModule), the same way s.image(galleryModule) picks from an image gallery.

Like image galleries, file collections are listed under Media in Val Studio rather than in the Explorer file tree.

Options:
accept: string

Required. Which file types the collection accepts. A comma-separated list of one or more MIME types, or unique file type specifiers.

s.files({ accept: "application/pdf", directory: "/public/val/documents" })
directory: "/public" | `/public/${string}`

Required. Where the files in this collection are stored. Must start with /public. Two collections may not claim the same directory.

s.files({ accept: "*/*", directory: "/public/val/documents" })
Methods:
.remote: method

Store the files of this collection on Val's remote server instead of in your git repository. See the remote files guide.

s.files({ accept: "application/pdf", directory: "/public/val/documents" }).remote()
Examples:
Defining a file collection
content/documents.val.ts
import { c, s } from "../val.config";

// A collection is the entire module - the schema is not wrapped in an object
export default c.define(
  "/content/documents.val.ts",
  s.files({
    accept: "application/pdf",
    directory: "/public/val/documents",
  }),
  {
    // Entries are added when you upload a file. They are keyed by file path.
    "/public/val/documents/report_a1b2c.pdf": {
      mimeType: "application/pdf",
    },
  },
);
Picking a file from the collection
content/page.val.ts
import { c, s } from "../val.config";
import documentsVal from "./documents.val";

export default c.define(
  "/content/page.val.ts",
  s.object({
    title: s.string(),
    // Pass the collection module to s.file() to pick from it
    datasheet: s.file(documentsVal),
  }),
  {
    title: "Product page",
    // Only the path: the mimeType lives in the collection
    datasheet: { path: "/public/val/documents/report_a1b2c.pdf" },
  },
);
A remote file collection
content/remoteDocuments.val.ts
export default c.define(
  "/content/remoteDocuments.val.ts",
  s
    .files({
      accept: "application/pdf",
      directory: "/public/val/remote-documents",
    })
    .remote(),
  {},
);