Wiki

Clone wiki

JSV / Home

JSV

Overview

JSV was developed by William Rivera for the purpose of validating JavaScript structures notably JSON or any hybrid grouping you are using. The concept for this came about when deploying multiple apps using different JSON formats. It was apparent that I was wasting time trying to determine if my data was being sent or parsed correctly. The main goal of this library is to simply validate that its correctly formatted and not missing properties or children objects/arrays etc...

Features

  • use multiple search criteria
  • easy to remember searching
  • oncomplete callback support
  • multiproperty searching and validation

Example

    
//Structure 	
var dta= 	
{ 	  
    header:{ 		
        lists:2,  		
        dataSet :2,  		
        callback:function(){ 
            console.log('hello'); 
        }  	  
    }, 	  
    data:[ 		 
        {val1:22,val2:23},
        {val1:33, val2:44} 	  
    ] 	
}          
//create callback function to pass in 	
var clb = function(){ 	    
    console.log(this.results); //status of parsing, usually not relevant since it occurs quickly
    console.log(this.errors); 	//show errors that occurred or will return false
    console.log(this.success); 	//returns true or false
    console.log(this.searchString);//returns the search criteria passed in 	
} 	 	
//test that header exists with lists and dataSet properties containing a number - usage explained in detail in usage section 	

var test="{header{lists,dataSet#"; 	
//call validation passing test, data and callback; 	
JSV.validate({validate:test, data:dta, callback:clb}); 

Usage

  • var test="{header{lists,dataSet#" - validation is done through a hierarchy meaning if I wanted to validate that I have an array of array of objects I would use my shortcuts and use the following string to represent this as "[[{"; the following are the shorcuts that can be used
  • Object as { aka curly brace
  • Array as [ aka bracket
  • Function as ( aka parenthesis
  • String as $ aka dollar sign
  • Number as # aka pound

As an example if I wanted to validate an array of functions I could use "[(" and this would check that each element is a function. If I wanted to check if there was an array of numbers then I could use "[#" etc.... Occasionally you may want to check for a particular property. You can use names the same way. For example, if i was interested in an array of objects with a property of num containing a number I could check that as "[{num#" its possible that there may be other properties but when names are used it will isolate to that property once found. You could check further down if you had nested arrays and function within a certain property name. for example given the following:

{   
    obj:{  	  
        func:function(){} 	
    },   
    num:3 
} 

If I were interested in checking for the presence of the function within the named obj property I would use the following syntax "{obj{func(" . This would ignore the property of num since obj is specifically named. If I wanted to check that num existed with a number I would use "{num#". Sometimes you will want to validate both in one call. you have the option to pass in an array for validation so using the 2 examples just mentioned I could make a call to check for both as JSV.validate({validate:["{obj{func(" ,"{num#"], data:dta, callback:clb}); and this will validate the entire structure

callback

when validation occurs and a call back is used, the call back will run. you can access the JSV instance either through the call back using the this keyword or directly by referencing your call as an instance so :

var j= JSV.validate({validate:["{obj{func(" ,"{num#"], data:dta}); 

you could then access the JSV properties through the instance. The following properties are

  • results : status of parsing, usually not relevant since it occurs quickly.
  • errors :show errors that occurred or will return false if none found. returns a detailed string of text
  • success : returns true on no errors found or false if errors are present
  • searchString : returns the search string criteria passed in.

Parameters

NameTypeDescription
ValidateStringthe syntax string of validation rules
DataArray/ObjectThis corresponds to the actual data object you are using, you must pass this in otherwise your validation will not run
callbackFunction [optional]the function to run when validation is complete

Updated