Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import { has } from '../../utils' import isSchema from './util/isSchema' class Condition { constructor(refs, options) { this.refs = refs if (typeof options === 'function') { this.fn = options return } if (!has(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?.value, options?.parent, options?.context, options?.rootValue, ), ) let schema = this.fn.apply(base, values.concat(base, options)) if (schema === undefined || schema === base) return base if (!isSchema(schema)) throw new TypeError('conditions must return a schema object') return schema.resolve(options) } } export default Condition |