-
Notifications
You must be signed in to change notification settings - Fork 906
/
array.js
151 lines (119 loc) · 3.44 KB
/
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
var MixedSchema = require('./mixed')
, Promise = require('promise/lib/es6-extensions')
, isAbsent = require('./util/isAbsent')
, { mixed, array: locale } = require('./locale.js')
, { inherits, collectErrors } = require('./util/_');
let scopeError = value => err => {
err.value = value
throw err
}
let hasLength = value => !isAbsent(value) && value.length > 0;
module.exports = ArraySchema
function ArraySchema(type) {
if (!(this instanceof ArraySchema))
return new ArraySchema(type)
MixedSchema.call(this, { type: 'array'})
this._subType = null;
this.withMutation(() => {
this.transform(function(values) {
if (typeof values === 'string')
try {
values = JSON.parse(values)
} catch (err){ values = null }
return this.isType(values) ? values : null
})
if (type)
this.of(type)
})
}
inherits(ArraySchema, MixedSchema, {
_typeCheck(v){
return Array.isArray(v)
},
_cast(_value, _opts) {
var value = MixedSchema.prototype._cast.call(this, _value, _opts)
//should ignore nulls here
if (!this._typeCheck(value) || !this._subType)
return value;
return value.map(v => this._subType.cast(v, _opts))
},
_validate(_value, options = {}) {
var errors = []
, subType, endEarly, recursive;
subType = this._subType
endEarly = this._option('abortEarly', options)
recursive = this._option('recursive', options)
return MixedSchema.prototype._validate.call(this, _value, options)
.catch(endEarly ? null : err => {
errors = err
return err.value
})
.then((value) => {
if (!recursive || !subType || !this._typeCheck(value) ) {
if (errors.length) throw errors[0]
return value
}
let result = value.map((item, key) => {
var path = (options.path || '') + '[' + key + ']'
// object._validate note for isStrict explanation
var innerOptions = { ...options, path, key, strict: true, parent: value };
if (subType.validate)
return subType.validate(item, innerOptions)
return true
})
result = endEarly
? Promise.all(result).catch(scopeError(value))
: collectErrors(result, value, options.path, errors)
return result.then(() => value)
})
},
of(schema){
var next = this.clone()
next._subType = schema
return next
},
required(msg) {
var next = MixedSchema.prototype.required.call(this, msg || mixed.required);
return next.test(
'required'
, msg || mixed.required
, hasLength
)
},
min(min, message){
message = message || locale.min
return this.test({
message,
name: 'min',
exclusive: true,
params: { min },
test(value) {
return isAbsent(value) || value.length >= this.resolve(min)
}
})
},
max(max, message){
message = message || locale.max
return this.test({
message,
name: 'max',
exclusive: true,
params: { max },
test(value) {
return isAbsent(value) || value.length <= this.resolve(max)
}
})
},
ensure() {
return this
.default([])
.transform(val => val != null ? [] : [].concat(val))
},
compact(rejector){
let reject = !rejector
? v => !!v
: (v, i, a) => !rejector(v, i, a);
return this.transform(values => values != null ? values.filter(reject) : values)
}
})