Skip to content

Latest commit

 

History

History
43 lines (29 loc) · 1.15 KB

2019-01-09-schema-comparison.md

File metadata and controls

43 lines (29 loc) · 1.15 KB
layout title subtitle tags categories
post
Schema Comparison
(key, value) pair
javascript ECMA6 jest testing
tools

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

<script src="https://gist.github.com/bbody/0cd99962f72a5a4d3e437995e65e8651.js"></script>