From 2e87196f96771120a15eaf2f982205a3947e181e Mon Sep 17 00:00:00 2001 From: Dmitry Maganov Date: Sun, 3 Feb 2019 16:58:13 +0300 Subject: [PATCH] fix: concat of mixed and subtype --- src/mixed.js | 2 -- src/util/merge.js | 26 ++++++++++---------------- test/mixed.js | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/mixed.js b/src/mixed.js index ac45454cb..a4173b04b 100644 --- a/src/mixed.js +++ b/src/mixed.js @@ -126,8 +126,6 @@ const proto = (SchemaType.prototype = { next = next.test(fn.OPTIONS); }); - next._type = schema._type; - return next; }, diff --git a/src/util/merge.js b/src/util/merge.js index 79ab8a2ba..323070425 100644 --- a/src/util/merge.js +++ b/src/util/merge.js @@ -4,27 +4,21 @@ import isSchema from './isSchema'; let isObject = obj => Object.prototype.toString.call(obj) === '[object Object]'; export default function merge(target, source) { - for (var key in source) - if (has(source, key)) { + for (var key in target) + if (has(target, key)) { var targetVal = target[key], sourceVal = source[key]; - if (sourceVal === undefined) continue; - - if (isSchema(sourceVal)) { - target[key] = isSchema(targetVal) - ? targetVal.concat(sourceVal) - : sourceVal; + if (sourceVal === undefined) { + source[key] = targetVal; + } else if (isSchema(sourceVal)) { + if (isSchema(targetVal)) source[key] = targetVal.concat(sourceVal); } else if (isObject(sourceVal)) { - target[key] = isObject(targetVal) - ? merge(targetVal, sourceVal) - : sourceVal; + if (isObject(targetVal)) source[key] = merge(targetVal, sourceVal); } else if (Array.isArray(sourceVal)) { - target[key] = Array.isArray(targetVal) - ? targetVal.concat(sourceVal) - : sourceVal; - } else target[key] = source[key]; + if (Array.isArray(targetVal)) source[key] = targetVal.concat(sourceVal); + } } - return target; + return source; } diff --git a/test/mixed.js b/test/mixed.js index 153f79ed3..a71efe828 100644 --- a/test/mixed.js +++ b/test/mixed.js @@ -1,4 +1,15 @@ -import { array, mixed, string, number, object, ref, reach, bool } from '../src'; +import { + array, + mixed, + string, + number, + object, + ref, + reach, + bool, + ValidationError, +} from '../src'; + let noop = () => {}; function ensureSync(fn) { @@ -585,6 +596,14 @@ describe('Mixed Types ', () => { }.should.not.throw(TypeError)); }); + it('concat should validate with mixed and other type', async function() { + let inst = mixed().concat(number()); + + await inst + .validate([]) + .should.be.rejected(ValidationError, /should be a `number`/); + }); + it('concat should maintain undefined defaults', function() { let inst = string().default('hi');