From 68fc010fc13837786903c3500679a22cf76b3056 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Sun, 10 Apr 2016 10:51:04 -0400 Subject: [PATCH] [added] array.of shorthand --- src/array.js | 15 ++++++++++++--- test/array.js | 9 +++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/array.js b/src/array.js index 0749b2bc0..27ebae5e5 100644 --- a/src/array.js +++ b/src/array.js @@ -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') @@ -31,6 +31,9 @@ function ArraySchema(){ return this.isType(values) ? values : null }) + + if (type) + this.of(type) }) } @@ -130,6 +133,12 @@ inherits(ArraySchema, MixedSchema, { }) }, + ensure() { + return this + .default([]) + .transform(val => val != null ? [] : [].concat(val)) + }, + compact(rejector){ let reject = !rejector ? v => !!v diff --git a/test/array.js b/test/array.js index 6b5d3390b..572e8da4d 100644 --- a/test/array.js +++ b/test/array.js @@ -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(){