kfiross

payload field validators

by kfiross
Custom fields
v3
3.29.0
MIT

Overview

Summary generated from the README

Provides helper functions that plug into the validate option of Payload collection fields. A generic validator takes one or more conditions with custom error messages, and ready-made validators cover regex, email, URL and IP address checks on text fields. It can also wrap valibot schemas for more concise conditions. Targets Payload 3.

Features

  • Generic validateField helper accepting a condition and custom error message
  • Multiple conditions per field, each with its own message
  • Regex validator for text fields
  • Email, URL and IP address validators
  • valibotCheck utility to use valibot schemas as conditions
  • Default error message of Invalid input

Use cases

  • Enforce numeric ranges or integer-only values on number fields with clear messages
  • Validate email, URL or IP address entries in text fields
  • Reuse valibot schemas to validate Payload field values

From the author

Easy validation handlers for Payload CMS collection's fields

Installation

npm i payload-field-validators

4

Stars

0

Forks

0

Open Issues

1 years ago

Last Updated

25

Weekly Downloads

v0.2.3

npm Version

20.1 KB

Unpacked Size

1

Dependencies

GitHub stars

40 since Sep 6

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

20+9 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202620
Dec 4, 20267
Dec 3, 20268
Dec 2, 20268
Dec 1, 20269
Nov 30, 20265
Nov 29, 202611
Nov 28, 202611
Nov 27, 202611
Nov 26, 20269
Nov 25, 20269
Nov 24, 20269
Nov 23, 20269
Nov 22, 20264
Nov 21, 20265
Nov 20, 20264
Nov 19, 20266
Nov 18, 202611
Nov 17, 202611
Nov 16, 202613
Nov 15, 202614
Nov 14, 202614
Nov 13, 202619
Nov 12, 202627
Nov 11, 202626
Nov 10, 202625
Nov 9, 202623
Nov 8, 202621
Nov 7, 202620
Nov 6, 202615
Nov 5, 20266
Nov 4, 20262
Nov 3, 20262
Nov 2, 20265
Nov 1, 20264
Oct 31, 20266
Oct 30, 20266
Oct 29, 20265
Oct 28, 20265
Oct 27, 20265
Oct 26, 20263
Oct 25, 20269
Oct 24, 20268
Oct 23, 20268
Oct 22, 20268
Oct 21, 20269
Oct 20, 20269
Oct 19, 20268
Oct 18, 20262
Oct 17, 20262
Oct 16, 20262
Oct 15, 20263
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, 20262
Oct 4, 20261
Oct 3, 20263
Oct 2, 20267
Oct 1, 20267
Sep 30, 20267
Sep 29, 202612
Sep 28, 202611
Sep 27, 202612
Sep 26, 202613
Sep 25, 202611
Sep 24, 202611
Sep 23, 202613
Sep 22, 202613
Sep 21, 202613
Sep 20, 202613
Sep 19, 202610
Sep 18, 20268
Sep 17, 202610
Sep 16, 202610
Sep 15, 20265
Sep 14, 20267
Sep 13, 20267
Sep 12, 20267
Sep 11, 202612
Sep 10, 202610
Sep 9, 20269
Sep 8, 202612
Sep 7, 202612
Sep 6, 202611

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

Health Score

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

README

payload-field-validators

Easy validation handlers for Payload CMS collection's fields

Usage

Quick start

Assuming we have a Collection

export default MyCollection: CollectionConfig = {
  // other fields..
    {
      name: "number_field",
      type: "number",
    },
    {
      name: "text_field",
      type: "text",
    }
    // other fields..
}

We can add a validator to TextField:

{
  // other configs..
  validate: (value: any) =>
    validateField({
      condition: value.length < 100,
    })
}

Similarly, we can add a validator to NumberField:

{
  // other configs..
  validate: (value: any) =>
    validateField({
      condition: value > 100,
    })
}

The default message will be "Invalid input"

Advanced

  • The default error message could be overridden using the message param:
{
    // other field's configs..
    name: "percentage_usage",
    validate: (value: any) => validateField({
      condition: value > 20 and value < 50,
      message: "percentage must be between 20 to 50 %"
  })
}
  • We can leverage multiple validator to NumberField and for each case to show appropriate message:
{
  name: "grade",
  type: "number",
  required: true,
  validate: (value: any) => validateField([
      {
        condition: value >= 55,
        message: "Grade must be at minimum 55"
      },
      {
        condition: typeof value === "number" && Number.isInteger(value),
        message: "Grade must be an Integer"
      }
  ])
},

Using Validations libraries

You can use form validation libraries like valibot/zod/vest to write more concise conditions

Valibot Example

using valibotCheck utility

// import valibot
import * as v from "valibot";

//....
{
  name: "grade",
  type: "number",
  required: true,
  validate: (value: any) => validateField({
      condition: valibotCheck(v.pipe(v.number(), v.integer(), v.minValue(55), value)),
      message: "Grade must be an Integer, minimum 55"
  })
},

Specific use-cases

  • Adding Regex validator to TextField
const onlyLettersRegex = /^[a-zA-Z0-9]+$/;
export default MyCollection: CollectionConfig = {
    {
      name: "name",
      type: "text",
      admin: {
      readOnly: false,
        hidden: false
    },
    validate: (value: any) => validateRegex(
      onlyLettersRegex,
      value,
      "Name must not contain special characters"   // (optional error message)
    ),
    required: true
}
  • Adding Email validator to TextField
export default MyCollection: CollectionConfig = {
    {
      name: "email",
      type: "text",
      validate: (value: any) => validateEmail(value, optionaLmessage),    // --> add this line
      required: true
}
  • Adding URL validator to TextField
export default MyCollection: CollectionConfig = {
    {
      name: "url",
      type: "text",
      admin: {
      readOnly: false,
        hidden: false
    },
    validate: (value: any) => validateUrl(valuem optionaLmessage),    // --> add this line
    required: true
}
  • Adding IP Address validator to TextField
export default MyCollection: CollectionConfig = {
    {
      name: "website_url",
      type: "text",
      admin: {
      readOnly: false,
        hidden: false
    },
    validate: (value: any) => validateIPAddress(value),    // --> add this line
    required: true
}

Similar plugins