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

labs-validator

v2.1.4

Published

standalone javascript form validator. Inspired by Laravel Validator in a form of javascript.

Downloads

4

Readme

labs-validator

lightweight and standalone javascript validator. Inspired by Laravel Validator in a form of javascript.

Install

$ npm i labs-validator --save

If you need HTML and <form> validation support, checkout the v1 here

API Usage

labsValidator.validate(inputObject, rules, [customMessages]);

Breaking Changes 2.0.x to 2.1.x

  • use colon (:) as parameter indicator (same as Laravel validator) unlike the v2.0.x which uses equal sign (=)

Sample Basic Usage

var labsValidator = require('labs-validator'); //for js modules

var input = {email: 'test'};
var rules = {
  name: 'required',
  email: 'required|email',
  password: 'required|min:6',
  confirm_password: 'required|same:password',
  country: 'required|in:Philippines,USA,China,UK', //only the specified values are accepted
  city: 'requiredIf:country,Philippines' //city is required if the country is Philippines
};

var result = labsValidator.validate(input, rules);
// or, for non js-module
var result = window.labsValidator.validate(input, rules);

if (result.pass) {
  // do something
} else {
  console.log(result.errorMessages)
}

Custom Messages

labsValidator.validate(input, rules, {
      country: {
        required: 'Custom message for required country.',
        in: 'another custom message.'
      }
    });

Validation rules are separated by '|'

Validation Rules

  • min
  • minimum value if number or minimum length for string
  • max -maximum value if number or maximum length for string.
  • email
  • value must be an email
  • required
  • the field is required
  • requiredIf -the field is required if the other field matches a value. e.g validator-requiredIf="team:we are one", first argument is the field, the second is the value of the field
  • number
  • the value of the field must be a valid number
  • between
  • the value of the field must between the arguments. e.g validator-between="5,10"
  • same
  • the value of the field must be the as the other field. e.g
  • in
  • the value of the field must be in the list. e.g validator-in="good,better,best"
  • not-in
  • the value of the field must not be in the list e.g validator-not-in="good,better,best"
  • boolean
  • the value of the field must be either of the following: true,false,1,0,'1','0'
  • startsWith
  • the value of the fiel must start the same as the argument. e.g validator-startsWith="my name"
  • endsWith
  • the value of the field must ends the same as the argument. validator-endsWith="cool"
  • regexp
  • the value of the field must match the regular expression in the argument
  • url
  • value must be a valid url
  • alpha
  • value must be a letter
  • alphaNum
  • value must be a letter or numbers
  • alphaNumDash
  • value must be a letter, number or a dash

Extending Validation

  • labsValidator.addValidator(name,fn);
  • callback function should return a boolean value
  • function arguments: value, parameter, name, element, helper
labsValidator.addValidator('required',function(value){
    return value ? true : false;
});

labsValidator.addValidator('date', function(v){
    return dayjs(v).isValid();
});
  • labsValidator.addValidatorMsg(name,fn);
  • callback function should return a message (string)
  • function arguments: value, parameter, name, element, helper
labsValidator.addValidatorMsg('required',function(value, par, name){
  return name + ' is required';  
});
labsValidator.addValidatorMsg('date',function(value, par, name){
  return name + ' is shoud be valid date.';  
});

Global Translate

add key-value to the translate attribute

labsValidator.translate.administrative_area_level_2  = 'Province';
labsValidator.translate.fname = 'First Name';

//outputs the error messages into
"Province field is required."
"First Name field is required."

Inertiajs

Inertiajs re-maps the laravel error message bag into a string instead of an array. In other match and have a uniform syntax through out the frontend and backend, declare the msgContainer as String

labsValidator.msgContainer = String; //declare this
var result = labsValidator.validate(input, rules);
console.log(result.errorMessages);

Backbonejs

the labsValidator.validate() is also a perfect validator for backbonejs Model

var User = Backbone.Model.extend({
  validate: function(attrs){
    var validation = labsValidator.validate(attrs,{
      email: 'required|email',
      password: 'required|min:8',
      name: 'required',
      country: 'required|in:Philippines,USA,China,UK',
      city: 'requiredIf:country:Philippines'
    });
    
    if( !validation.pass )
      return validation.errorMessages;
  }

});

var user1 = new User;

console.log(user1.isValid());
console.log(user1.validationError);