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

formal

v0.3.0

Published

Simple, flexible node.js form module with casting and validation; inspired by mongoose

Downloads

153

Readme

Formal Build Status Coverage Status Dependency Status

Formal is an awesome Form module for node.js with casting and validation inspired by mongoose!

Example

var Form = require('formal');

var form = new Form({
  name: {
    family: String,
    first: {
      type: [String],
      set: function(val) {return val.split(' ');}
    }
  },
  email: {
    type: String,
    required: true,
    match: /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/,
    attributes: {
      type: 'email'
    }
  },
  age: {type: Number, min: 18}
});

form.set({
  name: {family: 'Martinez'},
  'name.first': 'José Luis Chavez',
  age: 12
});

form.validate(function (err) {
  console.log(err); // missing required email, age to low
  console.log(form.get('name.first.0')); // José
  console.log(form.export());
});

Install

npm install formal --save

Summary

This module focus on form field casting and validation, with suppports for advanced path definition.

It supports

  • field of type: String, Number, Date, Boolean and [of each type] (Arrays)
  • dot.F.notation path à la mongoose to access field and for.arrays.0 too
  • shortcut validators, for required, enum, min, max and match to match mongoose
  • shortcut setters mimicking mongoose for trim, lowercase and uppercase
  • setters & getters
  • custom validators
  • virtuals
  • route-middleware to work seamlessy with express or connect
  • ... and of course it match mongoose but works fine without mongoose

It doesn't

  • render form to html (this will be supported via an external module)

To comes... maybe

  • support for sub-Form
  • support for sub-[Form]
  • incoming npm module to define the form from an existing mongoose schema
  • incoming npm (decorator) module to render the form into html via consolidate.js
  • support browser

API

Summary of the most useful methods.
For a complete list see gh-pages documentation.

new Form(Object:definition[, Object:option]):instance

For connect and express the alternative factory method can be used as a quick helper to create a new instance and return form.middleware() to monkey patch the request and response object.

app.post('/url',
  // sames as (new Form({...})).middleware()
  form({
    fieldA: String
  }),
  function (req, res) {
    console.log(req.body.form);
    console.log(res.locals.form.fieldA.value);
  }
);

form.field(obj:Object):this

Define a new field.

form.set({
  example: {
    'of.a.nested.field': String
  }
});

form.path(path:String):Field (arity 1)

form.path(path:String, obj:Object):this (arity 2)

form.set(path:String|obj:Object[, value])

form.get(path:String):value

form.validate(callback(err):Function)

Validate all fields and return an err object, if any, via the callback function.

form.middleware():Function(req, res, next)

Provide a route-middleware à la connect/express which will monkey patch the req.body.form and res.locals.form.

Test

npm test
Mocha Coverage
npm run-script coverage
On Coveralls.io

All tests are in Coffee-script, hence easy to read! Provides a great way to understand the API ;)

LICENSE

MIT