-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharray.js
33 lines (29 loc) · 959 Bytes
/
array.js
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
const Promise = require('bluebird')
let rules
const loadRules = () => {
if (!rules) rules = require('../rules')
}
const array = {
default: context => {
// Ensure array
if (!Array.isArray(context.value)) {
return context.fail('Value must be an array')
}
// If empty (and empty allowed), move forward
if (context.def.empty && context.value.length === 0) {
return context.value
}
// If empty (and not empty allowed), fail
if (!context.def.empty && context.value.length === 0) {
return context.fail('Value must not be empty array')
}
// Specific array sub-validation
if (!context.def.values) return context.value
loadRules()
const promises = context.value.map((elem, idx) => {
return rules.validate(context.def.values, elem, context.def.opts, `${context.key}[${idx}]`, context.errors, false, context.initData)
})
return Promise.all(promises)
}
}
module.exports = array