byte5digital

payload assist

by byte5digital
APIs & SDKs
v3
^3.0.0
MIT

Overview

Summary generated from the README

Wraps your Payload config to validate it against configurable rules at boot, such as kebab-case slugs, GraphQL being disabled, and DTO usage. Provides a base Dto class, transformAndValidate, a withResponse endpoint wrapper and a withDtoReadHook afterRead hook so only exposed fields leave the API. Requires Payload 3 and Next 15 or later.

Features

  • payloadAssist wrapper validates the raw config against a customizable rule set at startup
  • Built-in rules for disabling GraphQL and enforcing withResponse and withDtoReadHook
  • Dto base class with transformAndValidate based on class-transformer by default
  • withResponse wrapper enforces DTO-only JSON responses in custom endpoints
  • withDtoReadHook selects a DTO per read based on conditions such as user role
  • AccessControl helper type covering all collection access methods

Use cases

  • Restrict API responses to explicitly exposed fields and nested DTOs
  • Fail the build when collections or endpoints break project conventions
  • Return different response shapes for admins and public users from the same collection

From the author

A TypeScript library providing DTO utilities and validation hooks for Payload CMS

Installation

npm i @byte5digital/payload-assist

0

Stars

0

Forks

0

Open Issues

7 months ago

Last Updated

21

Weekly Downloads

v1.2.8

npm Version

40.3 KB

Unpacked Size

2

Dependencies

GitHub stars

00 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
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
Sep 6, 20260
npm weekly downloads

14−16 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202614
Dec 4, 20267
Dec 3, 20267
Dec 2, 20267
Dec 1, 202611
Nov 30, 202610
Nov 29, 202611
Nov 28, 202611
Nov 27, 202610
Nov 26, 20269
Nov 25, 20269
Nov 24, 20266
Nov 23, 20266
Nov 22, 20267
Nov 21, 202612
Nov 20, 202627
Nov 19, 202633
Nov 18, 202640
Nov 17, 202636
Nov 16, 202637
Nov 15, 202649
Nov 14, 202644
Nov 13, 202630
Nov 12, 202627
Nov 11, 202636
Nov 10, 202639
Nov 9, 202641
Nov 8, 202629
Nov 7, 202633
Nov 6, 202636
Nov 5, 202634
Nov 4, 202619
Nov 3, 202616
Nov 2, 202616
Nov 1, 202614
Oct 31, 202610
Oct 30, 20266
Oct 29, 20265
Oct 28, 20264
Oct 27, 20264
Oct 26, 20262
Oct 25, 20262
Oct 24, 20263
Oct 23, 20263
Oct 22, 202618
Oct 21, 202626
Oct 20, 202626
Oct 19, 202625
Oct 18, 202630
Oct 17, 202628
Oct 16, 202628
Oct 15, 202614
Oct 14, 20266
Oct 13, 20266
Oct 12, 20266
Oct 11, 20263
Oct 10, 20263
Oct 9, 20263
Oct 8, 20262
Oct 7, 20262
Oct 6, 20266
Oct 5, 20267
Oct 4, 20266
Oct 3, 202612
Oct 2, 202615
Oct 1, 202618
Sep 30, 202618
Sep 29, 202614
Sep 28, 202613
Sep 27, 202616
Sep 26, 202655
Sep 25, 202657
Sep 24, 202655
Sep 23, 202657
Sep 22, 202658
Sep 21, 202661
Sep 20, 202663
Sep 19, 202618
Sep 18, 202615
Sep 17, 202614
Sep 16, 202616
Sep 15, 202615
Sep 14, 202613
Sep 13, 202620
Sep 12, 202632
Sep 11, 202638
Sep 10, 202638
Sep 9, 202638
Sep 8, 202640
Sep 7, 202641
Sep 6, 202630

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

Health Score

