UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

95 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateFuncStructure = void 0; const constants_1 = require("../constants"); const isNull_1 = require("../utils/isNull"); const errors_1 = require("../errors"); const dictionary_1 = require("../errors/dictionary"); function validateFuncStructure(definition, args, node, options) { var _a, _b; let parameterIndex = 0; let argumentIndex = 0; const parameters = definition.parameters || []; const incrementCounter = () => { parameterIndex += 1; argumentIndex += 1; }; const execValidators = []; let invalidValidator = false; let hasMismatch = false; while (parameterIndex < parameters.length) { const parameter = parameters[parameterIndex]; const { value, dataType } = (_a = args[argumentIndex]) !== null && _a !== void 0 ? _a : {}; if ((0, isNull_1.isNull)(value) && (0, isNull_1.isNull)(dataType) && parameter.optional) { incrementCounter(); continue; } // Valid if the parameter is required or if it is optional but needs to be specified ex RIGHT("Text",) if ((0, isNull_1.isNull)(value) && (!parameter.optional || (parameter.optional && dataType === constants_1.AST_PRIMITIVES.UNKNOWN))) { invalidValidator = true; hasMismatch = true; execValidators.push(Object.assign(Object.assign({ valid: false, source: constants_1.RESPONSE_LEVEL.function }, dictionary_1.ERROR_DICTIONARY[dictionary_1.ERROR_LIST.missingArg]), { parameter, argumentIndex })); } const validatorContext = { fnNode: node, argument: (_b = args[argumentIndex]) !== null && _b !== void 0 ? _b : {}, globalContext: options.context, }; for (const validatorFunction of parameter.validator) { const validator = validatorFunction(value, dataType, validatorContext); execValidators.push(Object.assign(Object.assign({}, validator), { parameter, argumentIndex })); if (!validator.valid) { if (validator.mismatchData && !hasMismatch) hasMismatch = true; if (!invalidValidator) invalidValidator = true; break; } } incrementCounter(); const parameterListCompleted = parameterIndex === parameters.length; const hasMoreArguments = argumentIndex < args.length; const isRecursive = typeof (definition === null || definition === void 0 ? void 0 : definition.recursiveStartIn) === 'number'; if (parameterListCompleted && hasMoreArguments && isRecursive) { parameterIndex = definition.recursiveStartIn; } } if (invalidValidator) { if (hasMismatch) { throwFunctionMismatch(definition, args, node, { validators: execValidators, }); } else { const failedValidator = execValidators.find((val) => !val.valid); if (failedValidator) throwArgumentError(node, failedValidator); } } if (argumentIndex < args.length) { throwFunctionMismatch(definition, args, node, { error: dictionary_1.ERROR_LIST.tooManyArguments, }); } } exports.validateFuncStructure = validateFuncStructure; function throwFunctionMismatch(definition, args, node, validatorInfo, parameter) { var _a; const baseValidator = Object.assign({ valid: false, source: constants_1.RESPONSE_LEVEL.function }, dictionary_1.ERROR_DICTIONARY[(_a = validatorInfo.error) !== null && _a !== void 0 ? _a : dictionary_1.ERROR_LIST.unknown]); const { expected, received, validator } = (0, errors_1.validateParameters)(definition, args, validatorInfo.validators); throw new errors_1.FunctionArgumentsMismatch({ node, parameter, validator: validator || baseValidator, expected, received, }); } function throwArgumentError(node, validator) { throw new errors_1.ArgumentValidatorError(node, validator); } //# sourceMappingURL=validateFuncStructure.js.map