Object Validator Pro (OVP)
What is OVP?
Object-Validator-Pro provides a very simple API for developers to make both Sync and Async validations on any javascript object.
To Use OVP efficiently, you need to understand how it validates each key in any given object against its rules. Unlike other validation libraries, OVP is best for your custom validation rules.
You can create your own validation library out of OVP and use them in as many projects as you want.
You set your rules just the way you understand them.
Get Started.
See Full Documentation for more details.
After Installation
const ObjectValidator = ;const ovp = ; // log errors when they occur.ovp
Simple Form Data Validation
// Object to validatelet data = username: 'NodeJs' password: '123456'; // "*" sets validation for all data keys, more like a wildcard.let rules = "*": typeOf: 'string' must: true password: minLength: 10; let check = ovp; if !check // do something // returns: false// log: [ 'password', 'Password is too short. (Min. 10 characters)' ] // if rules has an Async validator use ovp;
check
and isValid
returned false,
validate
and validateAsync
function logs an array onEachError
,
Array[0]
: Failed Object key, Array[1]
: Error message.
To check if *
(wildcard) rules affected data.username
, it should return an error when username is not a string
datausername = 'an array instead of a string'; check = ovp; // returns: false// log: [ 'username', 'Username is not typeOf string' ]
Simple validateAsync Example
const axios = ; let asyncTestData = url: 'https://google.com'; let asyncTestRules = url: urlIsOnline: true; // lets add urlIsOnline Async validator.ovp; // if any of your rules contain an async validator,// You use the validateAsync function instead of validate, returns Promise<boolean>// You can use await or Promise.then((result){}) let first = await ovp;console;// logs: true asyncTestDataurl = 'https://some-website-that-does-not-exists-2019.com'; let second = await ovp;console;// log: ["url", "Url is not online!"]// log: false
Super Rules Example
// assuming we have an object:wildcardData = name: 'wildcard' address: 'Drive 6, Astro world!' mobile: '+1336d373'; // With rules set with both wildcards:wildcardTestRules = '*': typeOf: 'string' '**': must: true address: minLength: 10; // the above will be transformed to this using wildcardswildcardTestRules = address: typeOf: 'string' must: true minLength: 10 name: typeOf: 'string' mobile: typeOf: 'string' ; // name and mobile was automatically added because * wildcard exists.// If * is removed and ** still exists, will result to: wildcardTestRules = address: must: true minLength: 10
value of *
is added to all object keys, while value of **
is added to all defined rules.
Notice that wildcardTestRules.address
rules comes first when transformed before the other keys of the object.
This is because defined rules runs before *
wildcard added rules.
Contributing
Pull requests are VERY welcomed. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.