UNPKG

@edgeguideab/expect

Version:

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

163 lines (162 loc) 6.31 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.ExpectType = void 0; var index_1 = require("../util/index"); var any_1 = require("./any"); var array_1 = require("./array"); var boolean_1 = require("./boolean"); var number_1 = require("./number"); var object_1 = require("./object"); var string_1 = require("./string"); var date_1 = require("./date"); exports.ExpectType = { any: "any", number: "number", boolean: "boolean", string: "string", array: "array", object: "object", date: "date", }; var validate = function (props) { var type = props.type, parameter = props.parameter, input = props.input, schema = props.schema, visitedParams = props.visitedParams; var options = props.options, value = props.value; if (typeof options === "string") options = { type: type }; var parse = options.parse, equalTo = options.equalTo, requiredIf = options.requiredIf, allowNull = options.allowNull, condition = options.condition, errorCode = options.errorCode, allowNullErrorCode = options.allowNullErrorCode, conditionErrorCode = options.conditionErrorCode, equalToErrorCode = options.equalToErrorCode; var initialValue = value; if (parse) { value = typeof parse === "function" ? (0, index_1.parseFunctionWrapper)({ value: value, parse: parse }) : (0, index_1.parseType)({ value: value, type: type }); } var validation = validateType({ type: type, parameter: parameter, value: value, options: options, input: input, schema: schema, visitedParams: visitedParams, }); value = "parsed" in validation ? validation.parsed : value; var isNullValue = (0, index_1.isNull)(value) || (0, index_1.isNull)(initialValue); var isAllowNull = typeof allowNull === "function" ? allowNullWrapper({ value: value, allowNull: allowNull }) : allowNull; var notRequired = requiredIf && (0, index_1.isNull)((0, index_1.getDeep)(requiredIf, input)); var nullAllowed = isAllowNull || notRequired; if (isNullValue && !nullAllowed) { return { valid: false, error: allowNullErrorCode || errorCode || "Expected parameter " + (0, index_1.formatParameter)(parameter) + " to be of type " + type + " but it was " + ((0, index_1.isNull)(initialValue) ? JSON.stringify(initialValue) : JSON.stringify(value)), }; } if (!validation.valid && (!isNullValue || !nullAllowed)) { return { valid: false, error: validation.error || errorCode || "Expected parameter " + (0, index_1.formatParameter)(parameter) + " to be of type " + type + " but it was " + JSON.stringify(value), }; } var parsed = nullAllowed && (0, index_1.isNull)(initialValue) && !validation.valid ? initialValue : value; if (equalTo && !(0, index_1.isEqualTo)({ type: type, value: parsed, parameter: parameter, equalTo: equalTo, input: input, schema: schema, visitedParams: visitedParams, validate: exports.validate, })) { return { valid: false, error: equalToErrorCode || errorCode || "Expected parameter " + (0, index_1.formatParameter)(parameter) + " to be equal to " + JSON.stringify(equalTo) + ".", }; } if (nullAllowed && isNullValue) return { valid: true, parsed: parsed }; if (typeof condition === "function") { var valid = false; try { valid = condition(value); } catch (error) { // Do nothing } if (!valid) { return { valid: false, error: conditionErrorCode || errorCode || "Expected parameter " + (0, index_1.formatParameter)(parameter) + " to meet condition", }; } } return { valid: true, parsed: parsed }; }; exports.validate = validate; function allowNullWrapper(_a) { var value = _a.value, allowNull = _a.allowNull; try { return allowNull(value); } catch (error) { return false; } } function validateType(_a) { var type = _a.type, props = __rest(_a, ["type"]); switch (type) { case "any": return (0, any_1.validateAny)(); case "boolean": return (0, boolean_1.validateBoolean)(props); case "number": return (0, number_1.validateNumber)(props); case "date": return (0, date_1.validateDate)(props); case "string": return (0, string_1.validateString)(__assign(__assign({}, props), { options: props.options })); case "object": return (0, object_1.validateObject)(__assign(__assign({}, props), { validate: exports.validate, options: props.options })); case "array": return (0, array_1.validateArray)(__assign(__assign({}, props), { validate: exports.validate, options: props.options })); default: throw new Error("Invalid type " + JSON.stringify(type) + " for parameter " + (0, index_1.formatParameter)(props.parameter)); } }