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.
- 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
const isObjectMappedToSchema = require('./schema-compare')
if (isObjectMappedToSchema(
{key: {sub_key: "Something"}},
{key: {sub_key: true}})) {
console.log("Payload matches Schema")
}
Tests are written with Jest.
npm install jest -g
jest