Skip to content

Commit 686f6b1

Browse files
committed
[changed] concat() allows mixing "mixed" and other type
1 parent 90f37fa commit 686f6b1

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/mixed.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,21 @@ SchemaType.prototype = {
4646
if (!schema)
4747
return this
4848

49-
if( schema._type !== this._type )
49+
if (schema._type !== this._type && this._type !== 'mixed')
5050
throw new TypeError(`You cannot \`concat()\` schema's of different types: ${this._type} and ${schema._type}`)
5151

5252
var next = _.merge(this.clone(), schema.clone())
5353

5454
// undefined isn't merged over, but is a valid value for default
55-
if( schema._default === undefined && _.has(this, '_default') )
55+
if (schema._default === undefined && _.has(this, '_default'))
5656
next._default = schema._default
5757

5858
// trim exclusive tests, take the most recent ones
5959
next.tests = _.uniq(next.tests.reverse(),
6060
(fn, idx) => next[fn.VALIDATION_KEY] ? fn.VALIDATION_KEY : idx).reverse()
6161

62+
next._type = schema._type;
63+
6264
return next
6365
},
6466

@@ -68,7 +70,7 @@ SchemaType.prototype = {
6870
},
6971

7072
cast(_value, _opts) {
71-
var schema = this._resolve((_opts|| {}).context)
73+
var schema = this._resolve((_opts || {}).context)
7274
return schema._cast(_value, _opts)
7375
},
7476

@@ -212,7 +214,7 @@ SchemaType.prototype = {
212214
test(name, message, test, useCallback) {
213215
var opts = name
214216
, next = this.clone()
215-
, errorMsg, isExclusive;
217+
, isExclusive;
216218

217219
if (typeof name === 'string') {
218220
if (typeof message === 'function')

src/util/condition.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Conditional {
2424
if( !options.then && !options.otherwise )
2525
throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions')
2626

27-
if( options.then && options.then._type !== type || options.otherwise && options.otherwise._type !== type)
28-
throw new TypeError(`cannot create polymorphic conditionals, \`then\` and \`otherwise\` must be the same type: ${type}`)
27+
// if( options.then && options.then._type !== type || options.otherwise && options.otherwise._type !== type)
28+
// throw new TypeError(`cannot create polymorphic conditionals, \`then\` and \`otherwise\` must be the same type: ${type}`)
2929

3030
is = typeof is === 'function'
3131
? is : ((is, value) => is === value).bind(null, is)
@@ -53,4 +53,4 @@ class Conditional {
5353
}
5454
}
5555

56-
module.exports = Conditional;
56+
module.exports = Conditional;

test/mixed.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe( 'Mixed Types ', function(){
105105
})
106106

107107
it('exclusive tests should throw without a name', function(){
108-
;(function(){
108+
(function(){
109109
mixed().test({ message: 'invalid', exclusive: true, test: function(){} })
110110
}).should.throw()
111111
})
@@ -133,7 +133,7 @@ describe( 'Mixed Types ', function(){
133133
message: 'invalid',
134134
exclusive: true,
135135
name: 'max',
136-
test: function(v, path, context){
136+
test: function(){
137137
this.path.should.equal('test')
138138
this.parent.should.eql({ other: 5, test : 'hi' })
139139
this.options.context.should.eql({ user: 'jason' })
@@ -149,7 +149,7 @@ describe( 'Mixed Types ', function(){
149149
var inst = mixed().test({
150150
message: 'invalid ${path}',
151151
name: 'max',
152-
test: function(v){
152+
test: function(){
153153
return this.createError({ path: 'my.path' })
154154
}
155155
})
@@ -166,7 +166,7 @@ describe( 'Mixed Types ', function(){
166166
var inst = mixed().test({
167167
message: 'invalid ${path}',
168168
name: 'max',
169-
test: function(v){
169+
test: function(){
170170
return this.createError({ message: '${path} nope!', path: 'my.path' })
171171
}
172172
})
@@ -254,13 +254,22 @@ describe( 'Mixed Types ', function(){
254254
})
255255

256256
it('concat should fail on different types', function(){
257-
var inst = string().default('hi')
257+
var inst = string().default('hi');
258258

259-
;(function(){
259+
(function(){
260260
inst.concat(object())
261261
}).should.throw(TypeError)
262262
})
263263

264+
it('concat should allow mixed and other type', function(){
265+
var inst = mixed().default('hi');
266+
267+
(function(){
268+
inst.concat(string())._type.should.equal('string')
269+
270+
}).should.not.throw(TypeError)
271+
})
272+
264273
it('concat should maintain undefined defaults', function(){
265274
var inst = string().default('hi')
266275

@@ -285,7 +294,7 @@ describe( 'Mixed Types ', function(){
285294
//parent
286295
inst._validate(undefined, {}, { parent: { prop: 5 }}).should.be.rejected,
287296
inst._validate(undefined, {}, { parent: { prop: 1 }}).should.be.fulfilled,
288-
inst._validate('hello', {}, { parent: { prop: 5 }}).should.be.fulfilled,
297+
inst._validate('hello', {}, { parent: { prop: 5 }}).should.be.fulfilled
289298
])
290299
.then(function(){
291300

@@ -336,6 +345,3 @@ describe( 'Mixed Types ', function(){
336345
})
337346

338347
})
339-
340-
341-

0 commit comments

Comments
 (0)