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

univalid

v1.0.5

Published

Universal validator for server and client side

Downloads

13

Readme

univalid

Universal validation module. It may use different strategies for client validation, server validation and more.

In current moment exists two strategies:

Install

npm i univalid

Base usage

const Univalid = require('univalid');
const univalid = Univalid();

univalid.check([
    {
        name: 'login',
        val: 'User01',
        type: 'required'
    },
    {
        name: 'email',
        val: '[email protected]',
        type: 'email'
    },
    {
        name: 'password',
        val: undefined,
        type: 'password'
    }
]);

console.log(univalid.getCommonState, univalid.getState);
univalid.clearState();

API

check(pack)

Validating the pack

pack - Type object

Structure of pack must be strict.

  • packItem.name - Type string - (required) - filed name
  • packItem.type - Type string - (required) - by default has: 'required', 'email', 'password', 'equal'
  • packItem.val - Type string - (required) value of field
  • packItem.filter - Type boolean - filter type (see more univalid-strategy)
  • packItem.msg - Type boolean - message config. See in example below

name, val, type - required fields

//name, val, type - required fields

univalid.check(
    [
        {
            name: 'username',
            val: 'Uriy',
            type: 'required',
            filter: 'oL',
            msg: {
                empty: 'You shall not pass',
                invalid: 'Validation error',
                filter: 'Filter error',
                success: 'All right'
            }
        },
        {
            name: 'email',
            val: '[email protected]',
            type: 'email',
            filter: val => {
                // Your custom filter
                
                console.log('Filter', val);
                
                // if FilterHandler is Ok then "return true"
                    return true;
                // else return false
            },
            msg: {
                empty: 'You shall not pass',
                invalid: 'Bad email',
                filter: 'Only lat/numbers/specials symbols',
                success: 'All right'
            }
        }
    ]
);

setStrategy(strategy)

Set new Strategy of validation

strategy - Type object - instance of strategy

const UnivalidStrategyForm = require('univalid-strategy-form');

univalid.setStrategy(
    UnivalidStrategyForm({
        core: univalid, /* required prop */
        $form: '.js-reg-form' /* required prop */
    })
);

setValidHandler(pack)

Set new Validation Handler

pack - Type object

New validationHandler must return true\false how result validation of field

univalid.setValidHandler({
    'newValidator': val => {
        console.log(val, 'Valid');
        return true;
    }
});

setMsgConfig(config)

Set new Default Message config

If in item of validation pack not define 'msg' field, will be message from msgConfig be default

config - Type object

univalid.setMsgConfig({
    empty: 'NEW EMPTY ERROR', 
    invalid: 'NEW INVALID', 
    filter: "NEW FILTER", 
    success: 'NEW SUCCESS'
});

toggleDefaultMsgConfig()

Toggle to default and common configuration of messages.

This configuration is common for all univalid modules.

univalid.toggleDefaultMsgConfig(); // default msgConfig
univalid.toggleDefaultMsgConfig(); // msgConfig of instance

setDefaultMsgConfig(config)

Set new Common Message config

config - Type object

univalid.setMsgConfig({
    empty: 'NEW COMMON EMPTY ERROR', 
    invalid: 'NEW COMMON INVALID', 
    filter: "NEW COMMON FILTER", 
    success: 'NEW COMMON SUCCESS'
});

//or

univalid.setMsgConfig({
    empty: 'NEW COMMON EMPTY ERROR'
});
 

set(option, val)

Set new prop to your current strategy of validation

option - Type string

univalid.set('core', univalid);

get(prop, args)

Get prop your current strategy or call the method your strategy.

prop - Type string

args - if it a method of strategy


//univalid-strategy-form example

univalid.get('addEvent', {
    newEvent(){document.addEventListener('click', ()=>{
	    console.log('Click in document!');
    })}
});

univalid.get('clsConfig');

clearState()

Clear your current validation state

getState()

Get last validation state

getStrategy()

Get current Strategy of validation

getValidHandler()

Get current validation handler

getCommonState()

Get Common state of validation (true\false)

EVENTS

You can subscribe on univalid events (univalid extends EventEmitter)


univalid.on('start:valid', (args) => {
    console.log('Check!');
});

Table of events

| Event | Description | |:------:|:-----------:| |start:valid|Start validation pack| |end:valid|End validation pack| |start:valid:field|Start validation field| |end:valid:field|End validation field| |change:strategy|Change strategy event| |set:new-ValidationHandler|Set new ValidationHandler event| |change:msg-config|Change message config event| |clear:state|Clear state of last validation event| |error|Error event|

License

ISC ©