diff --git a/src/util/reach.js b/src/util/reach.js index bcdfc3394..af172defd 100644 --- a/src/util/reach.js +++ b/src/util/reach.js @@ -1,15 +1,14 @@ -'use strict'; let { forEach } = require('property-expr') , { has } = require('./_'); let trim = part => part.substr(0, part.length - 1).substr(1) module.exports = function (obj, path, value, context) { + let parent, lastPart; + // if only one "value" arg then use it for both context = context || value; - let parent, lastPart; - forEach(path, (_part, isBracket, isArray) => { let part = isBracket ? trim(_part) : _part; @@ -21,18 +20,19 @@ module.exports = function (obj, path, value, context) { if (!isArray) { obj = obj._resolve(context, parent); - if (!has(obj, 'fields')) + if (!has(obj, 'fields') || !has(obj.fields, part)) throw new Error( `The schema does not contain the path: ${path}. ` + `(failed at: ${lastPart} which is a type: "${obj._type}") ` ) obj = obj.fields[part] + parent = value; value = value && value[part] lastPart = isBracket ? '[' + _part + ']' : '.' + _part } }) - return obj._resolve(parent) + return obj && obj._resolve(parent) }