UNPKG

indicative-compiler

Version:

Indicative compiler to compile parsed schema into highly optimized functions

50 lines (49 loc) 1.73 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 indicative_parser_1 = require("indicative-parser"); const TreeWalker_1 = require("../TreeWalker"); const ArrayWrapper_1 = require("./ArrayWrapper"); const ValidationsRunner_1 = require("./ValidationsRunner"); /** * Compiles rules and messages schema to an array of top level * functions highly optimized for speed. */ class Compiler { constructor(schema, messages, validations) { this.validations = validations; this.parsedSchema = indicative_parser_1.rulesParser(schema); this.parsedMessages = indicative_parser_1.messagesParser(messages); } /** * Compiles the schema to an array of functions */ compile() { return new TreeWalker_1.TreeWalker( /** * Consume each node inside the tree */ (field, type, rules, dotPath, pointer) => { const messages = this.parsedMessages.fields[pointer] || {}; const genericMessage = this.parsedMessages.rules; return new ValidationsRunner_1.ValidationsRunner(field, type, dotPath, rules, this.validations, messages, genericMessage); }, /** * Wraps array children, since the length of array is unknown. */ (index, field, children, dotPath) => { return new ArrayWrapper_1.ArrayWrapper(field, index, children, dotPath); }).walk(this.parsedSchema); } } exports.Compiler = Compiler;