New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

univalid

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

univalid

Universal validator for server and client side

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

univalid

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

In current moment exists two strategies:

  • univalid-strategy-default (use by default)
  • univalid-strategy-form

Install

npm i univalid

Base usage

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

univalid.check([
    {
        name: 'login',
        val: 'User01',
        type: 'required'
    },
    {
        name: 'email',
        val: 'test@test.ts',
        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: 'Uriy@mzf.com',
            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

EventDescription
start:validStart validation pack
end:validEnd validation pack
start:valid:fieldStart validation field
end:valid:fieldEnd validation field
change:strategyChange strategy event
set:new-ValidationHandlerSet new ValidationHandler event
change:msg-configChange message config event
clear:stateClear state of last validation event
errorError event

License

ISC ©

Keywords

FAQs

Package last updated on 04 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc