kfiross

payload field validators

by kfiross
Testing & developer experience
v3
3.29.0
MIT

Overview

Summary generated from the README

Provides utility functions that plug into the validate option of Payload fields. Developers pass conditions with custom messages, or use ready-made validators for regex, email, URL and IP address. It also includes a helper to run valibot checks inside a condition.

Features

  • validateField accepts one condition or an array of conditions with custom error messages
  • Default error message of Invalid input that can be overridden
  • Regex validator for text fields
  • Email, URL and IP address validators
  • valibot check utility for concise conditions

Use cases

  • Enforce numeric ranges or integer-only values on number fields
  • Validate that text fields contain emails, URLs or IP addresses
  • Show specific error messages for each failed rule on a field

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

0

Weekly Downloads

v0.2.3

npm Version

20.1 KB

Unpacked Size

1

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

33/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