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

feathers-validator

v1.0.4

Published

A validator for Feathers services.

Downloads

378

Readme

#feathers-validator

WIP. Validator for Feathers services, or any service.

require('feathers-validator') will export a class. Simply instantiate it with your form data and validation rules. The form data and validation rules should be objects. The errors() method returns a string array containing any validation errors encountered.

Example:

var feathers = require('feathers');

var myService = {
	create: function(data, params, callback) {
			var Validator = require('feathers-validator');
			var validator = new Validator(data, {
				username: 'required|max:255',
				password: 'required|min:6',
				email: 'required|email',
				add_to_mailing_list: 'required|boolean'
			});
			
			var errors = validator.errors();
			
			if (errors.length == 0) {
				//Request is valid! Do stuff safely, without
				//breaking your app!
				//...
				callback(null, {error: 'success'});
			} else {
				//Validation errors occurred.
				//...
				callback(null, {error: 'failure', errors: errors});
			}
		}
}

var app = feathers().configure(feathers.rest());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', myService);

module.exports = app;

##Rules

Can take any of the following forms: 1. A string, with rules separated by pipes (|). Rule parameters are denoted by a colon (:) and separated by commas (,). Example:

'required|min:6|rule:foo,bar'

An object, where every rule is an object. Rule parameters take the form of arrays. Example:

{
    required: true,
    min: 6,
    rule: ['foo', 'bar']
}

##Available Validation Rules

Draws inspiration from Laravel's validation rules.

  • alpha_dash: Asserts a string matches the Regular Expression /^[A-Za-z0-9_-]$/.
  • alpha_num: Asserts a string matches the Regular Expression /^[A-Za-z0-9]$/
  • boolean: Asserts that input has a truthy or falsy value (true, false, 0 or 1)
  • confirmed: Like in Laravel, The field under validation must have a matching field of foo_confirmation.
  • email: Asserts a string complies to the RFC 5322 e-mail standard.
  • integer: Asserts a datum is an integer.
  • max: Limits a string to a maximum length, or restricts the magnitude of a Number.
  • min: Limits a string to a minimum length, or restricts the magnitude of a Number.
  • negative: Asserts a Number is less than zero.
  • numeric: Asserts a datum is a Number.
  • positive: Asserts a Number is greater than zero.
  • regex: Asserts a string matches a given Regular Expression.
  • required: Asserts a key is present in the request.

##Contributing to feathers-validator

This package is really easy to extend. All validation is performed by functions. feathers-validator determines which one to use by parsing input, then running a generator function.

If you want to add a new one, include a generator function in index.js. The generator must return a function(key, value). This function in turn must return either: {valid: true} OR {valid: false, error: 'Your error message here'} Otherwise, the validator will crash.

Then, submit a pull request. It will be reviewed, and if it's good, I'll add it to feathers-validator.