Skip to content
AtMyAppDocs
Open dashboard
Reference

@atmyapp/structure

Schema builders, type inference, validation and introspection.

Open with AI
Terminal window
npm install @atmyapp/structure

The package that defines what a schema is. Websites use its builders in atmyapp.schema.ts; tools use its compiler and validators. @atmyapp/core re-exports all of it. Guides: Design your schema and the field reference.

Export Creates
defineSchema({ definitions, submissions?, mdx?, localization?, description? }) The schema
defineDocument({ fields, path?, description? }) A document
defineCollection({ fields, description?, indexes? }) A collection
defineSubmission({ fields, description?, captcha? }) A form, under submissions
defineFile({ path }) / defineImage({ path }) A single managed asset
s Field builders: see the field reference

defineDocument and defineCollection also accept name (display metadata; the map key stays the identifier), localize and systemFields (advanced storage settings). defineSystemConfig, defineEvent and defineBasicEvent exist for framework presets and the experimental analytics; websites rarely need them.

import type { InferDefinition, InferSubmission, InferSchemaTypes } from "@atmyapp/structure";
import schema from "./atmyapp.schema";
type Post = InferDefinition<"posts", typeof schema>;
type ContactForm = InferSubmission<"contact", typeof schema>;
type All = InferSchemaTypes<typeof schema>;

You rarely need these directly: a client created with schema already returns typed values. InferSubmission describes what a form accepts, so asset fields are File/Blob rather than URLs.

import { compileSchema, validateSchemaDocument, validateContent } from "@atmyapp/structure";
import schema from "./atmyapp.schema";
const check = validateSchemaDocument(schema);
if (!check.valid) throw new Error(JSON.stringify(check.issues, null, 2));
const compiled = compileSchema(schema);
const result = validateContent(compiled, "home", { headline: "Hello" });
if (!result.valid) console.error(result.issues); // [{ path, message, code }]
Export Does
validateSchemaDocument(schema) Checks the schema itself: reserved names, references to missing definitions, invalid options
compileSchema(schema) Normalises and indexes the schema for fast lookups
validateContent(compiled, name, value) Checks a document or entry value with the same rules AtMyApp uses
validateContentAtPath(compiled, path, raw, mimeType?) Same, for a raw stored file
serializeSchema(schema) Deterministic JSON, as uploaded by atmyapp migrate
parseSchema(json) Reads a serialised schema back

Checks that need other content, such as unique values across entries or references to existing entries, run in AtMyApp when content is saved and published.

For tools that walk a compiled schema: getDefinition, getCollection, getDocument, getField, getSubmission, listDefinitionsByKind, listReferences, listAssetFields, listSubmissions and resolveDefinitionForPath.

diffSchemas and planMigration compare two schemas, and validateMigrationOperations checks renameField / backfillField operations. The CLI uses them; see Change a live schema.

resolveWebsiteMetadata(page, settings, defaults, pageUrl, siteUrl) turns page values and Website settings into final head tags, with the same rules as AtMyAppHead. Metadata is its type. See Website settings.