46/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/1049574899-root.svg)](https://payloaddirectory.dev/plugins/1049574899-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1049574899-root"><img src="https://payloaddirectory.dev/badge/1049574899-root.svg" alt="Payload Directory"></a>
Updated Apr 27, 2026Created Sep 3, 2025Published Apr 27, 2026

README

Assist for Payload
Assist for Payload Tests passing License MIT

Assist for Payload

Utilities to add guardrails, DTO tooling, and ergonomic rules to Payload CMS projects.

  • Rules: Validate your Payload config at boot (e.g., kebab-case slugs).
  • DTOs: First-class helpers to define, transform, validate, and enforce DTO-only responses.
  • Ergonomics: Thin wrappers to keep endpoints and collection reads consistent and secure.

Installation

yarn add @byte5digital/payload-assist
# or
npm install @byte5digital/payload-assist

Peer deps: Payload v3+, Next v15+. Dependencies class-transformer and reflect-metadata are included in the package.

API overview

  • payloadAssist(payloadConfig, options?): Main function that initializes Assist for Payload, validates your payload config against defined rules, and returns the built config.
  • defaultConfig: Default config with built-in rules and transformAndValidate function (can be spread/overridden).
  • Dto: Base abstract class for response DTOs.
  • transformAndValidate(Dto, data): Validate Payload data and transform to DTO. Uses the configured transformer/validator.
  • withResponse(handler): Enforce DTO-only JSON responses in your custom payload endpoints.
  • withDtoReadHook([{ dto, condition }, { dto }]): Attach to collections to return DTOs from read operations with conditional DTO selection.

Helper Types

AccessControl

A comprehensive type for Payload collection access control that includes all available access control methods.

import { AccessControl } from "@byte5digital/payload-assist";

export const MyCollection: CollectionConfig = {
  slug: "my-collection",
  access: {
    admin: ({ req }) => req.user?.role === "admin",
    create: ({ req }) => req.user?.role === "admin",
    read: ({ req }) => true, // public read
    update: ({ req }) => req.user?.role === "admin",
    delete: ({ req }) => req.user?.role === "admin",
  } satisfies AccessControl,
  // ...fields
};

Usage

Initialize Assist for Payload

The main payloadAssist function initializes the library, validates your payload config against defined rules, and returns the built config. You can customize the ruleSet and transformAndValidate function through options. payloadAssist is implemenented as a wrapper function and not as a payload plugin, to ensure it validates the raw config that is set by the user, instead of the config that was previously processed by other plugins and enriched by payload.

  • ruleSet: An object map of named rules; merge defaults with your own, if required. Deactivate a default rule by setting the rule to false.
  • rules: (config: payloadConfig) => boolean | void; throw to fail with an actionable message and return true if the rule is satisfied.
  • transformAndValidate: (dto: Dto, data: unknown) => Dto Turn raw Payload data into typed DTOs.

Built-in rules:

  • disableQraphQL: Ensures GraphQL is disabled in your config
  • collectionsEndpointsUseWithResponse: Ensures all collection endpoints use withResponse
  • collectionsUseWithDtoReadHook: Ensures all collections use withDtoReadHook in their afterRead hooks
import { buildConfig } from "payload";
import payloadAssist, {
  defaultConfig,
  PayloadAssistError,
} from "@byte5digital/payload-assist";

export default payloadAssist(
  {
    // your Payload config
  },
  {
    ruleSet: {
      ...defaultConfig.ruleSet,

      // add/override rules here
      secretIsSet: (config) => {
        if (config.secret?.length > 0) return true;
        throw new PayloadAssistError("A secret needs to be set");
      },
    },
  }
);

DTOs: Purpose and usage

Define exactly what leaves your API by modeling responses as DTOs. Only explicitly exposed fields and nested DTOs are serialized. It is important that all DTOs extend the Dto class. The example below shows the usage with the default transformAndValidate.

import { Dto, Expose, Type } from "@byte5digital/payload-assist";

export class MediaResponse extends Dto {
  @Expose() url: string;
  @Expose() mimeType: string;
}

export class UserResponse extends Dto {
  @Expose() id: number;
  @Expose() name: string;
}

export class MyCollectionDto extends Dto {
  @Expose() name: string;
  @Expose() @Type(() => MediaResponse) image: MediaResponse;
  @Expose() @Type(() => UserResponse) owner: UserResponse;
}

Transform any raw Payload doc into a DTO. By default transformAndValidate uses class-transformer, but it can be configured through the payloadAssist options.

import { transformAndValidate } from "@byte5digital/payload-assist";

const payloadDoc = await getPayloadDoc();
const dto = transformAndValidate(MyCollectionDto, payloadDoc);

Enforcing DTO-only responses in endpoints

Use withResponse to guarantee your endpoints return DTOs (and nothing else). It centralizes transform/serialize and standardizes error responses. The transformation strategy is configurable.

import payload from "payload";
import {
  withResponse,
  transformAndValidate,
} from "@byte5digital/payload-assist";
import { MyDataDto } from "path/to/dtos";

export const MyCollection: CollectionConfig = {
  slug: "my-collection",
  endpoints: [
    {
      path: "/my-custom-endpoint",
      method: "get",
      handler: withResponse(async (req: PayloadRequest) => {
        const myData = await req.payload.find({
          collection: "my-collection",
          // ...additional filter logic
        });

        const myDataDto = transformAndValidate(MyDataDto, myData);
        return {
          response: myDataDto,
          status: 200,
        };
      }),
    },
  ],

  // ...fields
};

Enforcing DTO-only responses from collections

Attach withDtoReadHook to your collections afterReadHooks to transform read results automatically to the given DTOs. Multiple objects with DTOs can be passed, when they include a condition, only the last item can be without a condition. The first DTO where the condition is met will be used or the DTO without a condition, if given. If none applies, null will be returned. So the order of the given DTOs should be: More specific first, default last.

// src/collections/MyCollection.ts
import { CollectionConfig } from "payload/types";
import { withDtoReadHook } from "@byte5digital/payload-assist";
import { MyCollectionDto, MyCollectionAdminDto } from "path/to/dtos";

export const MyCollection: CollectionConfig = {
  slug: "my-collection",
  hooks: {
    afterRead: [
      withDtoReadHook(
        {
          dto: MyCollectionAdminDto,
          condition: ({ req: { user } }) => user?.role === "admin",
        },
        {
          dto: MyCollectionDto,
        }
      ),
    ],
  },
  // ...fields
};

License

MIT

About byte5

We're a development company based in Frankfurt, Germany — remote-friendly, open-minded, and tech-driven. Our team brings deep expertise in Node.js, MedusaJS, Laravel, Umbraco, and decentralized tech like IOTA. We collaborate with clients who care about clean code, scalable solutions, and long-term maintainability.

We contribute to open source, run Laravel DACH Meetups, and support developer communities across the DACH region. Our expertise in e-commerce platforms makes us the perfect partner for building robust, scalable solutions.

If you love building smart solutions with real impact — we should talk.

Connect with us:

Support


Built with 🩵 by byte5

Similar plugins