Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 3x 112x 112x 112x 112x 112x 112x 112x 2x 110x 110x 110x 110x 110x 110x 3x 57x | import { forEach } from 'property-expr'
let trim = part => part.substr(0, part.length - 1).substr(1)
export function getIn(schema, path, value, context = value) {
let parent, lastPart, lastPartDebug
// root path: ''
Iif (!path) return { parent, parentPath: path, schema }
forEach(path, (_part, isBracket, isArray) => {
let part = isBracket ? trim(_part) : _part
schema = schema.resolve({ context, parent, value })
Iif (schema.innerType) {
let idx = isArray ? parseInt(part, 10) : 0
if (value && idx >= value.length) {
throw new Error(
`Yup.reach cannot resolve an array item at index: ${_part}, in the path: ${path}. ` +
`because there is no value at that index. `,
)
}
parent = value
value = value && value[idx]
schema = schema.innerType
}
// sometimes the array index part of a path doesn't exist: "nested.arr.child"
// in these cases the current part is the next schema and should be processed
// in this iteration. For cases where the index signature is included this
// check will fail and we'll handle the `child` part on the next iteration like normal
if (!isArray) {
if (!schema.fields || !schema.fields[part])
throw new Error(
`The schema does not contain the path: ${path}. ` +
`(failed at: ${lastPartDebug} which is a type: "${schema._type}")`,
)
parent = value
value = value && value[part]
schema = schema.fields[part]
}
lastPart = part
lastPartDebug = isBracket ? '[' + _part + ']' : '.' + _part
})
return { schema, parent, parentPath: lastPart }
}
const reach = (obj, path, value, context) =>
getIn(obj, path, value, context).schema
export default reach
|