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

valour

v0.0.27

Published

Simple javascript validation for any application

Downloads

40

Readme

Valour.js

Simple javascript validation for any app.

Greenkeeper badge Travis CI Build

If you're curious or if it's helpful, you can watch a presentation about valour.

Usage

Self-managed

var valour = require('valour');
var result;

valour.register('formName', {
  'email': valour.rule.isRequired()
                     .isEmail()
                     .isValidatedBy(function (value) {
                       var disallowedNames = ['[email protected]', '[email protected]', '[email protected]'];
                       return disallowedNames.indexOf(value) === -1;
                     }, 'This email is not allowed')
                     .isValidatedBy(function (value, allValues) {
                       return allValues.spouseEmail.indexOf(value) === -1;
                     }, 'The {name} field must be different from the spouse email.')
 }, function (res) {
  result = res;
 });

valour.update('formName', {
  'confirmEmail': valour.rule.equalsOther('email')
})

valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': {valid: true} }
valour.forceValidation('formName', {});
// result === {'email': {valid: false, messages: ['email is required.']}}
valour.runValidation('formName', { email: 'notanemail' });
// result === { 'email': {valid: false, messages: ['email must be a valid email address']} }
valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': {valid: false, messages: ['This email is not allowed']} }
valour.runValidation('formName', { email: '[email protected]', spouseEmail: '[email protected]' });
// result === { 'email': {valid: false, messages: ['The email field must be different from the spouse email.']} }
valour.runValidation('formName', { email: '[email protected]', confirmEmail: '[email protected]' });
// result === { 'confirmEmail': {valid: false, messages: ['The confirmEmail field must be equal to email.']} }
valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': {valid: true} }

NOTE:

An important distinction here is the difference between runValidation and forceValidation. runValidation is something you would use as things update (like in a change event for an input), and forceValidation is what you would use when you wanted to check all fields (like in a submit event). runValidation does not check undefined values, but forceValidation will. This is because, most of the time, you don't want your UI to falsely report to the user when they haven't yet put any data into a required field.

Async validation

var valour = require('valour');
var resolve, reject, result;

function resolveResult() {
  resolve();
}

function rejectResult() {
  resolve();
}

valour.register('formName', {
  'email': valour.rule.isEventuallyValidatedBy(
    function (value, allValues, res, rej) {
      resolve = res;
      reject = rej;
    });
  }, function (res) {
    result = res;
  });

valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': { waiting: true }}

resolveResult();
// result === { 'email': { valid: true }}

valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': { waiting: true }}

rejectResult();
valour.runValidation('formName', { email: '[email protected]' });
// result === { 'email': { valid: false }}

Setting the validation state

There may be times when you want to tell valour about the validity of your form. This may be on initial page load, or after some server-side validation has occurred. Whatever the case may be, 'setValidationState' is what you'll need to call. This little utility function takes in a form name and an object, then updates the form with the information the object holds. Afterwards, it will run any callbacks you have given it to alert them of the new state.

var valour = require('valour');
var result;

valour.register('formName', {
  'email': valour.rule.isEmail()
 }, function (res) {
  result = res;
 });

valour.setValidationState('formName', { email: { valid: false } });
// result === { 'email': { valid: false } }

valour.setValidationState('formName', { email: { valid: false, messages: ['New error.'] } });
// result === { 'email': { valid: false, messages: ['New error.'] } }

valour.setValidationState('formName', { email: { valid: true, messages: ['All clear'] } });
// result === { 'email': { valid: true, messages: ['All clear'] } }

Another way to do this is to initialize the state when registering. The callback provided will be called immediately, in this case.

var valour = require('valour');
var result;

valour.register('formName', {
  'email': valour.rule.isEmail().initializeState({ valid: false })
 }, function (res) {
  result = res;
 });
// result === { 'email': { valid: false } }

valour.register('anotherForm', {
  'email': valour.rule.isEmail().initializeState({ valid: true, messages: ['Some message'] })
 }, function (res) {
  result = res;
 });
// result === { 'email': { valid: true, messages: ['Some message'] } }