### Prettier Configuration File Example Source: https://val.build/docs/formatting An example of a `.prettierrc.json` file used to define Prettier's formatting rules. This file should be placed in the project's root directory. ```json { "semi": true, "trailingComma": "all", "singleQuote": false, "printWidth": 80, "tabWidth": 2 } ``` -------------------------------- ### Install Prettier with npm or pnpm Source: https://val.build/docs/formatting Installs Prettier as a regular dependency using either npm or pnpm. It's crucial to install it as a regular dependency, not a devDependency, for production environments. ```bash npm install prettier # or pnpm add prettier ``` -------------------------------- ### Install Val Packages (npm/pnpm) Source: https://val.build/docs/init Installs the core and Next.js integration packages for Val Build using npm or pnpm. Ensure you have Node.js and a package manager installed. ```bash npm install @valbuild/core@latest @valbuild/next@latest # pnpm i @valbuild/core@latest @valbuild/next@latest ``` -------------------------------- ### String Schema Code Editor Render Examples Source: https://val.build/docs/api/schema/string Shows how to define an object schema with string fields rendered as code editors using the 'code' render option, specifying the language for syntax highlighting. ```typescript const schema = s.object({ jsonConfig: s.string().render({ as: "code", language: "json" }), tsCode: s.string().render({ as: "code", language: "typescript" }), cssStyles: s.string().render({ as: "code", language: "css" }), }); ``` -------------------------------- ### Display Val Package Versions with CLI Source: https://val.build/docs/cli Displays the installed versions of `@valbuild/core` and `@valbuild/next` packages within your project. This command is useful for debugging and ensuring version compatibility. ```Bash npx -p @valbuild/cli val versions ``` -------------------------------- ### Define Val Module Schema and Content (TypeScript) Source: https://val.build/docs/workflow Defines a content schema using `s` (schema builder) and creates a content module with a unique path, schema, and initial content using `c.define()`. Ensures content matches the schema. ```typescript // app/page.val.ts import { s, c } from "@/val.config"; // Define your content schema const schema = s.object({ text: s.string(), }); // Create the module with a unique path, schema, and initial content export default c.define("/app/page.val.ts", schema, { text: "My first Val content", }); ``` -------------------------------- ### Install ESLint Plugin (npm/pnpm) Source: https://val.build/docs/init Installs the Val ESLint plugin as a development dependency using npm or pnpm. This plugin provides feedback on .val file configurations. ```bash npm install -D @valbuild/eslint-plugin@latest # pnpm i -D @valbuild/eslint-plugin@latest ``` -------------------------------- ### Monorepo Configuration with Valbuild (TypeScript) Source: https://val.build/docs/monorepo This configuration snippet demonstrates how to set up Valbuild for a project located in a subdirectory of a monorepo. It utilizes the `root` property in `val.config.ts` to define the project's relative path. This setup is crucial for correctly referencing project files and assets when Valbuild operates within a monorepo structure. ```typescript import { initVal } from '@valbuild/next'; const { s, c, val, config, nextAppRouter } = initVal({ root: '/web', // <- add this for monorepo project: 'valbuild/example', gitBranch: process.env.VERCEL_GIT_COMMIT_REF, gitCommit: process.env.VERCEL_GIT_COMMIT_SHA, defaultTheme: 'dark', }); export type { t } from '@valbuild/next'; export { s, c, val, config, nextAppRouter }; ``` -------------------------------- ### String Schema Textarea Render Example Source: https://val.build/docs/api/schema/string Illustrates how to define an object schema where a 'description' field uses the s.string() schema rendered as a textarea for multi-line input. ```typescript const schema = s.object({ description: s.string().render({ as: "textarea" }), }); ``` -------------------------------- ### Set up Val.js Page Router with Next.js Source: https://val.build/docs/page-router This snippet demonstrates the basic setup for Val's page router in a Next.js application. It uses `s.router(nextAppRouter, schema)` to automatically generate pages for each entry in a content record, mapping keys to URL paths. ```typescript // app/(main)/docs/[[...slug]]/page.val.ts import { c, nextAppRouter, s } from "@/val.config"; import { docsSchema } from "./docsSchema.val"; export default c.define( "/app/(main)/docs/[[...slug]]/page.val.ts", s.router(nextAppRouter, docsSchema), { "/docs": { title: "Overview", sections: [/* ... */], }, "/docs/init": { title: "Installation", sections: [/* ... */], }, "/docs/getting-started": { title: "Getting started", sections: [/* ... */], }, // ... more routes }, ); ``` -------------------------------- ### Fetch Route Content in Server Components Source: https://val.build/docs/page-router This example shows how to fetch route content in a Next.js Server Component using `fetchValRoute`. It takes the Val module and URL parameters, returning the content for the matching route or `null` if no match is found. ```typescript // app/(main)/privacy/page.tsx - Server Component import { fetchValRoute } from "@/val/val.rsc"; import pageVal from "./page.val"; import { notFound } from "next/navigation"; export default async function PrivacyPage({ params }: { params: unknown }) { const page = await fetchValRoute(pageVal, params); if (!page) { return notFound(); } return (

