Skip to content

Instantly share code, notes, and snippets.

@graue graue/expectKeys.js
Created May 19, 2014

What would you like to do?
Function to warn if unexpected keys are in an object
// Shameless yak-shaving. I don't even remotely need this for
// anything right now, but wrote it due to a lament that keys
// in JavaScript {options: 'objects'} aren't checked for
// correctness and typos/misspelling can easily go unnoticed.
var _ = require('underscore');
// Expect all keys in the object to be from the list in "allowed".
// Emit a warning if they don't.
// TODO: Should compile down to nothing in production as this is
// only to debug.
function expectKeys(obj, allowed) {
if (allowed && allowed.length >= 1) {
var toWarnAbout = [];
for (var k in obj) {
if ({}, k) && ~allowed.indexOf(k)) {
if (toWarnAbout.length) {
console.warn('object has unexpected key: ' + toWarnAbout.join(', '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.