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 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-formdata

v2.1.0

Published

Data structure for validating form data

Downloads

17

Readme

validate-formdata

Data structure for validating form data.

Features

  • minimal GC pressure
  • optimized for unidirectional rendering
  • framework agnostic
  • only a data structure, no opinions on UI
  • about 50 lines of code
  • creates a window.FormData object, ready for XHR

Usage

var html = require('choo/html')
var choo = require('choo')
var xhr = require('xhr')

var validateFormdata = require('validate-formdata')

var app = choo()
app.route('/', function (state, emitter) {
  var pristine = state.form.pristine
  var errors = state.form.errors
  var values = state.form.values

  return html`
    <body>
      <form onsubmit=${emitter.bind(emitter, 'submit')}>
        <label for="name">
          ${errors.name && !pristine.name ? errors.name.message : 'valid'}
        </label>
        <input name="name"
          type="text"
          autofocus
          value=${values.name}
          placeholder="name"
          onchange=${validate}>
        <label for="password">
          ${errors.password && !pristine.password ? errors.password.message : 'valid'}
        </label>
        <input name="password"
          type="password"
          value=${values.password}
          placeholder="password"
          onchange=${validate}>
        <input name="submit"
          disabled=${!state.form.valid}
          type="submit"
          value="Login">
      </form>
    </body>
  `

  function validate (e) {
    emitter.emit('validate', e)
  }
})

app.use(function (state, emitter) {
  var validator = validateFormdata()
  state.form = validator.state

  validator.field('name', function (data) {
    if (!data) return new Error("name can't be empty")
    if (!(data instanceof String)) return new Error('name should be a string')
    if (data.length < 6) return new Error('name should be at least 6 characters')
  })

  validator.field('password', function (data) {
    if (!data) return new Error("password can't be empty")
    if (!(data instanceof String)) return new Error('password should be a string')
    if (data.length < 6) return new Error('password should be at least 6 characters')
  })

  emitter.on('validate', function (e) {
    validator.validate(e.target.name, e.target.value)
    emitter.emit('render')
  })

  emitter.on('submit', function (e) {
    if (!state.form.valid) throw new Error('form not valid')

    var opts = { body: validator.formData() }
    xhr.post('/my-url', opts, function (err, res) {
      if (err) throw err
      console.log(res)
    })
  })
})
app.mount('body')

API

validator = validateFormdata()

Create a new instance.

validator.state

The state object is meant to be passed directly into the UI for rendering:

  • validator.state.pristine[key]: Check if the key has been validated before.
  • validator.state.errors[key]: Check if there's an error for the key.
  • validator.state.values[key]: Get the value from the key.
  • validator.state.valid: Check if the form is valid.
  • validator.state.changed: Check if the form was changed.

validator.field(key, [opts], validateFunction)

Create a new field validation function for the given key. The validation functions should either return nothing, or an Error object. The .message property from the error can be used when rendering.

validator.field(key, [opts], validateFunction)

Create a new file validation function for the given key. The validation functions should either return nothing, or an Error object. The .message property from the error can be used when rendering. Opts can contain:

  • required: default: true. Determine if the field is required to pass.

validator.validate(key, value)

Validate data. The first time the validate function is called for a key it sets the corresponding state.pristine[key] to false. state.valid is set to true when all values are valid.

  • required: default: true. Determine if the field is required to pass.

validator.formData()

Return a window.FormData instance from the form. Can be used to send Multipart data into an XHR request. Make sure state.valid is true before calling this.

validator.changed

Check if the form has changed to determine if it should be re-rendered. An example of a change is: "the form didn't have an error, and now it does." This value will be false if only new data was added.

See Also

License

MIT