(cxt, v, sch, $async)
| 5683 | } |
| 5684 | exports.getValidate = getValidate; |
| 5685 | function callRef(cxt, v, sch, $async) { |
| 5686 | const { gen, it } = cxt; |
| 5687 | const { allErrors, schemaEnv: env, opts } = it; |
| 5688 | const passCxt = opts.passContext ? names_1.default.this : codegen_1.nil; |
| 5689 | if ($async) |
| 5690 | callAsyncRef(); |
| 5691 | else |
| 5692 | callSyncRef(); |
| 5693 | function callAsyncRef() { |
| 5694 | if (!env.$async) |
| 5695 | throw new Error("async schema referenced by sync schema"); |
| 5696 | const valid = gen.let("valid"); |
| 5697 | gen.try(() => { |
| 5698 | gen.code((0, codegen_1._) `await ${(0, code_1.callValidateCode)(cxt, v, passCxt)}`); |
| 5699 | addEvaluatedFrom(v); // TODO will not work with async, it has to be returned with the result |
| 5700 | if (!allErrors) |
| 5701 | gen.assign(valid, true); |
| 5702 | }, (e) => { |
| 5703 | gen.if((0, codegen_1._) `!(${e} instanceof ${it.ValidationError})`, () => gen.throw(e)); |
| 5704 | addErrorsFrom(e); |
| 5705 | if (!allErrors) |
| 5706 | gen.assign(valid, false); |
| 5707 | }); |
| 5708 | cxt.ok(valid); |
| 5709 | } |
| 5710 | function callSyncRef() { |
| 5711 | cxt.result((0, code_1.callValidateCode)(cxt, v, passCxt), () => addEvaluatedFrom(v), () => addErrorsFrom(v)); |
| 5712 | } |
| 5713 | function addErrorsFrom(source) { |
| 5714 | const errs = (0, codegen_1._) `${source}.errors`; |
| 5715 | gen.assign(names_1.default.vErrors, (0, codegen_1._) `${names_1.default.vErrors} === null ? ${errs} : ${names_1.default.vErrors}.concat(${errs})`); // TODO tagged |
| 5716 | gen.assign(names_1.default.errors, (0, codegen_1._) `${names_1.default.vErrors}.length`); |
| 5717 | } |
| 5718 | function addEvaluatedFrom(source) { |
| 5719 | var _a; |
| 5720 | if (!it.opts.unevaluated) |
| 5721 | return; |
| 5722 | const schEvaluated = (_a = sch === null || sch === void 0 ? void 0 : sch.validate) === null || _a === void 0 ? void 0 : _a.evaluated; |
| 5723 | // TODO refactor |
| 5724 | if (it.props !== true) { |
| 5725 | if (schEvaluated && !schEvaluated.dynamicProps) { |
| 5726 | if (schEvaluated.props !== undefined) { |
| 5727 | it.props = util_1.mergeEvaluated.props(gen, schEvaluated.props, it.props); |
| 5728 | } |
| 5729 | } |
| 5730 | else { |
| 5731 | const props = gen.var("props", (0, codegen_1._) `${source}.evaluated.props`); |
| 5732 | it.props = util_1.mergeEvaluated.props(gen, props, it.props, codegen_1.Name); |
| 5733 | } |
| 5734 | } |
| 5735 | if (it.items !== true) { |
| 5736 | if (schEvaluated && !schEvaluated.dynamicItems) { |
| 5737 | if (schEvaluated.items !== undefined) { |
| 5738 | it.items = util_1.mergeEvaluated.items(gen, schEvaluated.items, it.items); |
| 5739 | } |
| 5740 | } |
| 5741 | else { |
| 5742 | const items = gen.var("items", (0, codegen_1._) `${source}.evaluated.items`); |
no test coverage detected