bhofstaetter

payload utilities

by bhofstaetter
Testing & developer experience
v3
>=3
MIT

Overview

Summary generated from the README

A small utility library for Payload CMS projects, not a config plugin. It offers a validator composer that runs custom validators before a Payload default validator, helpers that pull IDs from values that may be IDs or populated documents, and helpers that resolve a value to a full document through a fetch callback. Requires Payload 3 or later.

Features

  • createValidator combines custom validators with a Payload default validator and short-circuits on the first failure
  • Custom validators run only for present values, while null, empty strings and empty arrays go to the default validator
  • Optional formatError turns non-string validator errors into messages, for example translated keys
  • extractId and extractIds return IDs from IDs or objects with an id property
  • getDocument and getDocuments return populated documents or fetch them by ID through a callback

Use cases

  • Validate a date range field against its sibling start date with typed custom validators
  • Normalize relationship values that may be populated documents or plain IDs
  • Translate validation errors in one place using the request translation function

From the author

Utility functions for Payload CMS — extract IDs, resolve documents, and compose field validators

Installation

npm i payload-utilities

0

Stars

0

Forks

0

Open Issues

5 days ago

Last Updated

238

Weekly Downloads

v1.2.0

npm Version

10.4 KB

Unpacked Size

0

Dependencies

GitHub stars

00 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20260
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
Nov 28, 20260
Nov 27, 20260
Nov 26, 20260
Nov 25, 20260
Nov 24, 20260
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
npm weekly downloads

238+231 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026238
Dec 5, 2026199
Dec 4, 2026194
Dec 3, 2026180
Dec 2, 20262
Dec 1, 20267
Nov 30, 20267
Nov 29, 20266
Nov 28, 20266
Nov 27, 20266
Nov 26, 20266
Nov 25, 20266
Nov 24, 20261
Nov 23, 20261
Nov 22, 20263
Nov 21, 20263
Nov 20, 20263
Nov 19, 20264
Nov 18, 20268
Nov 17, 20268
Nov 16, 202610
Nov 15, 202611
Nov 14, 202612
Nov 13, 202612
Nov 12, 202615
Nov 11, 202611
Nov 10, 202611
Nov 9, 20269
Nov 8, 20267
Nov 7, 20266
Nov 6, 20265
Nov 5, 20261
Nov 4, 20261
Nov 3, 20262
Nov 2, 20263
Nov 1, 20263
Oct 31, 20263
Oct 30, 20263
Oct 29, 20263
Oct 28, 20263
Oct 27, 20263
Oct 26, 20266
Oct 25, 20266
Oct 24, 20267
Oct 23, 20267
Oct 22, 202610
Oct 21, 202610
Oct 20, 20269
Oct 19, 20265
Oct 18, 20264
Oct 17, 20264
Oct 16, 20264
Oct 15, 20262
Oct 14, 20262
Oct 13, 20262
Oct 12, 20262
Oct 11, 20263
Oct 10, 20262
Oct 9, 20262
Oct 8, 20261
Oct 7, 20261
Oct 6, 20261
Oct 5, 20261
Oct 4, 20260
Oct 3, 20261
Oct 2, 20263
Oct 1, 20263
Sep 30, 20265
Sep 29, 20269
Sep 28, 20269
Sep 27, 202610
Sep 26, 202612
Sep 25, 202610
Sep 24, 202610
Sep 23, 20268
Sep 22, 20268
Sep 21, 20268
Sep 20, 20268
Sep 19, 20265
Sep 18, 20265
Sep 17, 20266
Sep 16, 20267
Sep 15, 20263
Sep 14, 20265
Sep 13, 20264
Sep 12, 20268
Sep 11, 20268
Sep 10, 20267
Sep 9, 20267
Sep 8, 20267
Sep 7, 20267

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/1176252947-root.svg)](https://payloaddirectory.dev/plugins/1176252947-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1176252947-root"><img src="https://payloaddirectory.dev/badge/1176252947-root.svg" alt="Payload Directory"></a>
Updated Nov 30, 2026Created Mar 8, 2026Published Nov 30, 2026

README

Payload Utilities

Utility functions for Payload CMS — type-safe validators, ID extraction, and document resolution.

Installation

npm install payload-utilities

Usage

createValidator

Creates a composite field validator by combining custom validators with a Payload default validator. Custom validators run first — if any fails, it short-circuits. If all pass, the default validator runs.

import type {DateField} from 'payload';
import {date} from 'payload/shared';
import {createValidator, type CustomValidator} from 'payload-utilities';

type Data = Task;
type SiblingData = Data['schedule']['timeframe'];
type Value = SiblingData['end'];
type FieldConfig = DateField;

type Validator = CustomValidator<Data, SiblingData, Value, FieldConfig>;

const validateEndDate: Validator = (value, options) => {
    const start = options.siblingData?.start;

    if (!start) {
        return 'Start- & Ende des Einplanungszeitraum sind Pflichtfelder';
    }

    const startDate = new Date(start);
    const endDate = new Date(value);

    if (endDate.getTime() < startDate.getTime()) {
        return 'Das Ende des Einplanungszeitraum darf nicht vor dem Start liegen';
    }

    return true;
};

const validateStartDate: Validator = (value, options) => {
    // ...
};


export const timeframeValidator = createValidator<Data, SiblingData, Value, FieldConfig>({
    defaultValidator: date,
    validators: [validateEndDate, validateStartDate],
});

Custom validators only run for present values. null, undefined, '' and empty arrays go straight to the default validator; 0 and false are validated.

Formatting errors with formatError

Validators may return any error type instead of a string. formatError turns it into the message, e.g. to translate keys in one place. It is required when the error type is not a string.

type Error = {key: MyTranslationKey; values?: Record<string, unknown>};
type Validator = CustomValidator<Data, SiblingData, Value, FieldConfig, DefaultDocumentIDType, Error>;

const isUnique: Validator = async (value, options) =>
    (await isTaken(value)) ? {key: 'validation:notUnique'} : true;

export const validateTitle = createValidator<Data, SiblingData, Value, FieldConfig, DefaultDocumentIDType, Error>({
    defaultValidator: text,
    validators: [isUnique],
    formatError: (error, {req}) => req.t(error.key, error.values),
});

extractId / extractIds

Extract IDs from values that are either an ID or an object with an id property.

import {extractId, extractIds} from 'payload-utilities';

// Single values
extractId(1);              // 1
extractId('abc');          // 'abc'
extractId({id: 1});        // 1

// Arrays
extractIds([1, {id: 2}]);  // [1, 2]

getDocument / getDocuments

Resolves a value that is either a full document or an ID. If an object is passed, it is returned directly. If an ID is passed, the callback is called to fetch the document.

import {getDocument} from 'payload-utilities';

const post = await getDocument(idOrPost, (id) =>
    payload.findByID({collection: 'posts', id}),
);
import {getDocuments} from 'payload-utilities';

const posts = await getDocuments(idsOrPosts, (ids) =>
    payload.find({collection: 'posts', where: {id: {in: ids}}}).then(result => result.docs),
);

License

MIT

Todo

  • Better Labels
  • Github Actions

Similar plugins