Skip to content

Commit

Permalink
Merge pull request #1 from aszu80/feature/direct-access-to-paths
Browse files Browse the repository at this point in the history
Use '.@' at the end of the param, to pass effective path instead of the value
  • Loading branch information
komlev authored Jan 31, 2018
2 parents 637d527 + d91a932 commit c1b1895
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
isArray,
isObject,
isEmpty,
forEach
forEach,
last,
slice,
cloneDeep
} from 'lodash/fp'
import { isExisty } from './assert'
import { concat, arrayOfArrays, getContextPath } from './utils'
Expand Down Expand Up @@ -56,8 +59,20 @@ const defaultEnabler = [() => true],
return { ...rule, value, test, params, enabled }
},
queryPath = (value, context) => (p) => {
if (isFunction(p)) return p()
return traverse(getContextPath(p, context.indexes), value)
if (isFunction(p)) {
// clone to prevent anyone from messing with lib internals
return p(cloneDeep({
current: context.current,
indexes: context.indexes
}))
}
const
contextPath = getContextPath(p, context.indexes)

if (last(contextPath) === '@') {
return slice(0, contextPath.length - 1, contextPath)
}
return traverse(contextPath, value)
},
queryPaths = (params, value, context) =>
map(queryPath(value, context), params),
Expand Down Expand Up @@ -102,7 +117,7 @@ const defaultEnabler = [() => true],
resPath =
(rule.to && getContextPath(rule.to, context.indexes)) || context.goal
if (!isEnabled(ruleParams, rule.enabled)) return []
// eslint-disable-next-line
// eslint-disable-next-line one-var
const errors = map(r => r.value, validateItem([ruleParams, rule.test]))
if (!isEmpty(errors)) res = set(resPath, errors, res)
return null
Expand Down
54 changes: 54 additions & 0 deletions test/rule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,58 @@ describe('rule', () => {
team: [undefined, undefined, { customName: ['empty'], name: ['empty'] }]
})
})

it('provides path to the processed value', () => {
const
successMap2 = {
'L.1.1': ['team', '0', 'address', '0'],
'L.2.1': ['team', '1', 'address', '0'],
'L.2.2': ['team', '1', 'address', '1']
},
successMap3 = {
'L.1.1': ['team', 0, 'address', 0, 'line1'],
'L.2.1': ['team', 1, 'address', 0, 'line1'],
'L.2.2': ['team', 1, 'address', 1, 'line1']
},
successMap3b = {
'L.1.1': { team: 0, address: 0 },
'L.2.1': { team: 1, address: 0 },
'L.2.2': { team: 1, address: 1 }
},
rule = {
// -1
value: 'team.address.line1',
params: [
'team.{team}.address.{address}.line1',
'team.{team}.address.{address}.@',
a => a
],
test: [
[(v, ...params) => {
const [p1, p2, p3] = params
expect(successMap2[p1]).toEqual(p2)
expect(successMap3[p1]).toEqual(p3.current)
expect(successMap3b[p1]).toEqual(p3.indexes)
return true
}, 'ERR']
]
},
valForTest = {
team: [
{
address: [
{ line1: 'L.1.1' }
]
},
{
address: [
{ line1: 'L.2.1' },
{ line1: 'L.2.2' }
]
}
]
}

runNormalizedRule(rule, valForTest)
})
})

0 comments on commit c1b1895

Please sign in to comment.