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.
Utility functions for Payload CMS — extract IDs, resolve documents, and compose field validators
Installation
npm i payload-utilities0
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
Trends appear after 7 days of data. Daily snapshots have not started yet.
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
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1176252947-root)<a href="https://payloaddirectory.dev/plugins/1176252947-root"><img src="https://payloaddirectory.dev/badge/1176252947-root.svg" alt="Payload Directory"></a>README
Utility functions for Payload CMS — type-safe validators, ID extraction, and document resolution.
npm install payload-utilities
createValidatorCreates 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 / extractIdsExtract 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 / getDocumentsResolves 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),
);
MIT
payload plugin tooling
Shared lint, format, TypeScript, build, test and CI config for building Payload plugins
650/wk0v4
payload utilities
Developer helpers for Payload: document traversal, flattening, transformation and fully populated type generation
610/wk15v3
payload schema
Define Payload field validation once and reuse it as Payload fields and Zod schemas
570/wk2v3
payload cli
Command-line access to Payload collections and globals through the Local API, aimed at AI agents and developers
570/wk13v3
payload dev tools
Floating dev toolbar, /dev pages and an app snapshot for AI agents in Payload projects on Next.js
560/wk18v3
payload field factories
Typed factory functions for writing Payload field configs more concisely
502/wk0v3