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

@bouncingpixel/pixel-validate

v0.2.1

Published

Server and browser side validation when working with Mongoose models.

Downloads

5

Readme

pixel-validate

Server and browser side validation when working with Mongoose models. Server side uses is an Express Middleware. Browser side uses a jQuery plugin.

Working With

Requirements

  • NodeJS 6 LTS
  • webpack or similar for browser side
  • Mongoose v4.x server side and browser side
  • Recommend using either CDN or self-hosted version 4.0.2 of the mongoose.js for browser
  • jQuery for the browser version only

Using pixel-validate

Export your schemas separately from the models as this package works directly with schemas.

Server side

Import the server side portion and use the middleware in any route. You may need to do some data manipulation before running the middleware, especially with arrays. If the validation fails, the error is set as a 400 Bad Request. This error could be caught by an Express error handling middleware or sent to the client as is.

const ValidateMiddlware = require('@bouncingpixel/pixel-validate').ValidateMiddlware;
const MySchema = require('../../schemas/my-schema');

router.post(
  '/edit-thing/:id',

  // do data manipulation before validation to deserialize arrays
  MyController.thingEditDeserialize,

  // do the validation
  ValidateMiddlware(MySchema, '/admin/things'),

  // handle after everything is ok
  MyController.editThing
);

Browser side

Requires webpack or similar to handle require statements. jQuery and Mongoose's 4.0.2 browser mongoose.js should be exposed externally or bundled.

After that, just import pixel-validate, your schemas, and attach the validator to your form.

The validator will automatically add the classes valid and invalid to fields. Fields will be validated when focus is lost or when a form button is clicked. Fields that contain a name attribute will use the name to find the right path in the schema. Fields may also use the attribute data-validate-path to specify an exact path. This attribute may be useful for fields which are not sent to the server and should not have a name.

Fields may also use the following data attributes to adjust their behavior:

  • data-is-array: If the field is a comma separated list of values, the field will be deserialized into an array
  • data-empty-as-null: Treats the field as null if the field is empty
  • data-skip-validation: Ignores any validation errors on the field
  • data-ignore-empty: Ignores the field if it is empty

All submit buttons will be caught, validated, and then the button fired again to perform it's default action. This module does not catch the form submit event. This is done to avoid using Ajax to submit forms and also to make sure the button's name is attached to the body.

Optionally, if additional data processing must occur prior to validation, use the optional function postSerialize.

requirerequire('@bouncingpixel/pixel-validate');
const MySchema = require('../../schemas/my-schema.js');

$('#myform').pixelValidate(MySchema, {
  postSerialize: function(data) {
    // do things to data
    // returning is optional if directly manipulating data. otherwise, return the new data object.
    return data;
  }
});