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

uni-validator

v1.0.0

Published

strong js validator

Downloads

23

Readme

uni-validator

To install the stable version:

npm install --save uni-validator

Examples of validator methods

import Validator from 'uni-validator';

const validator = new Validator(); // instance with default rules

const validator = new Validator(myRules); // or instance with custom rules 
// gonna override default ones with same names (see link below)

About validation rules

Validate single value

.validate(stringToValidate, arrayOfRules)

validator.validate('someValue', [{
  name: 'minLength',
  len: 10
},{
  name: 'equals',
  to: 'someValueee'
}])
will return object below if validation fails for both rules:
for each rules which didnt passed validation equal pair of key-value will be created in results object
if one of rules fail validation - valid: false otherwise valid: true
  results: {
     minLength: 'minimum of 10 characters',
     equals: 'should be equal to someValueee'
  },
  valid: false
otherwise if validation ended with success will be returned:
  results: {},
  valid: true

Validate multiple values

.validateGroup(groups)

validator.validateGroup([
  {
    data: 'someValue1', // value to validate
    rules: [
      {
        name: 'minLength',
        len: 8
      }
    ],
    field: 'value1' // optional (will fallback to value of data key (currently 'someValue1')), provide it if you want other key-name of current value in returned validation results
  }, //second value
  {
     data: 'someValue2',
     rules: [
       {
         name: 'minLength',
         len: 12
       },{
         name: 'equals',
         to: 'someValueee',
         toAlias: 'password'
       }
     ]
  }])
returns
{ 
  errorObjects: {
    value1: {
      results: {},
      valid: true
    },
    someValue2: {
      results: {
        equals: 'should be equal to password',
        minLength: 'minimum of 12 characters'
      },
      valid: false
    }
  },
  valid: false
}

#####IMPORTANT: Both examples above will run paralell validation. If you like to validate with some rule only if previous validation ended with success (for example you want to check if your value equals to another one only in case if this value exist) you should add next reference to rule:

 // will run 'equals' check only if value 'someValue' isnt empty
 // same for '.validateGroup' method
 // you can chain 'next:' as many times as you want
 validator.validate('someValue',
            [{
                name: 'empty',
                next: {
                    name: 'equals',
                    to: 'someValueee'
                }
            }, {
                name: 'minLength',
                len: 8
            }])

Add custom validation rule

.addRule(name, rule, errorMsg)

validator.addRule(
  'myNewRule', // value to validate
  (val, options) => val !== options.notEqual, // function(val, options) - if returns true after validation = rule marked as valid
  (val, options) => `${ val } shouldn't be equal to ${ options.notEqual }` //can be function(val, options) or string - text to show if validation fails
);
//or
validator.addRule(
  'myNewRule2',
   val => val !== '2',
  "shouldn't be equal to 2"
);
returns this

About validation rules

In order to use your new rules or old ones you should pass them in array as second argument of .validate or .validateGroup (for each instance to validate) method. Invocation of method will go though all rules and if a single rule will fail validation it will return process mareked as valid: false with messages from all rules failed during validation. You can pass em as objects with structure

[
  {
    name: 'ruleName',  // name of rule (first argument of .addRule method)
    ...customValues    // set of key:values which used to determine if validation process went success. Will be inside options object.
  },
  ...otherObjectRules
]

or you can pass them as simple strings if rule doesnt require additional params to compare

['empty', 'isNumber', ...otherStrings]
You can check methods here:

https://github.com/N1gma/uni-validator/blob/master/src/index.js

And current validation rules which you can use:

https://github.com/N1gma/uni-validator/blob/master/src/validatorRules/commonRules.js