Skip to content

Commit

Permalink
fix: missing transforms on concat
Browse files Browse the repository at this point in the history
fixes #1260
  • Loading branch information
jquense committed Oct 11, 2021
1 parent 03584f6 commit f3056f2
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-ts-expect": "^1.0.1",
"eslint-plugin-typescript": "^0.14.0",
"husky": "^5.1.1",
"hookem": "^1.0.8",
"jest": "^26.6.1",
"lint-staged": "^11.0.0",
"prettier": "^2.1.2",
Expand Down
7 changes: 4 additions & 3 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export default abstract class BaseSchema<
});
});

combined.transforms = [...base.transforms, ...combined.transforms];
return combined as any;
}

Expand Down Expand Up @@ -384,7 +385,7 @@ export default abstract class BaseSchema<

let finalTests = [];
if (this._whitelistError) finalTests.push(this._whitelistError);
if (this._blacklistError) finalTests.push(this._blacklistError);
if (this._blacklistError) finalTests.push(this._blacklistError);

runTests(
{
Expand Down Expand Up @@ -691,7 +692,7 @@ export default abstract class BaseSchema<
: this.createError({
params: {
values: valids.toArray().join(', '),
resolved
resolved,
},
});
},
Expand Down Expand Up @@ -720,7 +721,7 @@ export default abstract class BaseSchema<
return this.createError({
params: {
values: invalids.toArray().join(', '),
resolved
resolved,
},
});
return true;
Expand Down
61 changes: 39 additions & 22 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,22 @@ describe('Mixed Types ', () => {
});

it('should limit values with a ref', async () => {
let someValues = [1,2,3] ;
let someValues = [1, 2, 3];
let context = { someValues };
let inst = mixed().oneOf([ref('$someValues[0]'),ref('$someValues[1]'),ref('$someValues[2]')]);
await inst.validate(1,{context}).should.eventually.equal(1);
await inst.validate(4,{context}).should.be.rejected().then(err => {
err.type.should.equal('oneOf')
expect(err.params.resolved).to.deep.equal(someValues)
})
})
let inst = mixed().oneOf([
ref('$someValues[0]'),
ref('$someValues[1]'),
ref('$someValues[2]'),
]);
await inst.validate(1, { context }).should.eventually.equal(1);
await inst
.validate(4, { context })
.should.be.rejected()
.then((err) => {
err.type.should.equal('oneOf');
expect(err.params.resolved).to.deep.equal(someValues);
});
});

it('should not require field when notRequired was set', async () => {
let inst = mixed().required();
Expand Down Expand Up @@ -542,12 +549,10 @@ describe('Mixed Types ', () => {

it('should fail when the test function returns a rejected Promise', async () => {
let inst = string().test(() => {
return Promise.reject(new Error('oops an error occurred'))
return Promise.reject(new Error('oops an error occurred'));
});

return inst
.validate('joe')
.should.be.rejected();
return inst.validate('joe').should.be.rejected();
});

describe('withMutation', () => {
Expand Down Expand Up @@ -658,6 +663,18 @@ describe('Mixed Types ', () => {
});
});

it('concat should carry over transforms', async () => {
let inst = string().trim();

await expect(inst.concat(string().min(4)).cast(' hello ')).to.equal(
'hello',
);

await expect(inst.concat(string().min(4)).isValid(' he ')).to.become(
false,
);
});

it('concat should fail on different types', function () {
let inst = string().default('hi');

Expand All @@ -667,17 +684,17 @@ describe('Mixed Types ', () => {
});

it('concat should not overwrite label and meta with undefined', function () {
const testLabel = "Test Label"
const testLabel = 'Test Label';
const testMeta = {
testField: "test field"
}
let baseSchema = mixed().label(testLabel).meta(testMeta)
const otherSchema = mixed()

baseSchema = baseSchema.concat(otherSchema)
expect(baseSchema.spec.label).to.equal(testLabel)
expect(baseSchema.spec.meta.testField).to.equal(testMeta.testField)
})
testField: 'test field',
};
let baseSchema = mixed().label(testLabel).meta(testMeta);
const otherSchema = mixed();

baseSchema = baseSchema.concat(otherSchema);
expect(baseSchema.spec.label).to.equal(testLabel);
expect(baseSchema.spec.meta.testField).to.equal(testMeta.testField);
});

it('concat should allow mixed and other type', function () {
let inst = mixed().default('hi');
Expand Down
Loading

0 comments on commit f3056f2

Please sign in to comment.