{"id":20340170,"url":"https://github.com/webengage/javascript-object-validator","last_synced_at":"2025-04-11T23:16:59.885Z","repository":{"id":57286116,"uuid":"47567157","full_name":"WebEngage/Javascript-Object-Validator","owner":"WebEngage","description":"A small JS Object Validator for validating complex nested objects","archived":false,"fork":false,"pushed_at":"2016-09-24T17:15:41.000Z","size":16,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T23:16:54.370Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebEngage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-07T17:12:20.000Z","updated_at":"2021-10-28T08:49:05.000Z","dependencies_parsed_at":"2022-08-29T12:00:49.299Z","dependency_job_id":null,"html_url":"https://github.com/WebEngage/Javascript-Object-Validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebEngage%2FJavascript-Object-Validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebEngage%2FJavascript-Object-Validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebEngage%2FJavascript-Object-Validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebEngage%2FJavascript-Object-Validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebEngage","download_url":"https://codeload.github.com/WebEngage/Javascript-Object-Validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492885,"owners_count":21113163,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T21:20:05.639Z","updated_at":"2025-04-11T23:16:59.857Z","avatar_url":"https://github.com/WebEngage.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Javascript Object Validator(JsOV)\n## Installation\n#### Using npm\n`$ npm install jsov --save`\nand then, require it\n```javascript\nvar JsOV=require('jsov');\n```\n#### Using bower\n`$ bower install jsov --save`\nand then, require it\n```javascript\nvar JsOV=require('jsov');\n```\nor\n#### Directly use JsOV.js\n```javascript\n\u003cscript type='text/javascript' src='JsOV.js'\u003e\u003cscript\u003e\n```\nNow you can use `JsOV` object to access various functions.\n## Usage\n\nIn its simlplest essence, Javascript Object Validator(JsOV) can be used as\n```javascript\nvar result=JsOV.schemaValidator(Schema,Obj);\n```\n\nThe above function `schemaValidator` takes two arguments, the `Schema` of the object you want to validate and the `Object` itself.\nFor example lets consider an object\n```javascript\nvar Obj={\n    'title':'abc',\n    'variations':[{\n        'title':'p1',\n        'message':'m1',\n        'cta':{\n            'type':'DEEP_LINK',\n            'actionLink':'abc'\n        }\n    },\n    {\n        'title':'p2',\n        'message':'m2',\n        'cta':{\n            'type':'EXTERNAL',\n            'actionLink':'https://jsv.com'\n        }\n    }]\n};\n```\nAnd the schema for the above object is\n```javascript\nvar Schema = {\n    'type': 'Object',\n    'required': true,\n    'message': 'Perfectoo',\n    'data': {\n        'title': {\n            'type': 'String',\n            'validation': {\n                'RegEx': 'abc'\n            }\n        },\n        'variations': {\n            'type': 'Array',\n            'required': true,\n            'validation': function(data) {\n                if (data.length \u003e 1)\n                    return true;\n                else\n                    return false;\n            },\n            'dataType': 'Object',\n            'data': [{\n                'title': {\n                    'type': 'String',\n                    'required': true\n                },\n                'message': {\n                    'type': 'String',\n                    'required': true\n                },\n                'cta': {\n                    'type': 'Object',\n                    'validation': function(cta) {\n                        if (cta.actionLink === 'abc')\n                            return true;\n                        else\n                            return false;\n                    },\n                    'required': true,\n                    'data': {\n                        'type': {\n                            'type': 'String',\n                            'required': true,\n                            'validation': function(type) {\n                                if (type === 'DEEP_LINK' || type === 'EXTERNAL_URL')\n                                    return true;\n                                else\n                                    return false;\n                            }\n                        },\n                        'actionLink': {\n                            'type': 'String',\n                            'required': true\n                        }\n                    }\n                }\n            }]\n        }\n    }\n};\n```\nNow `schemaValidator` function will take these two arguments and will return the following output\n```javascript\nvar result = {\n    'title': {\n        'value': 'abc',\n        'valid': true,\n        'message': ''\n    },\n    'variations': [{\n        'title': {\n            'value': 'p1',\n            'valid': true,\n            'message': ''\n        },\n        'message': '',\n        'cta': {\n            'type': {\n                'value': 'DEEP_LINK',\n                'valid': true,\n                'message': ''\n            },\n            'actionLink': {\n                'message': '',\n                'valid': true,\n                'value': 'abc'\n            },\n            'valid': true,\n            'message': ''\n        },\n        'valid': true\n    }, {\n        'title': {\n            'value': 'p2',\n            'valid': true,\n            'message': ''\n        },\n        'message': '',\n        'cta': {\n            'type': {\n                'value': 'EXTERNAL',\n                'valid': false,\n                'message': ''\n            },\n            'actionLink': {\n                'message': '',\n                'valid': false\n            },\n            'valid': false,\n            'message': '',\n            'value': 'https://jsv.com'\n        },\n        'valid': false\n    }],\n    'valid': false,\n    'message': 'Perfectoo'\n};\n```\nAs 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\n```javascript\nresult.variations[0].valid //true\nresult.variations[1].valid //false\nresult.variations.valid//false\nresult.valid//false\n```\n\nTo get the value of a property\n```javascript\nresult.variations[0].title.value //p1\n```\n\nIf there is an message you want to add to a property (like in case of error messages)\n```javascript\nresult.message //Perfectoo\n```\nAll this sounds good, but what about the pain you are going to incur while creating the Schema. \nBelow is your Answer\n\n## Schema Generator to your Rescue !!\nSo what does it take to generate your Schema.\n```javascript\nvar Schema=JsOV.generateSchema(Obj,true);\nJSON.stringify(Schema,null,'\\t');\n```\nYeah, 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\n```javascript\nvar Obj={\n    'name':'ABCD',\n    'address':{\n        'street':'Coder's street',\n        'city':'Gotham'\n     }\n};\n```\nAnd your output will be (After using JSON.Stringify)\n\n```javascript\n{\n\t'type': 'Object',\n\t'required': true,\n\t'validation': '',\n\t'data': {\n\t\t'name': {\n\t\t\t'type': 'String',\n\t\t\t'required': true,\n\t\t\t'validation': ''\n\t\t},\n\t\t'address': {\n\t\t\t'type': 'Object',\n\t\t\t'required': true,\n\t\t\t'validation': '',\n\t\t\t'data': {\n\t\t\t\t'street': {\n\t\t\t\t\t'type': 'String',\n\t\t\t\t\t'required': true,\n\t\t\t\t\t'validation': ''\n\t\t\t\t},\n\t\t\t\t'city': {\n\t\t\t\t\t'type': 'String',\n\t\t\t\t\t'required': true,\n\t\t\t\t\t'validation': ''\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n```\n\n### Validations\nThere are two ways you can add validations\n#### Type 1\nYou can write a custom function to evaluate your property. \nE.g consider an object \n```javascript\nvar obj = {\n    'cta': {\n        'type': 'DEEP_LINK',\n        'actionLink': 'abc'\n    }\n}\n```\nand its schema\n```javascript\nvar Schema = {\n    'cta': {\n        'type': 'Object',\n        'validation': function(cta) { //validation 1\n            if (cta.actionlink === 'abc')\n                return true;\n            else\n                return false;\n        },\n        'required': true,\n        'data': {\n            'type': {\n                'type': 'String',\n                'required': true,\n                'validation': function(type, global) { //validation 2\n                    if (type === 'DEEP_LINK' || type === 'EXTERNAL_URL')\n                        return true;\n                    else\n                        return false;\n                }\n            },\n            'actionLink': {\n                'type': 'String',\n                'required': true\n            }\n        }\n    }\n}\n```\n \nHere **validation 1** will take  \n```javascript\n{\n\t'type': 'DEEP_LINK',\n\t'actionLink': 'abc'\n}\n```\nas its argument and **validation 2** will take\n```javascript\n'DEEP_LINK'\n```\nand\n```javascript\n{\n    'cta': {\n        'type': 'DEEP_LINK',\n        'actionLink': 'abc'\n    }\n}\n```\nNOTE: 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.\n\n#### Type 2\nUsing inbuilt validation properties\n```javascript\n//this will match whether the value of the property aginst the specified regex string\n'validation':{\n'RegEx':'abc'\n}\n```\n\nE.g\n```javascript\nvar Schema = {\n    'type': 'Object',\n    'required': true,\n    'message': 'Perfectoo',\n    'data': {\n        'title': {\n            'type': 'String',\n            'validation': { //Do it this way\n                'RegEx': 'abc'\n            }\n        }\n    }\n}\n```\nJust Like RegEx there are various kind of validations that are provided by JSV, below is the list\n\n1. RegEx\n\nNOTE: 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.\n\n```javascript\n/*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*/\n\njsOV.addCustomValidations('MaxLen',function(key,val){\n   return val.length \u003c = key; \n});\n```\nand then to use this\n```javascript\nvar Schema = {\n    'type': 'Object',\n    'required': true,\n    'message': 'Perfectoo',\n    'data': {\n        'title': {\n            'type': 'String',\n            'validation': { //Do it this way\n                'MaxLen': 2\n            }\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebengage%2Fjavascript-object-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebengage%2Fjavascript-object-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebengage%2Fjavascript-object-validator/lists"}