Production Guides & Recipes
The Complete 15-Field Factory Catalog
Section titled “The Complete 15-Field Factory Catalog”@nexload-sdk/payload-schema provides 15 specialized factories under the field namespace. Every factory configures both the Payload field definition and the corresponding Zod schema.
import { defineEntity, field } from "@nexload-sdk/payload-schema";import { z } from "zod";
export const catalogEntity = defineEntity({ name: "CatalogItem", fields: { // 1. Text: Trimming, case conversion, length constraints, regex pattern title: field.text({ required: true, trim: true, minLength: 3, maxLength: 120, }),
// 2. Slug: Automatic Unicode NFKC normalization and URL hyphen-casing slug: field.slug({ required: true, minLength: 3, maxLength: 140, }),
// 3. Textarea: Multiline text with max length summary: field.textarea({ required: true, trim: true, maxLength: 1000, }),
// 4. Number: Integer, safe integer checks, min/max range, step multiples stock: field.number({ integer: true, safe: true, minimum: 0, defaultValue: 0, }),
// 5. Money: Minor-unit integer (cents/rials) with explicit currency metadata price: field.money({ currency: "USD", minimum: 0, required: true, }),
// 6. Boolean: Checkbox field with boolean casting isActive: field.boolean({ defaultValue: true, }),
// 7. Date: Timezone-aware ISO datetime string with min/max bounds (hook accepts JS Date) publishedAt: field.date({ minimum: "2024-01-01T00:00:00.000Z", nullable: true, }),
// 8. Select: Single or multi-choice enums with custom admin labels tier: field.select({ values: ["standard", "premium", "enterprise"] as const, labels: { enterprise: "Enterprise SLA" }, defaultValue: "standard", }), badges: field.select({ hasMany: true, values: ["new", "sale", "featured"] as const, }),
// 9. Relationship: Single or polymorphic relationships category: field.relationship({ relationTo: "categories", required: true, }), authorOrTeam: field.relationship({ relationTo: ["authors", "teams"] as const, hasMany: false, idSchema: z.string().uuid(), }),
// 10. Upload: Media asset relationship heroImage: field.upload({ relationTo: "media", required: true, }),
// 11. Group: Nested object schema and Payload named group seo: field.group({ fields: { metaTitle: field.text({ maxLength: 70 }), metaDesc: field.textarea({ maxLength: 160 }), }, }),
// 12. Array: Repeatable rows with row count constraints variants: field.array({ minRows: 1, maxRows: 10, fields: { sku: field.text({ required: true, uppercase: true }), price: field.money({ currency: "USD", required: true }), }, }),
// 13. Blocks: Discriminated polymorphic blocks with child validation layout: field.blocks({ minRows: 1, maxRows: 10, blocks: { hero: { fields: { headline: field.text({ required: true }), subheading: field.text({ nullable: true }), }, }, quote: { fields: { quoteText: field.textarea({ required: true }), author: field.text({ required: true }), }, }, }, }),
// 14. RichText: Requires explicit AST schema validator description: field.richText({ schema: z.record(z.string(), z.unknown()), }),
// 15. Native: Escape hatch for custom/unsupported Payload field types coordinates: field.native({ payload: { type: "point" }, schema: z.tuple([z.number(), z.number()]), }), },});Static Defaults vs. Dynamic Defaults
Section titled “Static Defaults vs. Dynamic Defaults”@nexload-sdk/payload-schema separates static values from runtime functions:
// 1. Static Default: Validated during defineEntity() at startupfield.number({ defaultValue: 100,});
// 2. Dynamic Default: Evaluated at runtime by Payload per write requestfield.date({ dynamicDefaultValue: ({ req, user, locale }) => new Date().toISOString(),});- Providing both
defaultValueanddynamicDefaultValuethrowsCONFLICTING_DEFAULT_CONFIGURATION. - Direct assignment to
payload.defaultValueis prohibited and throwsRESERVED_PAYLOAD_OPTION. - Defaults do not append
.default()to derived Zod schemas, keeping API request validation explicit.
Advanced Schema Derivation Patterns
Section titled “Advanced Schema Derivation Patterns”Use entity.schema() to generate input schemas for mutations, projections, and forms:
1. Picking Fields with Strictness and optional: "auto"
Section titled “1. Picking Fields with Strictness and optional: "auto"”export const createItemSchema = catalogEntity.schema(({ pick }) => pick(["title", "slug", "price", "stock", "layout"], { optional: "auto", // Automatically marks non-required fields optional strict: true, // Rejects unexpected fields }));- With
{ optional: "auto" }, any field whose definition does not haverequired: truebecomes.optional(). - An explicit
requiredarray (e.g.required: ["stock"]) takes precedence and forces listed keys to be required.
2. Custom Field Transformations
Section titled “2. Custom Field Transformations”export const catalogCardSchema = catalogEntity.schema(({ fields, z }) => z.object({ title: fields.title, slug: fields.slug, // Transform minor-unit cents into formatted dollar string priceDisplay: fields.price.transform((cents) => `$${(cents / 100).toFixed(2)}`), }));3. Cross-Field Refinements
Section titled “3. Cross-Field Refinements”export const inventoryUpdateSchema = catalogEntity.schema(({ fields, z }) => z .object({ stock: fields.stock, isActive: fields.isActive, }) .refine((data) => !(data.stock === 0 && data.isActive), { message: "Cannot mark an out-of-stock item as active.", path: ["isActive"], }));4. Inferring TypeScript Types
Section titled “4. Inferring TypeScript Types”export type CreateItemInput = z.infer<typeof createItemSchema>;export type CatalogCardDTO = z.infer<typeof catalogCardSchema>;Payload Collection Layout Integration
Section titled “Payload Collection Layout Integration”Payload field compilation is decoupled from the core entity and imported from @nexload-sdk/payload-schema/payload (Node-only):
import type { CollectionConfig } from "payload";import { payloadFields } from "@nexload-sdk/payload-schema/payload";import { catalogEntity } from "./catalog.entity";
// Option A: Direct full exportexport const CatalogCollection: CollectionConfig = { slug: "catalog", fields: payloadFields(catalogEntity).all(),};
// Option B: Mixed export with tabs and sidebar layoutexport const AdvancedCatalogCollection: CollectionConfig = { slug: "catalog", fields: [ ...payloadFields(catalogEntity).pick(["title", "slug"]), { type: "tabs", tabs: [ { label: "Pricing & Stock", fields: payloadFields(catalogEntity).pick(["price", "stock", "variants"]), }, { label: "SEO & Media", fields: payloadFields(catalogEntity).pick(["seo", "heroImage", "description", "layout"]), }, ], }, ],};Intrinsic Normalization & Runtime Semantics
Section titled “Intrinsic Normalization & Runtime Semantics”Normalization on Writes
Section titled “Normalization on Writes”When writing data through the Payload Admin UI or payload.create / payload.update, canonical normalization executes automatically in beforeValidate:
// Local API Write:await payload.create({ collection: "catalog", data: { title: " Vintage Leather Jacket ", // Normalized to "Vintage Leather Jacket" slug: "Vintage Leather Jacket", // Normalized to "vintage-leather-jacket" variants: [ { sku: "vlj-01", price: 15000 }, // sku normalized to "VLJ-01" ], },});Null Semantics
Section titled “Null Semantics”Payload CMS represents empty or cleared fields as null. Canonical beforeValidate hooks pass null through unchanged for all field kinds (including nested children in groups, arrays, and blocks), matching undefined semantics:
- Clearing optional fields with
nullsucceeds without raising Zod type errors. - Required fields without a value fail Payload’s built-in validation (“This field is required.”), reporting the exact path and field label.
Date Objects
Section titled “Date Objects”In field.date(), the runtime Payload hook accepts JavaScript Date instances and normalizes valid dates to ISO 8601 strings. Canonical and derived Zod schemas continue to expect ISO strings.
Codec Safety
Section titled “Codec Safety”Built-in field normalizations (trim, lowercase, uppercase, slug, date) are implemented using Zod .overwrite() instead of unidirectional .transform(). This preserves codec reversibility so derived schemas can be encoded with z.encode (e.g., in @nexload-sdk/payload-operations outputs).
Known Limitations
Section titled “Known Limitations”- No inline Admin validation: Validation is enforced via
beforeValidatehooks. The package does not generate inline Adminvalidatefunctions. - Custom
options.schematransforms are not codec-safe: Custom transformations passed tooptions.schema(...)cannot be inverted byz.encode. Use.refine()or.overwrite()if schemas are used in operation responses. - Strict pick default: Calling
pick(keys)without{ optional: ... }makes every key strictly required. Use{ optional: "auto" }to inherit field-level optionality.
Troubleshooting & Diagnostics
Section titled “Troubleshooting & Diagnostics”1. SCHEMA_UNAVAILABLE
Section titled “1. SCHEMA_UNAVAILABLE”Occurs when deriving a schema that includes a field.native() defined without a schema parameter, or a container (group, array, blocks) that includes a schema-less native field.
// Solution: Provide the schema parameterfield.native({ payload: { type: "point" }, schema: z.tuple([z.number(), z.number()]),});2. INVALID_ENTITY
Section titled “2. INVALID_ENTITY”Thrown when passing a non-entity value to payloadFields():
import { payloadFields } from "@nexload-sdk/payload-schema/payload";
// Throws PayloadSchemaError: INVALID_ENTITY// @ts-expect-error - intentional misuse with non-entitypayloadFields({ name: "NotAnEntity" });3. ASYNC_CANONICAL_SCHEMA_UNSUPPORTED
Section titled “3. ASYNC_CANONICAL_SCHEMA_UNSUPPORTED”Payload’s field validation hooks run synchronously. Canonical parsing raises ASYNC_CANONICAL_SCHEMA_UNSUPPORTED when a schema is async (e.g., contains .refine(async ...) or .transform(async ...)): at defineEntity time only when validating a static defaultValue, otherwise when the Payload hook parses a value during writes. Move async checks into custom Payload collection hooks or derived API schemas.
4. Safe Introspection with inspect()
Section titled “4. Safe Introspection with inspect()”Inspect the runtime structure and field configurations safely without exposing internal state:
const inspection = catalogEntity.inspect();console.log(inspection.name); // "CatalogItem"console.log(inspection.fields.price.currency); // "USD"5. Error Handling Helper
Section titled “5. Error Handling Helper”Use isPayloadSchemaError() to catch and format entity definition failures:
import { isPayloadSchemaError } from "@nexload-sdk/payload-schema";
try { // defineEntity(...)} catch (error) { if (isPayloadSchemaError(error)) { console.error(`[${error.code}] in phase ${error.phase}: ${error.message}`); }}Migrating to 3.0
Section titled “Migrating to 3.0”Version 3.0 brings browser safety, codec compatibility, blocks fields, and predictable null handling.
New in 3.0
Section titled “New in 3.0”field.blocks(): Discriminated polymorphic block containers with per-block schemas and child validation hooks.pick(..., { optional: "auto" }): Automatically marks fields withoutrequired: trueas optional in derived schemas while preserving explicit required overrides.Dateaccepted by the date hook: The runtimefield.date()hook accepts JavaScriptDateinstances and normalizes valid dates to ISO 8601 strings.- Composite child optionality & nullability: Inside
group,array, andblocks, non-required child fields are automatically optional and acceptnull(key?: T | null), so stored Payload documents with empty fields or rows omitting optional children parse cleanly. - Reserved block child names: Block child fields cannot be named
id,blockName, orblockType; doing so throwsPayloadSchemaError(INVALID_FIELD_CONFIGURATION) at definition time.
Breaking Changes
Section titled “Breaking Changes”Below are the before and after patterns for each breaking change.
1. Payload Compilation Moved to /payload Subpath
Section titled “1. Payload Compilation Moved to /payload Subpath”Entity definitions no longer carry a .payload facade on the entity object. Payload compilation is now imported from @nexload-sdk/payload-schema/payload (Node-only), making the root entry browser-safe.
Before (2.0):
import { defineEntity, field } from "@nexload-sdk/payload-schema";
export const articleEntity = defineEntity({ /* ... */ });
export const Articles = { slug: "articles", // In 2.0, compiled fields were accessed via property on the entity: fields: articleEntity["payload"]["all"](),};After (3.0):
import { defineEntity, field } from "@nexload-sdk/payload-schema";import { payloadFields } from "@nexload-sdk/payload-schema/payload";
export const articleEntity = defineEntity({ name: "Article", fields: { title: field.text({ required: true }), },});
export const Articles = { slug: "articles", fields: payloadFields(articleEntity).all(),};2. Zod Peer Dependency Floor Raised to 4.1
Section titled “2. Zod Peer Dependency Floor Raised to 4.1”Zod 4.1.0 or higher is required to support Zod codec methods (z.encode / z.decode).
Before (2.0):
{ "peerDependencies": { "zod": ">=4.0.0 <5.0.0" }}After (3.0):
{ "peerDependencies": { "zod": ">=4.1.0 <5.0.0" }}3. Null Passes Canonical Validation Hooks
Section titled “3. Null Passes Canonical Validation Hooks”In 2.0, clearing an optional field by passing null caused Zod to throw a ValidationError unless the field was explicitly declared nullable: true. In 3.0, null passes through canonical hooks unchanged (like undefined), allowing empty optional fields to clear cleanly.
Before (2.0):
// Required nullable: true to allow clearing optional fields in database writes:subtitle: field.text({ nullable: true }),
// Clearing without nullable: true threw ValidationError:// "Expected string, received null"await payload.update({ collection: "articles", id: "1", data: { subtitle: null },});After (3.0):
// Optional fields without nullable: true allow null to pass through:subtitle: field.text(),
// Clears field cleanly; required fields fail Payload's "This field is required." check:await payload.update({ collection: "articles", id: "1", data: { subtitle: null },});4. Date Objects Accepted by Date Hook
Section titled “4. Date Objects Accepted by Date Hook”In 2.0, passing JavaScript Date instances to Payload writes failed canonical validation because the hook strictly required ISO strings. In 3.0, the runtime hook accepts valid Date objects and converts them to ISO strings.
Before (2.0):
// Threw ValidationError: "Expected string, received object"await payload.create({ collection: "events", data: { eventDate: new Date(), },});After (3.0):
// Accepted and normalized to ISO string by runtime hook:await payload.create({ collection: "events", data: { eventDate: new Date(), },});5. Built-in Normalizations Are Codec-Safe (.overwrite)
Section titled “5. Built-in Normalizations Are Codec-Safe (.overwrite)”In 2.0, text transformations like trim: true and lowercase: true used unidirectional .transform(), which causes z.encode to fail. In 3.0, normalizations use .overwrite(), making derived schemas compatible with codec encoding.
Before (2.0):
import { defineEntity, field } from "@nexload-sdk/payload-schema";import { z } from "zod";
const productEntity = defineEntity({ name: "Product", fields: { title: field.text({ trim: true }), },});
const productSchema = productEntity.schema(({ fields }) => fields.title);// Failed: unidirectional transform cannot be encodedz.encode(productSchema, " widget ");After (3.0):
import { defineEntity, field } from "@nexload-sdk/payload-schema";import { z } from "zod";
const productEntity = defineEntity({ name: "Product", fields: { title: field.text({ trim: true }), },});
const productSchema = productEntity.schema(({ fields }) => fields.title);// Succeeds: .overwrite() produces codec-safe schemas for operation outputsconst encoded = z.encode(productSchema, " widget ");6. Composite Children Without required: true Are Optional and Nullable
Section titled “6. Composite Children Without required: true Are Optional and Nullable”In 2.0, composite containers (group, array, blocks) constructed their canonical object schemas directly from child schemas, causing stored documents with null (Payload’s empty field representation) or rows omitting optional children to fail validation. In 3.0, any child field not declared required: true inside a composite container is automatically optional and nullable (child.schema.nullable().optional()).
Before (2.0):
const userEntity = defineEntity({ name: "User", fields: { meta: field.group({ fields: { tag: field.text({ required: true }), note: field.text(), // In 2.0, note was required in canonical schema }, }), },});
// Stored Payload document with empty note failed canonical parse:// "Invalid input: expected string, received null"userEntity.fields.meta.schema.parse({ tag: "vip", note: null });After (3.0):
const userEntity = defineEntity({ name: "User", fields: { meta: field.group({ fields: { tag: field.text({ required: true }), note: field.text(), // In 3.0, non-required child is optional and nullable (key?: string | null) }, }), },});
// Parses cleanly for both null and omitted values:userEntity.fields.meta.schema.parse({ tag: "vip", note: null }); // { tag: "vip", note: null }userEntity.fields.meta.schema.parse({ tag: "vip" }); // { tag: "vip" }7. Reserved Child Field Names in Blocks
Section titled “7. Reserved Child Field Names in Blocks”In 3.0, block definitions reject child fields named id, blockName, or blockType at definition time with PayloadSchemaError (INVALID_FIELD_CONFIGURATION), as these names are reserved by Payload blocks runtime metadata.
Before (2.0): Blocks were not supported in 2.0.
After (3.0):
// Throws PayloadSchemaError (code: "INVALID_FIELD_CONFIGURATION"):field.blocks({ blocks: { hero: { fields: { id: field.text(), // Reserved name: throws at definition time }, }, },});