From 594fa53f2c3e78c65a48eeeb8ddbabf1c9229977 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Tue, 5 Apr 2016 16:55:30 -0400 Subject: [PATCH] [changed] added reach error --- src/util/reach.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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) }