Skip to content

Commit

Permalink
fix: array.ensure
Browse files Browse the repository at this point in the history
closes #343
  • Loading branch information
jquense committed Mar 6, 2020
1 parent 3d90d6f commit 94659c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ inherits(ArraySchema, MixedSchema, {
},

ensure() {
return this.default(() => []).transform(val => {
if (this.isType(val)) return val;
return val === null ? [] : [].concat(val);
return this.default(() => []).transform((val, original) => {
// We don't want to return `null` for nullable schema
if (this._typeCheck(val)) return val;
return original == null ? [] : [].concat(original);
});
},

compact(rejector) {
let reject = !rejector ? v => !!v : (v, i, a) => !rejector(v, i, a);

return this.transform(
values => (values != null ? values.filter(reject) : values),
return this.transform(values =>
values != null ? values.filter(reject) : values,
);
},

Expand Down
12 changes: 12 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ describe('Array types', () => {
inst.cast(a).should.equal(a);

inst.cast(null).should.eql([]);
// nullable is redundant since this should always produce an array
// but we want to ensure that null is actually turned into an array
inst
.nullable()
.cast(null)
.should.eql([]);

inst.cast(1).should.eql([1]);
inst
.nullable()
.cast(1)
.should.eql([1]);
});

it('should pass resolved path to descendants', async () => {
Expand Down

0 comments on commit 94659c2

Please sign in to comment.