npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsov

v1.2.1

Published

A small JS Object Validator for validating complex nested objects.

Downloads

11

Readme

Javascript Object Validator(JsOV)

Installation

Using npm

$ npm install jsov --save and then, require it

var JsOV=require('jsov');

Using bower

$ bower install jsov --save and then, require it

var JsOV=require('jsov');

or

Directly use JsOV.js

<script type="text/javascript" src="JsOV.js"><script>

Now you can use JsOV object to access various functions.

Usage

In its simlplest essence, Javascript Object Validator(JsOV) can be used as

var result=JsOV.schemaValidator(Schema,Obj);

The above function schemaValidator takes two arguments, the Schema of the object you want to validate and the Object itself. For example lets consider an object

var Obj={
    'title':'abc',
    'variations':[{
        'title':'p1',
        'message':'m1',
        'cta':{
            'type':'DEEP_LINK',
            'actionLink':'abc'
        }
    },
    {
        'title':'p2',
        'message':'m2',
        'cta':{
            'type':'EXTERNAL',
            'actionLink':'https://jsv.com'
        }
    }]
};

And the schema for the above object is

var Schema = {
    'type': 'Object',
    'reuired': true,
    'message': 'Perfectoo',
    'data': {
        'title': {
            'type': 'String',
            'validation': {
                'RegEx': 'abc'
            }
        },
        'variations': {
            'type': 'Array',
            'reuired': true,
            'validation': function(data) {
                if (data.length > 1)
                    return true;
                else
                    return false;
            },
            'dataType': 'Object',
            'data': [{
                'title': {
                    'type': 'String',
                    'required': true
                },
                'message': {
                    'type': 'String',
                    'required': true
                },
                'cta': {
                    'type': 'Object',
                    'validation': function(cta) {
                        if (cta.actionLink === 'abc')
                            return true;
                        else
                            return false;
                    },
                    'required': true,
                    'data': {
                        'type': {
                            'type': 'String',
                            'required': true,
                            'validation': function(type) {
                                if (type === 'DEEP_LINK' || type === 'EXTERNAL_URL')
                                    return true;
                                else
                                    return false;
                            }
                        },
                        'actionLink': {
                            'type': 'String',
                            'required': true
                        }
                    }
                }
            }]
        }
    }
};

Now schemaValidator function will take these two arguments and will return the following output

var result = {
    "title": {
        "value": "abc",
        "valid": true,
        "message": ""
    },
    "variations": [{
        "title": {
            "value": "p1",
            "valid": true,
            "message": ""
        },
        "message": "",
        "cta": {
            "type": {
                "value": "DEEP_LINK",
                "valid": true,
                "message": ""
            },
            "actionLink": {
                "message": "",
                "valid": true,
                "value": 'abc'
            },
            "valid": true,
            "message": ""
        },
        "valid": true
    }, {
        "title": {
            "value": "p2",
            "valid": true,
            "message": ""
        },
        "message": "",
        "cta": {
            "type": {
                "value": "EXTERNAL",
                "valid": false,
                "message": ""
            },
            "actionLink": {
                "message": "",
                "valid": false
            },
            "valid": false,
            "message": "",
            "value": 'https://jsv.com'
        },
        "valid": false
    }],
    "valid": false,
    "message": "Perfectoo"
};

As you can see the resultant object gives you capability to drill down to any level to know whether that property is true or false. Plus not only that, if the outcome of a property is false, this information is bubbled up to all its parents upto the object level.e.g

result.variations[0].valid //true
result.variations[1].valid //false
result.variations.valid//false
result.valid//false

To get the value of a property

result.variations[0].title.value //p1

If there is an message you want to add to a property (like in case of error messages)

result.message //Perfectoo

All this sounds good, but what about the pain you are going to incur while creating the Schema. Below is your Answer

Schema Generator to your Rescue !!

So what does it take to generate your Schema.

var Schema=JsOV.generateSchema(Obj,true);
JSON.stringify(Schema,null,"\t");

Yeah, that's it and you are ready with your boilerplate Schema. The first argument is your Object for which you need to generate schema and the second object is whether you want all your fields to be required or not. E.g

var Obj={
    "name":"ABCD",
    "address":{
        "street":"Coder's street",
        "city":"Gotham"
    }
};

And your output will be (After using JSON.Stringify)

{
	"type": "Object",
	"required": true,
	"validation": "",
	"data": {
		"name": {
			"type": "String",
			"required": true,
			"validation": ""
		},
		"address": {
			"type": "Object",
			"required": true,
			"validation": "",
			"data": {
				"street": {
					"type": "String",
					"required": true,
					"validation": ""
				},
				"city": {
					"type": "String",
					"required": true,
					"validation": ""
				}
			}
		}
	}
}

Validations

There are two ways you can add validations

Type 1

You can write a custom function to evaluate your property. E.g consider an object

var obj = {
    'cta': {
        'type': 'DEEP_LINK',
        'actionLink': 'abc'
    }
}

and its schema

var Schema = {
    'cta': {
        'type': 'Object',
        'validation': function(cta) { //validation 1
            if (cta.actionlink === 'abc')
                return true;
            else
                return false;
        },
        'required': true,
        'data': {
            'type': {
                'type': 'String',
                'required': true,
                'validation': function(type, global) { //validation 2
                    if (type === 'DEEP_LINK' || type === 'EXTERNAL_URL')
                        return true;
                    else
                        return false;
                }
            },
            'actionLink': {
                'type': 'String',
                'required': true
            }
        }
    }
}

Here validation 1 will take

{
	"type": "DEEP_LINK",
	"actionLink": "abc"
}

as its argument and validation 2 will take

'DEEP_LINK'

and

{
    'cta': {
        'type': 'DEEP_LINK',
        'actionLink': 'abc'
    }
}

NOTE: here as you can see validation 2 function has a second argument. Second argument is optional, which provides you your whole object to perform complex validations.

Type 2

Using inbuilt validation properties

//this will match whether the value of the property aginst the specified regex string
'validation':{
'RegEx':'abc'
}

E.g

var Schema = {
    "type": "Object",
    "reuired": true,
    "message": "Perfectoo",
    "data": {
        "title": {
            "type": "String",
            "validation": { //Do it this way
                "RegEx": "abc"
            }
        }
    }
}

Just Like RegEx there are various kind of validations that are provided by JSV, below is the list

  1. RegEx

NOTE: Incase there is any kind of validation that you want to add to the your custom validation to use them reapeatedly in your projet, you can use.

/*This function will take two arguments where key is value of the property inside validation object (like here it is 2) and the second argument is the actual vale in original object against the key*/

jsOV.addCustomValidations('MaxLen',function(key,val){
   return val.length < = key; 
});

and then to use this

var Schema = {
    "type": "Object",
    "reuired": true,
    "message": "Perfectoo",
    "data": {
        "title": {
            "type": "String",
            "validation": { //Do it this way
                "MaxLen": 2
            }
        }
    }
}