{page.title}

{/* Render your content */}
); } ``` -------------------------------- ### Router Schema Shorthand Explanation - Val.config Source: https://val.build/docs/api/schema/router This snippet explains that `s.router(nextAppRouter, schema)` is a shorthand for a more verbose configuration. It is equivalent to using `s.record(schema).router(nextAppRouter)`, providing a more concise way to achieve the same routing setup. ```javascript // s.router(nextAppRouter, schema) is shorthand for: s.record(schema).router(nextAppRouter) ``` -------------------------------- ### Register Val Module (TypeScript) Source: https://val.build/docs/workflow Registers a Val module in `val.modules.ts` so Val can recognize it. It uses dynamic imports for code splitting and better performance. Ensure the `def` property correctly points to your module. ```typescript import { modules } from "@valbuild/next"; import { config } from "./val.config"; export default modules(config, [ // Add your module here { def: () => import("./app/page.val") }, // You can add more modules as your project grows ]); ``` -------------------------------- ### Use Val Content in Server Component (React/TypeScript) Source: https://val.build/docs/workflow Employs the `fetchVal` function to retrieve Val module content server-side, ideal for Server Components to enhance performance. The fetched content is then rendered as HTML. ```typescript import { fetchVal } from "@/val/val.rsc"; import pageVal from "./page.val"; export default async function Page() { // fetchVal is async and fetches content server-side const { text } = await fetchVal(pageVal); return (

