One problem I constantly have in JavaScript is checking what a object looks like. For example a remote call might need to have certain attributes and potentially nested attributes. So I created a code solution to solve this problem for me.

Arguments

  • comparison - the object that is being compared with
  • schema - the schema of what the object should look like
  • throwError [optional] - will throw a JavaScript error if the arguments are invalid, defaults to false

How to use

const isObjectMappedToSchema = require('./schema-compare')

if (isObjectMappedToSchema(
		{key: {sub_key: "Something"}},
		{key: {sub_key: true}})) {
	console.log("Payload matches Schema")
}

How to run tests test

Tests are written with Jest.

Install Jest (If not already installed)

npm install jest -g

How to run tests

jest

Code