bhofstaetter

payload field factories

by bhofstaetter
Testing & developer experience
v3
>=3.24.0
MIT

Overview

Summary generated from the README

A library of factory functions for writing Payload field configurations more concisely, with overrides typed from the underlying Payload field types. It covers text, number, date, relationship, upload, join, array, blocks, group, tabs and other field types, and requires Payload 3.24 or later. Helpers allow custom factories and project-wide defaults.

Features

  • Factories for 25 field types including text, date, relationship, blocks, tabs and ui
  • Overrides typed from Payload field types, with hasMany overloads for single and many variants
  • Date factories with built-in picker appearance and German display formats
  • mergeOverrides helper layering defaults, caller overrides and enforced values
  • createFieldFactories sets project-wide label and date format defaults

Use cases

  • Reduce boilerplate when defining many collection fields in a Payload project
  • Build reusable custom field factories with enforced validation and hooks
  • Apply consistent labels and date formats across all collections

From the author

Opinionated factory functions for Payload CMS field configurations.

Installation

npm i payload-field-factories

1

Stars

0

Forks

0

Open Issues

5 days ago

Last Updated

235

Weekly Downloads

v1.3.0

npm Version

44.5 KB

Unpacked Size

0

Dependencies

GitHub stars

1+1 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20261
Dec 4, 20261
Dec 3, 20261
Dec 2, 20261
Dec 1, 20261
Nov 30, 20261
Nov 29, 20261
Nov 28, 20261
Nov 27, 20261
Nov 26, 20261
Nov 25, 20261
Nov 24, 20261
Nov 23, 20260
Nov 22, 20260
Nov 21, 20260
Nov 20, 20260
Nov 19, 20260
Nov 18, 20260
Nov 17, 20260
Nov 16, 20260
Nov 15, 20260
Nov 14, 20260
Nov 13, 20260
Nov 12, 20260
Nov 11, 20260
Nov 10, 20260
Nov 9, 20260
Nov 8, 20260
Nov 7, 20260
Nov 6, 20260
Nov 5, 20260
Nov 4, 20260
Nov 3, 20260
Nov 2, 20260
Nov 1, 20260
Oct 31, 20260
Oct 30, 20260
Oct 29, 20260
Oct 28, 20260
Oct 27, 20260
Oct 26, 20260
Oct 25, 20260
Oct 24, 20260
Oct 23, 20260
Oct 22, 20260
Oct 21, 20260
Oct 20, 20260
Oct 19, 20260
Oct 18, 20260
Oct 17, 20260
Oct 16, 20260
Oct 15, 20260
Oct 14, 20260
Oct 13, 20260
Oct 12, 20260
Oct 11, 20260
Oct 10, 20260
Oct 9, 20260
Oct 8, 20260
Oct 7, 20260
Oct 6, 20260
Oct 5, 20260
Oct 4, 20260
Oct 3, 20260
Oct 2, 20260
Oct 1, 20260
Sep 30, 20260
Sep 29, 20260
Sep 28, 20260
Sep 27, 20260
Sep 26, 20260
Sep 25, 20260
Sep 24, 20260
Sep 23, 20260
Sep 22, 20260
Sep 21, 20260
Sep 20, 20260
Sep 19, 20260
Sep 18, 20260
Sep 17, 20260
Sep 16, 20260
Sep 15, 20260
Sep 14, 20260
Sep 13, 20260
Sep 12, 20260
Sep 11, 20260
Sep 10, 20260
Sep 9, 20260
Sep 8, 20260
Sep 7, 20260
Sep 6, 20260
npm weekly downloads

