54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = void 0;
|
||
|
|
||
|
var _has = _interopRequireDefault(require("lodash/has"));
|
||
|
|
||
|
var _isSchema = _interopRequireDefault(require("./util/isSchema"));
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
class Condition {
|
||
|
constructor(refs, options) {
|
||
|
this.refs = refs;
|
||
|
this.refs = refs;
|
||
|
|
||
|
if (typeof options === 'function') {
|
||
|
this.fn = options;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!(0, _has.default)(options, 'is')) throw new TypeError('`is:` is required for `when()` conditions');
|
||
|
if (!options.then && !options.otherwise) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions');
|
||
|
let {
|
||
|
is,
|
||
|
then,
|
||
|
otherwise
|
||
|
} = options;
|
||
|
let check = typeof is === 'function' ? is : (...values) => values.every(value => value === is);
|
||
|
|
||
|
this.fn = function (...args) {
|
||
|
let options = args.pop();
|
||
|
let schema = args.pop();
|
||
|
let branch = check(...args) ? then : otherwise;
|
||
|
if (!branch) return undefined;
|
||
|
if (typeof branch === 'function') return branch(schema);
|
||
|
return schema.concat(branch.resolve(options));
|
||
|
};
|
||
|
}
|
||
|
|
||
|
resolve(base, options) {
|
||
|
let values = this.refs.map(ref => ref.getValue(options == null ? void 0 : options.value, options == null ? void 0 : options.parent, options == null ? void 0 : options.context));
|
||
|
let schema = this.fn.apply(base, values.concat(base, options));
|
||
|
if (schema === undefined || schema === base) return base;
|
||
|
if (!(0, _isSchema.default)(schema)) throw new TypeError('conditions must return a schema object');
|
||
|
return schema.resolve(options);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
var _default = Condition;
|
||
|
exports.default = _default;
|