Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use '.@' at the end of the param, to pass effective path instead of the value #1

Merged
merged 8 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})