214+199 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 2026214
Dec 4, 2026211
Dec 3, 2026179
Dec 2, 20262
Dec 1, 202614
Nov 30, 202614
Nov 29, 202613
Nov 28, 202613
Nov 27, 202614
Nov 26, 202614
Nov 25, 202615
Nov 24, 20263
Nov 23, 20264
Nov 22, 20266
Nov 21, 20266
Nov 20, 20264
Nov 19, 202610
Nov 18, 202617
Nov 17, 202617
Nov 16, 202616
Nov 15, 202618
Nov 14, 202618
Nov 13, 202622
Nov 12, 202626
Nov 11, 202618
Nov 10, 202618
Nov 9, 202620
Nov 8, 202617
Nov 7, 202618
Nov 6, 202614
Nov 5, 20264
Nov 4, 20265
Nov 3, 20266
Nov 2, 20267
Nov 1, 20267
Oct 31, 20266
Oct 30, 20266
Oct 29, 20266
Oct 28, 20265
Oct 27, 20264
Oct 26, 20263
Oct 25, 20262
Oct 24, 20262
Oct 23, 20262
Oct 22, 20265
Oct 21, 20265
Oct 20, 20265
Oct 19, 20263
Oct 18, 20264
Oct 17, 20265
Oct 16, 20265
Oct 15, 20263
Oct 14, 202610
Oct 13, 202610
Oct 12, 202610
Oct 11, 202610
Oct 10, 20269
Oct 9, 20269
Oct 8, 20268
Oct 7, 20263
Oct 6, 20263
Oct 5, 20263
Oct 4, 20262
Oct 3, 20265
Oct 2, 202610
Oct 1, 202610
Sep 30, 20268
Sep 29, 202617
Sep 28, 202617
Sep 27, 202618
Sep 26, 202618
Sep 25, 202614
Sep 24, 202615
Sep 23, 202616
Sep 22, 202616
Sep 21, 202617
Sep 20, 202617
Sep 19, 202614
Sep 18, 202617
Sep 17, 202620
Sep 16, 202621
Sep 15, 202612
Sep 14, 202613
Sep 13, 202612
Sep 12, 202612
Sep 11, 20268
Sep 10, 20264
Sep 9, 20267
Sep 8, 20268
Sep 7, 202615
Sep 6, 202615

Each point is npm's rolling 7-day download count.

Health Score

