npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

payload-field-validators

v0.2.3

Published

Easy validation handlers for Payload CMS collection's fields

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
}