1 | var N = require('libnested')
|
2 |
|
3 | module.exports = function assertGiven (gives, given, key) {
|
4 | if (!given) {
|
5 | throw new Error('create function should return a function or an object in: ' + key)
|
6 | }
|
7 |
|
8 | if (typeof gives === 'string' && typeof given !== 'function') {
|
9 | throw new Error('create function should return a function when gives is a string in: ' + key)
|
10 | } else if (isObject(gives) && isObject(given)) {
|
11 | firstMissingKey(gives, given, function (path) {
|
12 | throw new Error('keys returned by create must match keys in given. missing: ' + path.join('.') + ' in ' + key)
|
13 | })
|
14 | }
|
15 | }
|
16 |
|
17 | function firstMissingKey (gives, given, onMissingKey) {
|
18 | return N.each(gives, function (value, path) {
|
19 | if (N.get(given, path) === undefined) {
|
20 | onMissingKey(path)
|
21 | return false
|
22 | }
|
23 | })
|
24 | }
|
25 |
|
26 | function isObject (o) {
|
27 | return o && typeof o === 'object'
|
28 | }
|