77/100Excellent

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1175876504-root.svg)](https://payloaddirectory.dev/plugins/1175876504-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1175876504-root"><img src="https://payloaddirectory.dev/badge/1175876504-root.svg" alt="Payload Directory"></a>
Updated Nov 30, 2026Created Mar 8, 2026Published Nov 30, 2026

README

Payload Field Factories

Opinionated factory functions for Payload CMS field configurations. Type-safe, concise, and composable.

Installation

npm install payload-field-factories

Usage

import {
    dateField,
    groupField,
    relationshipField,
    richTextField,
    tab,
    tabsField,
    textField,
} from 'payload-field-factories';

const fields = [
    textField('title', {required: true, localized: true}),
    textField('tags', {hasMany: true}),
    relationshipField('author', 'users'),
    dateField('publishedAt'),
    groupField('address', [
        textField('street'),
        textField('city'),
    ]),
    tabsField([
        tab('content', [richTextField('body')]),    // named tab
        tab({en: 'Meta'}, [textField('slug')]),      // unnamed tab
    ]),
];

The override type is derived from the Payload field type with required args omitted, giving you full type safety and autocompletion. There are two calling patterns:

Fields identified by just a name take the name as their only required argument, followed by an optional overrides object:

textField('title', {required: true, localized: true});
numberField('price');

Fields that also need child fields or options take that content as their second argument (or as the only argument for rowField, tabsField, and unnamed groupField). Pass it directly as a shortcut, or pass an object with the content plus overrides when you need to tweak the field:

// array — name + fields
arrayField('items', [textField('label')]);
arrayField('items', {fields: [textField('label')], overrides: {minRows: 1}});

// blocks — name + block references
blocksField('content', ['hero', 'cta']);
blocksField('content', {blockReferences: ['hero', 'cta'], overrides: {minRows: 1}});
blocksField('content', {blocks: [heroBlock], overrides: {minRows: 1}}); // inline blocks instead of references

// radio — name + options
radioField('size', ['sm', 'md', 'lg']);
radioField('size', {options: ['sm', 'md', 'lg'], overrides: {defaultValue: 'md'}});

// select — name + options
selectField('status', ['draft', 'published']);
selectField('tags', {options: ['news', 'blog'], overrides: {hasMany: true}});

// group — name + fields (name optional)
groupField('address', [textField('street'), textField('city')]);
groupField('address', {fields: [textField('street')], overrides: {localized: true}});
groupField([textField('street')]); // unnamed

// ui — name + components
uiField('preview', {Field: '/components/Preview'});
uiField('preview', {components: {Field: '/components/Preview'}, overrides: {admin: {position: 'sidebar'}}});

// collapsible — label + fields
collapsibleField('Advanced', [textField('slug')]);
collapsibleField('Advanced', {fields: [textField('slug')], overrides: {admin: {initCollapsed: true}}});

// row — fields only
rowField([textField('firstName'), textField('lastName')]);
rowField({fields: [textField('firstName'), textField('lastName')], overrides: {admin: {readOnly: true}}});

// tabs — tabs only
tabsField([tab('content', [richTextField('body')])]);
tabsField({tabs: [tab('content', [richTextField('body')])], overrides: {label: 'Sections'}});

// tab — name + fields (named, label goes in overrides) or label + fields (unnamed)
tab('content', [richTextField('body')]);
tab('content', {fields: [richTextField('body')], overrides: {label: 'Content'}});
tab({en: 'Meta', de: 'Meta'}, [textField('slug')]); // unnamed

Relationship, upload and join fields take their targets as positional arguments:

relationshipField('author', 'users');
relationshipField('related', ['posts', 'pages'], {hasMany: true});
uploadField('image', 'media');
joinField('posts', 'posts', 'author'); // name, collection, on

hasMany is typed via overloads on textField, numberField, selectField, relationshipField and uploadField: without hasMany (or hasMany: false) you get the single variant (e.g. RelationshipFieldSingle), with hasMany: true the many variant (RelationshipFieldMany, which allows minRows/maxRows). A hasMany that is only known as boolean at runtime returns the union (RelationshipField).

const hasMany: boolean = config.multiple;
relationshipField('tags', 'tags', {hasMany}); // RelationshipField

Date defaults

dateField, dateTimeField and timeField set the picker appearance and German formats:

FactorypickerAppearancedisplayFormattimeFormattimeIntervals
dateFielddayOnlydd.MM.yyyy––
dateTimeFielddayAndTimedd.MM.yyyy HH:mmHH:mm5
timeFieldtimeOnlyHH:mmHH:mm5

Keys in admin.date are merged over these defaults per call; pickerAppearance is fixed by the factory. For project-wide formats use createFieldFactories (see Project-wide defaults).

dateTimeField('startsAt', {admin: {date: {displayFormat: 'yyyy-MM-dd HH:mm', timeIntervals: 15}}});

Available factories

textField textareaField emailField numberField checkboxField dateField dateTimeField timeField codeField jsonField richTextField pointField radioField selectField uploadField relationshipField joinField arrayField blocksField rowField groupField collapsibleField tabsField tab uiField

Helpers: createFieldFactories mergeOverrides removeUndefinedDeep

Each factory exports its overrides type (TextFieldOverrides, DateFieldOverrides, …); factories with hasMany also export …Single/…Many field and overrides types.

Building your own factories

mergeOverrides(defaults, overrides, {enforced}?) builds your factory's config from three layers, without mutating any of them: defaults (the caller may change them) → overrides (passed by the caller) → enforced (the caller cannot change them). Each layer is laid over the previous one with the same rules:

  • undefined values in overrides and enforced are ignored; top-level keys from the later layer win.
  • access, admin, admin.components, admin.date and custom are merged one level deep, the later layer wins per key. Passing only access.read keeps your access.update; passing only admin.components.Description keeps your Field. Arrays such as beforeInput/afterInput and anything deeper are replaced.
  • hooks are concatenated per hook name in layer order: defaults hooks, caller hooks, enforced hooks. Put a hook that must run first into defaults, one that must run last into enforced.
  • Everything else (labels, validate, filterOptions, admin.condition …) is replaced.

The built-in factories don't use it: an admin passed to them replaces their defaults.

export const mailToField = (name: string, overrides?: Partial<Omit<TextField, 'type' | 'name'>>) =>
    textField(
        name,
        mergeOverrides({admin: {placeholder: 'mailto:…'}}, overrides, {
            enforced: {validate: validateMailto, hooks: {beforeValidate: [appendMailto]}}, // runs after caller hooks
        }),
    );

Project-wide defaults

createFieldFactories(config) returns all factories with project-wide defaults; without config it returns the originals. Values passed per call always win (label: false too). The returned factories keep their overload types.

  • label: (name) => label sets the label of every named field without its own label. Factories without a name (rowField, tabsField, collapsibleField, unnamed groupField/tab) and uiField stay unchanged.
  • date, dateTime, time are merged into admin.date of dateField, dateTimeField and timeField, over the built-in defaults (dd.MM.yyyy, dd.MM.yyyy HH:mm, HH:mm, interval 5).
const t = createTranslator('collections:events');
const {textField, dateField} = createFieldFactories({label: name => t(name), date: {displayFormat: 'yyyy-MM-dd'}});

textField('title');                 // label: t('title')
textField('slug', {label: false});  // no label
dateField('startDate', {admin: {date: {displayFormat: 'MM/yyyy'}}}); // per call wins

License

MIT

Todo

  • Github Actions

Similar plugins