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

parry

v2.1.0

Published

A structural Node.js validation module

Downloads

2

Readme

parry

npm version Build Status

A structural Node.js validation module.

This module will protect your application from invalid inputs!

Installation

npm install parry

Or, you can use in browser through the browserify.

Usage

var Field = require('parry').Field;
var Form = require('parry').Form;

var UsernameField = Field.extend()
  .type('matches', /[-_a-z0-9]/i)
  .type('isLength', [4, 16])
;
var PasswordField = Field.extend()
  .type('isAlphanumeric')
  .type('isLength', [8, 16])
;
var GenderField = Field.extend({ passIfEmpty: true })
  .type('isIn', ['male', 'female'])
;

var UserForm = Form.extend()
  .field('username', UsernameField)
  .field('password', PasswordField)
  .field('gender', GenderField)
;

// Validate inputs
var inputs = {
  username: 'my-username@',
  password: 'abcd123',
  gender: 'man'
};
var userForm = new UserForm(inputs);
userForm.validate(function(err, validationResult) {
  console.log(validationResult);
  // -> {
  //   isValid: false,
  //   errors: {
  //     username: [ 'Not matched' ],
  //     password: [ 'String is not in range' ],
  //     gender: [ 'Unexpected value' ]
  //   },
  //   reporter: { ErrorReporter instance }
  // }
});

Field

Field.type

You can set the following typical validations.

var SubField = Field.extend();
  .type('isEmail');
  .type('isLength', [4, 64])
;

Field.specify

Use it, in the case of complex validation.

var SubField = Field.extend()
  .specify(function(input, callback) {
    if (input === 'good') {
      callback(null, { isValid: true });
    } else if (input === 'bad') {
      callback(null, { isValid: false, errorMessages: ['It is a bad input'] });
    } else {
      // Error message is 'It is a not good input'
      callback(null, { isValid: false });
    }
  }, 'It is a not good input')
;

Field.passIfEmpty

Pass validation if value is empty.

Default: false

var SubField = Field.extend({ passIfEmpty: true });

Field.shouldValidateAll

Validate all validators already even if error occurs.

Default: false

var SubField = Field.extend({ shouldValidateAll: true });

Field.extend

Create sub class.

var SubField = Field.extend({ passIfEmpty: true, shouldValidateAll: true });

Field.prototype.validate

Validate with input.

field.validate('your input', function(err, { isValid, errorMessages }) {
});

Or, if you use promise:

field.validate('your input').then(..);

Form

Form.field

Set Field sub class with id.

Please see Usage.

Form.extend

Create sub class.

var SubForm = Field.extend({ shouldValidateAll: true });

Form.shouldValidateAll

Validate all fields already even if error occurs

Default: true

var SubForm = Form.extend({ shouldValidateAll: true });

Form.prototype.input

Input a value.

form.input('email', '[email protected]');

Form.prototype.inputs

Input values.

form.inputs({
  email: '[email protected]',
  username: 'foo'
});

Or,

var form = new SubForm({
  email: '[email protected]',
  username: 'foo'
});

Form.prototype.validate

Validate fields with inputs.

Please see Usage.

Or, if you use promise:

form.validate().then(..);

ErrorReporter

DEFAULT_ERROR_MESSAGES