index.js
2.88 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict'
/**
* indicative
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const haye = require('haye')
const arrayExpressionRegex = /(\w[^.*]+)(\.\*\.?)(.+)?/
const _ = require('lodash')
let Parser = exports = module.exports = {}
/**
* parse a given set of validations to a consumable array.
*
* @param {String|Array} rule
*
* @return {Array}
*/
Parser.parse = function (validations) {
if (validations instanceof Array) {
return validations.map((rule) => rule)
}
/**
* Add .map to be backward compatible, since we want
* rules to always expected an array of string,
* null or array.
*
* Can be changed later
*/
return haye.fromPipe(validations).toArray().map((i) => {
return {
name: i.name,
args: i.args === null ? [] : (i.args instanceof Array === true ? i.args : [i.args])
}
})
}
/**
* parses an array expression to a consumable object
*
* @param {String} field
* @return {Object|Null}
*
* @public
*/
Parser.expressionCurryFor = function (field, whenMatched, otherwise) {
const expression = field.match(arrayExpressionRegex)
if (_.size(expression) < 4) {
return otherwise()
}
return whenMatched(expression[1], expression[3])
}
/**
* parses a rule and returns an object with
* field name and parsed rule.
*
* @param {String} rule
* @param {String} field
*
* @return {Object}
*
* @private
*/
Parser.parseFieldRule = function (rule, field) {
return {[field]: Parser.parse(rule)}
}
/**
* parses field rules for a array expressions
*
* @param {Object} data
* @param {String} rule
* @param {String} node
* @param {String} [child]
*
* @return {Object}
*
* @private
*/
Parser.getRulesForExpression = function (data, rule, node, child) {
return _.fromPairs(_.map(data[node], (value, index) => {
const fieldName = _([node, index, child]).takeWhile((value) => value !== undefined).join('.')
return [fieldName, Parser.parse(rule)]
}))
}
/**
* transforms a single rule or an array expression
* into multiple rules
*
* @param {Object} data
* @param {String} rule
* @param {String} field
*
* @return {Object}
*
* @private
*/
Parser.transformRule = function (data, rule, field) {
return Parser.expressionCurryFor(
field,
(dataKey, fieldKey) => Parser.getRulesForExpression(data, rule, dataKey, fieldKey),
() => Parser.parseFieldRule(rule, field)
)
}
/**
* transform rules by parsing each rule and converting
* array expressions into multiple rules
*
* @param {Object} data
* @param {Object} rules
*
* @return {Object}
*
* @private
*/
Parser.transformRules = function (data, rules) {
return _(rules)
.transform((result, rule, field) => {
_.extend(result, Parser.transformRule(data, rule, field))
return result
}, {})
.value()
}