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 rather than a config plugin. It provides a validator composer that runs custom validators before a Payload default validator, helpers that pull IDs from values that are either an ID or a populated object, and helpers that resolve a value to a full document by calling a fetch callback. Targets Payload 3 and later and is typed for TypeScript.

Features

  • createValidator combines custom validators with a Payload default validator and short-circuits on the first failure
  • extractId and extractIds return IDs from plain IDs or objects with an id property
  • getDocument resolves an ID or document to a full document using a fetch callback
  • getDocuments resolves arrays of IDs or documents with a single batched fetch
  • Type-safe generics for data, sibling data, value and field config

Use cases

  • Validate a date range field by checking its end date against a sibling start date before the default date validator
  • Normalize relationship values that may be populated or unpopulated into plain IDs
  • Resolve relationship values to full documents only when they are not already populated

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

7 months ago

Last Updated

6

Weekly Downloads

v1.1.1

npm Version

9.0 KB

Unpacked Size

0

Dependencies

GitHub stars

Trends appear after 7 days of data. Daily snapshots have not started yet.

npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

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

Health Score

43/100Fair

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 Mar 19, 2026Created Mar 8, 2026Published Mar 19, 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],
});

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