UNPKG

@edgeguideab/expect

Version:

Check for user input in a consistent way and generate error messages for missings

53 lines (52 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateArray = void 0; var index_1 = require("../util/index"); function validateArray(_a) { var parameter = _a.parameter, value = _a.value, options = _a.options, input = _a.input, schema = _a.schema, _b = _a.visitedParams, visitedParams = _b === void 0 ? [] : _b, validate = _a.validate; var convert = options.convert, items = options.items; if (convert && !Array.isArray(value)) { value = value === undefined ? [] : [value]; } if (!Array.isArray(value)) return { valid: false }; if (!items) return { valid: true, parsed: value }; var error = Object.create(null); var parsed = []; var hasInvalidItems = value.filter(function (item, index) { var itemOptions = items; if (typeof items === "function") { try { itemOptions = items(item); } catch (itemsError) { return { valid: false, error: itemsError }; } } if (itemOptions === null || (typeof itemOptions !== "string" && typeof itemOptions !== "object")) { return { valid: false, error: "Invalid items" }; } var validation = validate({ type: typeof itemOptions === "string" ? itemOptions : itemOptions.type, parameter: Array.isArray(parameter) ? parameter.concat(index) : [parameter, index], value: item, options: itemOptions, input: input, schema: schema, visitedParams: visitedParams.concat((0, index_1.formatParameter)(parameter)), }); if (!validation.valid) error[index] = validation.error; else parsed.push("parsed" in validation ? validation.parsed : item); return !validation.valid; }); return hasInvalidItems.length ? { valid: false, error: error } : { valid: true, parsed: parsed }; } exports.validateArray = validateArray;