Skip to content

Commit

Permalink
fix: concat of mixed and subtype (#444)
Browse files Browse the repository at this point in the history
* fix: concat of mixed and subtype

* fix: concat with lazy
  • Loading branch information
vonagam authored and jquense committed Feb 6, 2019
1 parent 386eb48 commit 7705972
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 45 deletions.
16 changes: 7 additions & 9 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import toArray from 'lodash/toArray';
import { mixed as locale } from './locale';
import Condition from './Condition';
import runValidations from './util/runValidations';
import merge from './util/merge';
import prependDeep from './util/prependDeep';
import isSchema from './util/isSchema';
import isAbsent from './util/isAbsent';
import createValidation from './util/createValidation';
Expand Down Expand Up @@ -103,31 +103,29 @@ const proto = (SchemaType.prototype = {
},

concat(schema) {
if (!schema) return this;
if (!schema || schema === this) return this;

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 cloned = this.clone();
var next = merge(this.clone(), schema.clone());

// undefined isn't merged over, but is a valid value for default
var next = prependDeep(schema.clone(), this);

// new undefined default is overriden by old non-undefined one, revert
if (has(schema, '_default')) next._default = schema._default;

next.tests = cloned.tests;
next._exclusive = cloned._exclusive;
next.tests = this.tests;
next._exclusive = this._exclusive;

// manually add the new tests to ensure
// the deduping logic is consistent
schema.tests.forEach(fn => {
next = next.test(fn.OPTIONS);
});

next._type = schema._type;

return next;
},

Expand Down
30 changes: 0 additions & 30 deletions src/util/merge.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/util/prependDeep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import has from 'lodash/has';
import isSchema from './isSchema';

let isObject = obj => Object.prototype.toString.call(obj) === '[object Object]';

export default function prependDeep(target, source) {
for (var key in source)
if (has(source, key)) {
var sourceVal = source[key],
targetVal = target[key];

if (targetVal === undefined) {
target[key] = sourceVal;
} else if (targetVal === sourceVal) {
continue;
} else if (isSchema(targetVal)) {
if (isSchema(sourceVal)) target[key] = sourceVal.concat(targetVal);
} else if (isObject(targetVal)) {
if (isObject(sourceVal))
target[key] = prependDeep(targetVal, sourceVal);
} else if (Array.isArray(targetVal)) {
if (Array.isArray(sourceVal)) target[key] = sourceVal.concat(targetVal);
}
}

return target;
}
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
21 changes: 16 additions & 5 deletions test/yup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import reach, { getIn } from '../src/util/reach';
import merge from '../src/util/merge';
import prependDeep from '../src/util/prependDeep';
import { settled } from '../src/util/runValidations';

import { object, array, string, lazy, number } from '../src';
Expand Down Expand Up @@ -36,11 +36,11 @@ describe('Yup', function() {
]);
});

it('should merge', function() {
var a = { a: 1, b: 'hello', c: [1, 2, 3], d: { a: /hi/ }, e: { b: 5 } };
var b = { a: 4, c: [4, 5, 3], d: { b: 'hello' }, f: { c: 5 }, g: null };
it('should prepend deeply', function() {
var a = { a: 4, c: [4, 5, 3], d: { b: 'hello' }, f: { c: 5 }, g: null };
var b = { a: 1, b: 'hello', c: [1, 2, 3], d: { a: /hi/ }, e: { b: 5 } };

merge(a, b).should.deep.eql({
prependDeep(a, b).should.deep.eql({
a: 4,
b: 'hello',
c: [1, 2, 3, 4, 5, 3],
Expand All @@ -54,6 +54,17 @@ describe('Yup', function() {
});
});

it('should not prepend needlesly', function() {
var schema = string();
var spy = sinon.spy(schema, 'concat');
var a = { schema };
var b = { schema };
var c = prependDeep(a, b);

c.schema.should.equal(schema);
spy.should.not.have.been.called();
});

it('should getIn correctly', async () => {
var num = number(),
inst = object().shape({
Expand Down

0 comments on commit 7705972

Please sign in to comment.