Skip to content

Commit 68fc010

Browse files
committed
[added] array.of shorthand
1 parent 21cea29 commit 68fc010

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/array.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ let hasLength = value => !isAbsent(value) && value.length > 0;
1414

1515
module.exports = ArraySchema
1616

17-
function ArraySchema(){
17+
function ArraySchema(type) {
1818
if (!(this instanceof ArraySchema))
19-
return new ArraySchema()
19+
return new ArraySchema(type)
2020

2121
MixedSchema.call(this, { type: 'array'})
2222

2323
this._subType = null;
24-
24+
2525
this.withMutation(() => {
2626
this.transform(function(values) {
2727
if (typeof values === 'string')
@@ -31,6 +31,9 @@ function ArraySchema(){
3131

3232
return this.isType(values) ? values : null
3333
})
34+
35+
if (type)
36+
this.of(type)
3437
})
3538
}
3639

@@ -130,6 +133,12 @@ inherits(ArraySchema, MixedSchema, {
130133
})
131134
},
132135

136+
ensure() {
137+
return this
138+
.default([])
139+
.transform(val => val != null ? [] : [].concat(val))
140+
},
141+
133142
compact(rejector){
134143
let reject = !rejector
135144
? v => !!v

test/array.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ describe('Array types', function(){
6060
})
6161

6262
it('should pass options to children', function(){
63-
array()
64-
.of(object({ name: string() }))
65-
.cast([{ id: 1, name: 'john' }], { stripUnknown: true })
66-
.should.eql([{name: 'john'}])
63+
array(
64+
object({ name: string() })
65+
)
66+
.cast([{ id: 1, name: 'john' }], { stripUnknown: true })
67+
.should.eql([{name: 'john'}])
6768
})
6869

6970
it('should VALIDATE correctly', function(){

0 commit comments

Comments
 (0)