-
Notifications
You must be signed in to change notification settings - Fork 906
/
array.js
150 lines (120 loc) · 3.33 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
import inherits from './util/inherits';
import isAbsent from './util/isAbsent';
import MixedSchema from './mixed';
import { mixed, array as locale } from './locale.js';
import runValidations, { propagateErrors } from './util/runValidations';
let hasLength = value => !isAbsent(value) && value.length > 0;
export default 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 = {}) {
let errors = []
let path = options.path
let subType = this._subType
let endEarly = this._option('abortEarly', options)
let recursive = this._option('recursive', options)
return MixedSchema.prototype._validate
.call(this, _value, options)
.catch(propagateErrors(endEarly, errors))
.then((value) => {
if (!recursive || !subType || !this._typeCheck(value) ) {
if (errors.length) throw errors[0]
return value
}
let validations = value.map((item, idx) => {
var path = (options.path || '') + '[' + idx + ']'
// object._validate note for isStrict explanation
var innerOptions = {
...options,
path,
strict: true,
parent: value
};
if (subType.validate)
return subType.validate(item, innerOptions)
return true
})
return runValidations({
path,
value,
errors,
endEarly,
validations
})
})
},
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)
}
})