From 686f6b102e3d249d7379ab2424591cad5b086b83 Mon Sep 17 00:00:00 2001 From: jquense Date: Sun, 8 Nov 2015 12:08:35 -0500 Subject: [PATCH] [changed] concat() allows mixing "mixed" and other type --- src/mixed.js | 10 ++++++---- src/util/condition.js | 6 +++--- test/mixed.js | 26 ++++++++++++++++---------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/mixed.js b/src/mixed.js index 99c4be0..2efacba 100644 --- a/src/mixed.js +++ b/src/mixed.js @@ -46,19 +46,21 @@ SchemaType.prototype = { if (!schema) return this - if( schema._type !== this._type ) + if (schema._type !== this._type && this._type !== 'mixed') throw new TypeError(`You cannot \`concat()\` schema's of different types: ${this._type} and ${schema._type}`) var next = _.merge(this.clone(), schema.clone()) // undefined isn't merged over, but is a valid value for default - if( schema._default === undefined && _.has(this, '_default') ) + if (schema._default === undefined && _.has(this, '_default')) next._default = schema._default // trim exclusive tests, take the most recent ones next.tests = _.uniq(next.tests.reverse(), (fn, idx) => next[fn.VALIDATION_KEY] ? fn.VALIDATION_KEY : idx).reverse() + next._type = schema._type; + return next }, @@ -68,7 +70,7 @@ SchemaType.prototype = { }, cast(_value, _opts) { - var schema = this._resolve((_opts|| {}).context) + var schema = this._resolve((_opts || {}).context) return schema._cast(_value, _opts) }, @@ -212,7 +214,7 @@ SchemaType.prototype = { test(name, message, test, useCallback) { var opts = name , next = this.clone() - , errorMsg, isExclusive; + , isExclusive; if (typeof name === 'string') { if (typeof message === 'function') diff --git a/src/util/condition.js b/src/util/condition.js index e1b6c0f..fbed006 100644 --- a/src/util/condition.js +++ b/src/util/condition.js @@ -24,8 +24,8 @@ class Conditional { if( !options.then && !options.otherwise ) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions') - if( options.then && options.then._type !== type || options.otherwise && options.otherwise._type !== type) - throw new TypeError(`cannot create polymorphic conditionals, \`then\` and \`otherwise\` must be the same type: ${type}`) + // if( options.then && options.then._type !== type || options.otherwise && options.otherwise._type !== type) + // throw new TypeError(`cannot create polymorphic conditionals, \`then\` and \`otherwise\` must be the same type: ${type}`) is = typeof is === 'function' ? is : ((is, value) => is === value).bind(null, is) @@ -53,4 +53,4 @@ class Conditional { } } -module.exports = Conditional; \ No newline at end of file +module.exports = Conditional; diff --git a/test/mixed.js b/test/mixed.js index 3f80872..98ed2af 100644 --- a/test/mixed.js +++ b/test/mixed.js @@ -105,7 +105,7 @@ describe( 'Mixed Types ', function(){ }) it('exclusive tests should throw without a name', function(){ - ;(function(){ + (function(){ mixed().test({ message: 'invalid', exclusive: true, test: function(){} }) }).should.throw() }) @@ -133,7 +133,7 @@ describe( 'Mixed Types ', function(){ message: 'invalid', exclusive: true, name: 'max', - test: function(v, path, context){ + test: function(){ this.path.should.equal('test') this.parent.should.eql({ other: 5, test : 'hi' }) this.options.context.should.eql({ user: 'jason' }) @@ -149,7 +149,7 @@ describe( 'Mixed Types ', function(){ var inst = mixed().test({ message: 'invalid ${path}', name: 'max', - test: function(v){ + test: function(){ return this.createError({ path: 'my.path' }) } }) @@ -166,7 +166,7 @@ describe( 'Mixed Types ', function(){ var inst = mixed().test({ message: 'invalid ${path}', name: 'max', - test: function(v){ + test: function(){ return this.createError({ message: '${path} nope!', path: 'my.path' }) } }) @@ -254,13 +254,22 @@ describe( 'Mixed Types ', function(){ }) it('concat should fail on different types', function(){ - var inst = string().default('hi') + var inst = string().default('hi'); - ;(function(){ + (function(){ inst.concat(object()) }).should.throw(TypeError) }) + it('concat should allow mixed and other type', function(){ + var inst = mixed().default('hi'); + + (function(){ + inst.concat(string())._type.should.equal('string') + + }).should.not.throw(TypeError) + }) + it('concat should maintain undefined defaults', function(){ var inst = string().default('hi') @@ -285,7 +294,7 @@ describe( 'Mixed Types ', function(){ //parent inst._validate(undefined, {}, { parent: { prop: 5 }}).should.be.rejected, inst._validate(undefined, {}, { parent: { prop: 1 }}).should.be.fulfilled, - inst._validate('hello', {}, { parent: { prop: 5 }}).should.be.fulfilled, + inst._validate('hello', {}, { parent: { prop: 5 }}).should.be.fulfilled ]) .then(function(){ @@ -336,6 +345,3 @@ describe( 'Mixed Types ', function(){ }) }) - - -