visorjs v2.0.1
VisorJS
A Validation Framework.
Visorjs doesn't attempt to tell you what rules you have to make and what rules you have to follow. It comes with only with two rules required and notRequired
This allows you to use a different library like validator. This is a highly popular and tested library.
Usage
import Validator from "visorjs";
// Extends this class
class UserValidation extends Validator{
constructor(){
super();
this.setConfig({
name:"username"
})
}
username(key,value){
if(value.length < 6){
throw new Error(`username needs 6 characters`)
}
}
}
let v = new UserValidation()
v.validate({
name:"short"
})
//throws error
Best Practice
You only want to use a Validation instance once. So you will be creating a new one each time. Use extend to create Base Classes that has common validations that you want to run.
You can pass parameters to the function easily by adding params.
class UserValidation extends Validator{
constructor(){
super();
this.setConfig({
name:"username:6,20"
})
}
username(key,value,min,max){
//All parameters are going to be passed as a string
//So they need to be parsed
if(value.length < parseInt(min) || value.length > parseInt(max)){
throw new Error(`username needs 6 characters`)
}
}
}
required notRequired
These two functions are very important. They allow you to do something very nice with validation.
Imaging this possibility.
class UserValidation extends Validator{
constructor(){
super();
this.setConfig({
name:"username:6,20",
"address":"notRequired",
"address.street":"required",
"address.number":"required"
})
}
username(key,value,min,max){
//All parameters are going to be passed as a string
//So they need to be parsed
if(value.length < parseInt(min) || value.length > parseInt(max)){
throw new Error(`username needs 6 characters`)
}
}
}
This allows you to have a parameter that is missing. Like address, however if the address is present it forces certain elements to be required.
//Valid against the rule above
{
name:"shavy"
}
//not valid missing number
{
name:"shavy",
address:{
street:"home"
}
}
//valid
{
name:"shavy",
address:{
number:10,
street:"home"
}
}
Config
The config allows a certain amount of flexibility. For example you can use these different combination in your rules.
//single string value that must be a method that accepts key and value
"rulename"
//multiple rules
"rulename|other_rulename"
//Array rule
["rulename","other_rulename"]
//function
(key:string,value:any)=>{
//throw error here if you need be
}
//mixture of string and arrays
["rulename",(key:string,value:any)=>{
}]
//passing parameters
"rulename:arg1,arg2"
//this will call the method with args as such
class Example extends Validator{
rulename(key:string,value:any,arg1:string,arg2:string){
//all passed args will always be a string
//throw if error
}
}
All other functions that are required will need to be created on your own.
Future
I plan on adding a different class that will have some basic validation.
Concepts
Rules are validated in the order that they are written. It fails on the first one.
Dependencies
This is simple. None.
Typescript
Hell yea!!!