Skip to content

Commit 9b5232a

Browse files
committed
[added] allow function then/otherwise bodies
1 parent 0f43122 commit 9b5232a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Condition.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import has from 'lodash/has';
22
import isSchema from './util/isSchema';
33

4+
function callOrConcat(schema) {
5+
if (typeof schema === 'function')
6+
return schema
7+
8+
return base => base.concat(schema)
9+
}
10+
411
class Conditional {
512

613
constructor(refs, options) {
714
let { is, then, otherwise } = options;
815

916
this.refs = [].concat(refs)
1017

18+
then = callOrConcat(then);
19+
otherwise = callOrConcat(otherwise);
20+
1121
if (typeof options === 'function')
1222
this.fn = options
1323
else
@@ -24,8 +34,10 @@ class Conditional {
2434
? is : ((...values) => values.every(value => value === is))
2535

2636
this.fn = function (...values) {
27-
let ctx = values.pop();
28-
return isFn(...values) ? ctx.concat(then) : ctx.concat(otherwise)
37+
let currentSchema = values.pop();
38+
let option = isFn(...values) ? then : otherwise
39+
40+
return option(currentSchema)
2941
}
3042
}
3143
}

0 commit comments

Comments
 (0)