Skip to content

Commit

Permalink
fix: concat of mixed and subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Feb 3, 2019
1 parent d02ff5e commit 2e87196
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 0 additions & 2 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ const proto = (SchemaType.prototype = {
next = next.test(fn.OPTIONS);
});

next._type = schema._type;

return next;
},

Expand Down
26 changes: 10 additions & 16 deletions src/util/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 20 additions & 1 deletion test/mixed.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit 2e87196

Please sign in to comment.