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 = ;const univalid = ; univalid; console;univalid;
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;
setStrategy(strategy)
Set new Strategy of validation
strategy - Type object
- instance of strategy
const UnivalidStrategyForm = ; univalid;
setValidHandler(pack)
Set new Validation Handler
pack - Type object
New validationHandler must return true\false how result validation of field
univalid;
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;
toggleDefaultMsgConfig()
Toggle to default and common configuration of messages.
This configuration is common for all univalid modules.
univalid; // default msgConfigunivalid; // msgConfig of instance
setDefaultMsgConfig(config)
Set new Common Message config
config - Type object
univalid; //or univalid;
set(option, val)
Set new prop to your current strategy of validation
option - Type string
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; univalid;
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;
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 ©