Skip to content

Commit

Permalink
fix: concat with lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Feb 6, 2019
1 parent 2e87196 commit 461face
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
14 changes: 7 additions & 7 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,22 +103,22 @@ 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
Expand Down
24 changes: 0 additions & 24 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: 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 461face

Please sign in to comment.