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

serializable-form

v1.0.4

Published

Serializable form validation for universal web apps

Downloads

7

Readme

serializable-form

install

$ npm install serializable-form --save

why?

  • Writing form validation on the client side and the server-side is a pain.
  • Creating bespoke form validation over and over is tedious and prone to mistake.
  • Many validation libraries do too much.

examples

creating a form

const form = require('serializable-form')

const userCreateForm = form({
  helpers: {
    async usernameTaken (username) {
      const response = await fetch(`/taken?username=${username}`)
      const { taken } = await response.json()
      return taken
    }
  },
  fields: {
    username: [
      form.validator(value => value.length >= 5, 'too short'),
      form.validator(value => value.length <= 64, 'too long'),
      form.validator(async (value, helpers) => {
        const taken = await helpers.usernameTaken(value)
        if (taken) throw new Error('already taken')
        return true
      })
    ],
    password: [
      form.validator(value => value.length < 12, 'too short'),
      form.validator(value => value.match(/[a-zA-Z]/g) !== null, 'must contain a letter'),
      form.validator(value => value.match(/[0-9]/g) !== null, 'must contain a number'),
      form.validator(value => value.match(/[A-Z]/g) !== null, 'must contain a capital letter')
    ]
  }
})

serializing a form

await userCreateForm()
// {
//   valid: null,
//   fields: {
//     username: { valid: null, errors: [] },
//     password: { valid: null, errors: [] }
//   }
// }

serializing a form entry with invalid data

await userCreateForm({ username: 'test', password: 'abcdef' })
// {
//   valid: false,
//   fields: {
//     username: {
//       value: 'test',
//       valid: false,
//       errors: [
//         'too short',
//         'already taken'
//       ]
//     },
//     password: {
//       value: 'abcdef',
//       valid: false,
//       errors: [
//         'must contain a number',
//         'must contain a capital letter'
//       ]
//     }
//   }
// }

serializing a form entry with valid data

await userCreateForm({ username: 'zuren', password: 'Password1234' })
// {
//   valid: true,
//   fields: {
//     username: {
//       value: 'zuren',
//       valid: true,
//       errors: []
//     },
//     password: {
//       value: 'Password1234',
//       valid: true,
//       errors: []
//     }
//   }
// }

validating an individual field

await userCreateForm.validate('username', 'test')
// { valid: false, errors: [ 'too short' ] }

await userCreateForm.validate('username', 'zuren')
// { valid: true, errors: [] }

helpers can be overridden, which allows for easy universal use

Would recommend only overriding server-side to keep server code out of your client bundles.

userCreateForm.helpers.usernameTaken = async username => {
  return User.usernameTaken(username)
}

api

form(options) -> entry

Returns a function (lets call it entry) which

entry([options]) -> Object

entry is the return value of form. When calling entry with no arguments, returns an unvalidated schema hash of the form, which can be serialized. When calling entry with a hash of fields and values, returns a validated entry hash.

form.validator(predicate, [errorMessage]) -> validate

Returns a validate function, which will use the passed error message if available, or any errors thrown if not.

validate(value) -> Object

Calls predicate with value. If predicate returns true, it will return a 'valid' hash with no errors. If the predicate returns false, it will return an 'invalid' hash with the errorMessage provided. If the predicate throws an Error, it will use the error.message instead.

  • Valid: { valid: true, errors: [] }
  • Invalid: { valid: false, errors: ['too long'] }