-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: reach() no longer resolves the returned schema meaning it's conditions have not been processed yet; prefer validateAt/castAt where it makes sense
- Loading branch information
Showing
7 changed files
with
129 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,64 @@ | ||
import has from 'lodash/has'; | ||
import isSchema from './util/isSchema'; | ||
|
||
function callOrConcat(schema) { | ||
if (typeof schema === 'function') return schema; | ||
|
||
return base => base.concat(schema); | ||
function wrapCusomFn(fn) { | ||
return function(...args) { | ||
args.pop(); | ||
return fn.apply(this, args); | ||
}; | ||
} | ||
|
||
class Conditional { | ||
constructor(refs, options) { | ||
let { is, then, otherwise } = options; | ||
function makeFn(options) { | ||
if (typeof options === 'function') return wrapCusomFn(options); | ||
|
||
this.refs = [].concat(refs); | ||
if (!has(options, 'is')) | ||
throw new TypeError('`is:` is required for `when()` conditions'); | ||
|
||
then = callOrConcat(then); | ||
otherwise = callOrConcat(otherwise); | ||
if (!options.then && !options.otherwise) | ||
throw new TypeError( | ||
'either `then:` or `otherwise:` is required for `when()` conditions', | ||
); | ||
|
||
if (typeof options === 'function') this.fn = options; | ||
else { | ||
if (!has(options, 'is')) | ||
throw new TypeError('`is:` is required for `when()` conditions'); | ||
let { is, then, otherwise } = options; | ||
|
||
if (!options.then && !options.otherwise) | ||
throw new TypeError( | ||
'either `then:` or `otherwise:` is required for `when()` conditions', | ||
); | ||
let check; | ||
if (typeof is === 'function') { | ||
check = is; | ||
} else { | ||
check = (...values) => values.every(value => value === is); | ||
} | ||
|
||
let isFn = | ||
typeof is === 'function' | ||
? is | ||
: (...values) => values.every(value => value === is); | ||
let fn = function(...args) { | ||
let options = args.pop(); | ||
let schema = args.pop(); | ||
let branch = check(...args) ? then : otherwise; | ||
|
||
this.fn = function(...values) { | ||
let currentSchema = values.pop(); | ||
let option = isFn(...values) ? then : otherwise; | ||
if (!branch) return undefined; | ||
if (typeof branch === 'function') return branch(schema); | ||
return schema.concat(branch.resolve(options)); | ||
}; | ||
|
||
return option(currentSchema); | ||
}; | ||
} | ||
return fn; | ||
} | ||
|
||
class Condition { | ||
constructor(refs, options) { | ||
this.refs = refs; | ||
this.fn = makeFn(options); | ||
} | ||
|
||
resolve(ctx, options) { | ||
resolve(base, options) { | ||
let values = this.refs.map(ref => ref.getValue(options)); | ||
|
||
let schema = this.fn.apply(ctx, values.concat(ctx)); | ||
let schema = this.fn.apply(base, values.concat(base, options)); | ||
|
||
if (schema === undefined || schema === base) return base; | ||
|
||
if (schema !== undefined && !isSchema(schema)) | ||
if (!isSchema(schema)) | ||
throw new TypeError('conditions must return a schema object'); | ||
|
||
return schema || ctx; | ||
return schema.resolve(options); | ||
} | ||
} | ||
|
||
export default Conditional; | ||
export default Condition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters