UNPKG

indicative-compiler

Version:

Indicative compiler to compile parsed schema into highly optimized functions

63 lines (62 loc) 1.84 kB
"use strict"; /** * @module compiler/validator */ Object.defineProperty(exports, "__esModule", { value: true }); /** * indicative-compiler * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ const Collector_1 = require("./Collector"); /** * Executor is meant to execute the compiled functions with runtime * data. */ class Executor { constructor(fns) { this.fns = fns; } /** * Executes the compiled functions in sequence. */ async exec(data, Formatter, config, bail, removeAdditional, customErrorCollector) { /** * Creating a root data node. The `tip` and `pointer` will be copied * and mutated down the road */ const root = { tip: data, original: data, pointer: '' }; /** * Collector to collect errors and a fresh data object with only * validated data (relies on removeAdditional though) */ const collector = new Collector_1.Collector(new Formatter(), removeAdditional, customErrorCollector); for (let fn of this.fns) { let passed = false; if (fn.async) { passed = await fn.execAsync(root, collector, config, bail); } else { passed = fn.exec(root, collector, config, bail); } if (!passed && bail) { break; } } const errors = collector.getErrors(); /** * If passed, return the data */ if (!errors) { return removeAdditional ? collector.getData() : data; } /** * Otherwise return errors */ throw errors; } } exports.Executor = Executor;