Skip to content

Commit

Permalink
feat: when() shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Feb 3, 2019
1 parent 0945119 commit 0f9df92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ const proto = (SchemaType.prototype = {
},

when(keys, options) {
if (arguments.length === 1) {
options = keys;
keys = '.';
} else if (isSchema(keys) || typeof keys === 'function') {
options = { ...options, is: keys };
keys = '.';
}

var next = this.clone(),
deps = [].concat(keys).map(key => new Ref(key));

Expand Down
29 changes: 29 additions & 0 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,35 @@ describe('Mixed Types ', () => {
await inst.validate(-1).should.be.fulfilled();
});

it('should support conditional first argument as `is` shortcut', async function() {
let inst = number().when(value => value > 0, {
then: number().min(5),
});

await inst
.validate(4)
.should.be.rejectedWith(ValidationError, /must be greater/);

await inst.validate(5).should.be.fulfilled();

await inst.validate(-1).should.be.fulfilled();
});

it('should support conditional signle argument as options shortcut', async function() {
let inst = number().when({
is: value => value > 0,
then: number().min(5),
});

await inst
.validate(4)
.should.be.rejectedWith(ValidationError, /must be greater/);

await inst.validate(5).should.be.fulfilled();

await inst.validate(-1).should.be.fulfilled();
});

it('should use label in error message', async function() {
let label = 'Label';
let inst = object({
Expand Down

0 comments on commit 0f9df92

Please sign in to comment.