1.1.1 • Published 6 years ago

compare-validate-json v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

compare-validate-json.js

compare-validate-json.js is usefull in deep comparing first json with second. Specially for http response validation which are in JSON.

Installation

If you are using node, you can install compare-validate-json with npm.

npm install compare-validate-json

Then you can include it in your code:

var comapareValidateJson = require("compare-validate-json");

Usage

comapareValidateJson.compare(json1, json2)

comapareValidateJson.deepStrictCompare(json1, json2)

Description

The first json to be passed to the compare function can be a subset of the second json. The compare function skips the missing key not present in the first json and present in the second json. If a key present in the first json is not present in the second json then the valdation will fail. This is useful when we want to validate for a json response,but we only want to provide the keys which we want to validate and skip the other keys. This also works for circular JSONs also. For deep and strict json comparison we also have another api "deepStrictCompare", which will return true only if both the json passed to it are identical in all aspects.

Examples:

var comapareValidateJson = require("compare-validate-json");

var json1 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1"},"arrObj":[{"artObj1":"arrObjVal1"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

var json2 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1","obj2":"val2"},"arrObj":[{"artObj1":"arrObjVal1"},{"artObj2":"arrObjVal2"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

var json3 = {"a":"1","b":"2"};
var json4 = {"a":"1","b":"2","c":"3"};

console.log("Json validation result "+comapareValidateJson.compare(json1,json2));//true
console.log("Json validation result "+comapareValidateJson.compare(json3,json4));//true
console.log("Json validation result "+comapareValidateJson.compare(json4,json3));//false

console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json1,json2));//false
console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json3,json4));//false
console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json4,json3));//false

var json5 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1"},"arrObj":[{"artObj1":"arrObjVal1"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json1,json5));//true

Methods

compare(json1,json2);

compare all the keys and values in json1 against json2. If key is not present in json2 returns false, skips extra keys in json 2.

deepStrictCompare(json1,json2);

compare all the keys in both json1 and json2. if any key is missing or the value is different in any of the json then returns false.

License

This project is public domain. For more details, read about the Unlicense.