{text}

); } ``` -------------------------------- ### String Schema Basic Validation Source: https://val.build/docs/api/schema/string Provides an example of basic string validation using the .maxLength() method to enforce a maximum character limit. ```typescript s.string().maxLength(100) ``` -------------------------------- ### Define a Public Remote File URL in Val Build Source: https://val.build/docs/remote Example of how to define a public remote file URL. The filename part of the URL (after '/p/public/val/') can be customized as long as the file extension remains the same. ```javascript // Example of a public remote file // You can change the filename snurrebart to whatever you want c.remote("https://remote.val.build/file/p/ce803e0/b/v08/v/0.84.1/h/637c/f/c89d27d4be02/p/public/val/snurrebart.png") ``` -------------------------------- ### Marking Files as Remote in Val Build Source: https://val.build/docs/remote Code examples demonstrating how to designate an image or file as a remote file using the `.remote()` method in Val Build. This method is used to indicate that the file is stored remotely. ```javascript s.image().remote() // or s.file().remote() ``` -------------------------------- ### Define Array Schema with String Elements - Val Build Source: https://val.build/docs/api/schema/array This example demonstrates how to define an array schema where all elements must be strings using the s.array() and s.string() functions. It's a fundamental way to ensure data integrity for array inputs. ```javascript s.array(s.string()) ``` -------------------------------- ### Date Range and Custom Validation with s.date() (TypeScript) Source: https://val.build/docs/api/schema/date This example illustrates advanced validation using s.date(). It shows how to set minimum and maximum date constraints using .from() and .to(), and how to implement custom validation logic, such as ensuring a date is in the future. ```typescript const schema = s.object({ // Date must be between Jan 1, 2024 and Dec 31, 2024 eventDate: s.date().from("2024-01-01").to("2024-12-31"), // Date must be after today futureDate: s.date().validate((date) => { const eventDate = new Date(date); const today = new Date(); today.setHours(0, 0, 0, 0); if (eventDate < today) { return { success: false, message: "Date must be in the future" }; } return { success: true }; }), }); ``` -------------------------------- ### Fetch and Display Date from Val Schema (React Server Component) Source: https://val.build/docs/api/schema/date This React Server Component example shows how to fetch date data defined by a Val schema using fetchVal and then parse and format the date for display. It highlights the integration of Val schemas within a React application. ```typescript import { fetchVal } from "@/val/val.rsc"; import articleVal from "./article.val"; export default async function Article() { const { publishedAt } = await fetchVal(articleVal); // Parse and format the date const date = new Date(publishedAt); const formatted = date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); return ; } ``` -------------------------------- ### Handle Recursive Schemas with Getter Syntax for s.keyOf() Source: https://val.build/docs/keyof This example illustrates how to manage mutually recursive schemas when using s.keyOf(). By employing the 'getter syntax' for schema properties, TypeScript can correctly infer types, preventing issues with recursive definitions. ```typescript import { s } from "@/val.config"; import { Schema, t } from "@valbuild/next"; import blogsPageVal from "./blogs/[slug]/page.val"; import supportArticlePageVal from "./support/[slug]/page.val"; export const linkButtonSchema = s.object({ label: s.string(), link: s.union( "type", s.object({ type: s.literal("support-article"), get href(): Schema { return s.keyOf(supportArticlePageVal); }, }), s.object({ type: s.literal("blog"), get href(): Schema { return s.keyOf(blogsPageVal); }, }), ), }); export type LinkButton = t.DecodeVal>; ``` -------------------------------- ### Run Val Initialization Script Source: https://val.build/docs/init Executes the Val initialization script using npx or pnpm to set up the project for Val Build. This script configures the project based on Val's requirements. ```bash npx @valbuild/init@latest # pnpm @valbuild/init@latest ``` -------------------------------- ### Import Val Helper from Configuration Source: https://val.build/docs/api/val-helper Demonstrates how to import the `val` helper object from your Val configuration file. This is the initial step to utilize Val's utility methods. ```javascript import { val } from "@/val.config"; ``` -------------------------------- ### Use Val Content in Client Component (React/TypeScript) Source: https://val.build/docs/workflow Utilizes the `useVal` hook to fetch and display content from a Val module in a React Client Component. This is suitable for interactive components requiring client-side state. ```typescript "use client"; import { useVal } from "@/val/val.client"; import pageVal from "./page.val"; export default function Page() { // useVal returns the content from your Val module const { text } = useVal(pageVal); return (

