Skip to content

Commit

Permalink
[added] array.of shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 10, 2016
1 parent 21cea29 commit 68fc010
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ let hasLength = value => !isAbsent(value) && value.length > 0;

module.exports = ArraySchema

function ArraySchema(){
function ArraySchema(type) {
if (!(this instanceof ArraySchema))
return new ArraySchema()
return new ArraySchema(type)

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

this._subType = null;

this.withMutation(() => {
this.transform(function(values) {
if (typeof values === 'string')
Expand All @@ -31,6 +31,9 @@ function ArraySchema(){

return this.isType(values) ? values : null
})

if (type)
this.of(type)
})
}

Expand Down Expand Up @@ -130,6 +133,12 @@ inherits(ArraySchema, MixedSchema, {
})
},

ensure() {
return this
.default([])
.transform(val => val != null ? [] : [].concat(val))
},

compact(rejector){
let reject = !rejector
? v => !!v
Expand Down
9 changes: 5 additions & 4 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ describe('Array types', function(){
})

it('should pass options to children', function(){
array()
.of(object({ name: string() }))
.cast([{ id: 1, name: 'john' }], { stripUnknown: true })
.should.eql([{name: 'john'}])
array(
object({ name: string() })
)
.cast([{ id: 1, name: 'john' }], { stripUnknown: true })
.should.eql([{name: 'john'}])
})

it('should VALIDATE correctly', function(){
Expand Down

0 comments on commit 68fc010

Please sign in to comment.