Skip to content

Instantly share code, notes, and snippets.

@antonmedv
Last active December 17, 2015 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonmedv/5550597 to your computer and use it in GitHub Desktop.
Save antonmedv/5550597 to your computer and use it in GitHub Desktop.
Check json structure: expect json, {mustBe: yes, sub: {weNeedThis: yes, butNotThis: no}}
exports.expect = expect = (json, pattern, throws = false, current = []) ->
condition = false
for own key, value of pattern
at = current.concat [key]
if typeof value is 'boolean'
condition = if value is true then json[key]? else not json[key]?
if not condition and throws
throw (if value is true then 'expected' else 'unexpected') + ' ' + at.join(' ')
else
if json[key]?
condition = expect json[key], value, throws, at
else
condition = false
if throws
throw 'expected ' + at.join(' ')
return condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment