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 🙏

© 2026 – Pkg Stats / Ryan Hefner

stimulus-validation

v1.0.1-beta.3

Published

Validation controller for Stimulus.js

Readme

Stimulus Validation

stimulus-validation helps validate user input and helps you display errors.

It does not dictate how the errors are displayed; instead, all errors are stored in the errors object, and it is up to the developer how to display them. Stimulus-validation has two types of validations, built-in validators, and custom validators. The built-in validators are a wrapper for Validatejs so you can all Validatejs' validators expect equality validator.

Codepen Demo

Installation

  yarn install stimulus-validation

Basic Usage

Let's build a simple signup form with name and email fields.

Controller

In your controller extend validation-controller

// controllers/signup_validation_controller.js

import { ValidationController } from "stimulus-validation"

export default class extends ValidationController {}

We want to make the name and email required and also validate the email.

So we need to define these rules. In the controller add:

  static rules = {
    email: { presence: { allowEmpty: false }, email: true},
    name: { presence: { allowEmpty: false } }
  }

Each key in rules corresponds to an attribute defined in the markup.

HTML

  <form data-controller="signup-validation" data-action="submit->signup-validation#validateAll">
    <div class="field">
      <input
        type="text"   data-action="blur->signup-validation#validate"
        data-attr="signup-validation.name"
      >
      <div class="error-message"></div>
    </div>

    <div class="field">
      <input
        type="text"   data-action="blur->signup-validation#validate"
        data-attr="signup-validation.email"
      >
      <div class="error-message"></div>
    </div>
  </form>

On each field you have to add data-attr it uses the same syntax as data-target identifier.ATTRNAME

There are two methods validate and validateAll

validate validates a single input and validateAll validates the entire form and prevents form submission if an error is found.

displaying errors

To display errors, we can utilize afterValidate callback.

  afterValidate({ el, attr }) {
    this.errorMessageEl(el).textContent = this.errorMessage(attr)
  }

  errorMessageEl(el) {
    return el.closest(".field").querySelector(".error-message")
  }

  errorMessage(attr) {
    return this.errors.has(attr) ? this.errors.get(attr)[0] : ""
  }

The important part is what's happening in the errorMessage method. We check if the errors object has error messages for the attr if it has we return the first error message.

Custom validators

To define a custom validator, you first write a method that adds a message to the errors object

Let's say we want to validate the input length to be equal to 10.

  validLength({attr, value}) {
    if(value.length !== 10) {
	    this.errors.add(attr, 'must be 10 chars long')
    }
  }

After defining the validator we need to register it and specify the attributes the validator should validate.

  static validators = { validLength: { attributes: ['someAttr' ]  } }

TODO

Rewrite README

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jwald1/stimulus-validation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

This package is available as open source under the terms of the MIT License.