Skip to content

Commit

Permalink
[changed] added reach error
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 5, 2016
1 parent 5e1949f commit 594fa53
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/util/reach.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
}

0 comments on commit 594fa53

Please sign in to comment.