{text}

); } ``` -------------------------------- ### Connect Project to Val Build with CLI Source: https://val.build/docs/cli Connects your project to Val Build, enabling non-technical users to edit content. Refer to the 'Connect to Val Build' documentation for detailed instructions. ```Bash npx -p @valbuild/cli val connect ``` -------------------------------- ### Configure Prettier in val.server.ts Source: https://val.build/docs/formatting Demonstrates how to integrate Prettier for code formatting within the Val server initialization. It involves importing Prettier and its configuration, then passing a formatter function to `initValServer`. ```typescript import prettier from "prettier"; import prettierOptions from "../.prettierrc.json"; const { valNextAppRouter } = initValServer( valModules, { ...config }, { draftMode, // Add formatter option here formatter: (code: string, filePath: string) => { return prettier.format(code, { filepath: filePath, ...prettierOptions, } as prettier.Options); }, }, ); ``` -------------------------------- ### Login to Val Build Service with CLI Source: https://val.build/docs/cli Logs into admin.val.build and generates a Personal Access Token for authenticating your local project with the Val Build service. ```Bash npx -p @valbuild/cli val login ``` -------------------------------- ### Connect to Val Build CLI Source: https://val.build/docs/init Connects the local project to Val Build using the Val CLI. This enables features like live editing and commit creation directly from editors. ```bash npx -p @valbuild/cli@latest connect ``` -------------------------------- ### c.file() - Define File Content Source: https://val.build/docs/api/content The `c.file()` function defines generic file content (videos, PDFs, etc.). Files must be in the `public/val` folder, and metadata like `mimeType` is required. ```APIDOC ## c.file() ### Description Used to define generic file content (e.g., videos, PDFs) in Val files. Files must be stored in the `public/val` folder. Requires a path and metadata including `mimeType`. ### Method `c.file(path: string, metadata: FileMetadata)` ### Parameters - **`path`** (string) - Required - The file path to the file, relative to the project root (must be in `/public/val`). - **`metadata`** (object) - Required - Metadata about the file. Contains: - **`mimeType`** (string) - The MIME type of the file (e.g., `video/mp4`, `application/pdf`). ### Example with video ```typescript import { c, s } from "@/val.config"; const schema = s.object({ title: s.string(), video: s.file({ accept: "video/*" }), }); export default c.define("/content/video.val.ts", schema, { title: "Product Demo", video: c.file("/public/val/demo.mp4", { mimeType: "video/mp4" }), }); ``` ### Important Notes - All files must be stored in the `/public/val` folder. - The `accept` parameter in `s.file()` should match the MIME type of the file. - Wildcards like `video/*` can be used to accept multiple file types. ``` -------------------------------- ### String Schema Rendering Options Source: https://val.build/docs/api/schema/string Demonstrates how to use the .render() method of the s.string() schema to change how strings are displayed in the UI. Supports 'textarea' for multi-line input and 'code' for syntax-highlighted code editing. ```typescript s.string().render({ as: "textarea" }) s.string().render({ as: "code", language: "typescript" }) ``` -------------------------------- ### Disable Stega Encoding for Schema Fields Source: https://val.build/docs/stega This example shows how to define a schema using Val's schema builder. By appending `.raw()` to a string field definition, you prevent stega encoding for that specific field, ensuring it retains its original value. ```typescript const schema = s.object({ // This field will have stega encoding title: s.string(), // This field will NOT have stega encoding apiKey: s.string().raw() }); ``` -------------------------------- ### Render Record Schema as List (JavaScript) Source: https://val.build/docs/api/schema/record Demonstrates how to define a record schema with object properties and render it as a list using the .render method. It specifies the output format and selects specific fields for display. ```javascript s.record(s.object({ name: s.string(), age: s.number(), })).render({ as: "list", select: ({ key, val }) => ({ title: val.name, subtitle: val.age }) }) ``` -------------------------------- ### Define File Schema and Content with Val Studio Source: https://val.build/docs/api/schema/file This snippet demonstrates how to define a file schema using 's.file()' with an 'accept' option for video files and then integrate the file content using 'c.file()'. It highlights the requirement for files to be placed in the 'public/val' folder. ```typescript const schema = s.file({ accept: "video/*" }); export default c.define("/content/file.val.ts", schema, c.file("/public/val/video.mp4") // NOTE: the actual files must be in the public/val folder ) ``` -------------------------------- ### Use Files in React Components (Basic) Source: https://val.build/docs/files Demonstrates basic usage of files fetched from Val within a React Server Component. It shows how to access the file URL using the `.url` property and render them as HTML elements like `