Skip to content

Commit

Permalink
Merge d9d4bae into 637d527
Browse files Browse the repository at this point in the history
  • Loading branch information
aszu80 committed Jan 30, 2018
2 parents 637d527 + d9d4bae commit fad792b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 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
56 changes: 55 additions & 1 deletion test/rule.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map, cloneDeep } from 'lodash/fp'
import { runRule, normalizeRule, run, makeConfig } from '../src/rule'
import { runRule, normalizeRule, run, makeConfig, getPathTo } from '../src/rule'
import { config, value } from './rule/test-config'

const runNormalizedRule = (rule, val) => runRule(normalizeRule(rule), val)
Expand Down 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 fad792b

Please sign in to comment.