Skip to content

Commit

Permalink
[changed] concat() allows mixing "mixed" and other type
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Nov 8, 2015
1 parent 90f37fa commit 686f6b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},

Expand All @@ -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)
},

Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions src/util/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,4 +53,4 @@ class Conditional {
}
}

module.exports = Conditional;
module.exports = Conditional;
26 changes: 16 additions & 10 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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' })
Expand All @@ -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' })
}
})
Expand All @@ -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' })
}
})
Expand Down Expand Up @@ -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')

Expand All @@ -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(){

Expand Down Expand Up @@ -336,6 +345,3 @@ describe( 'Mixed Types ', function(){
})

})



0 comments on commit 686f6b1

Please sign in to comment.