UNPKG

@splitsoftware/splitio-commons

Version:
52 lines (51 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateKey = void 0; var lang_1 = require("../lang"); var constants_1 = require("../../logger/constants"); var KEY_MAX_LENGTH = 250; function validateKeyValue(log, maybeKey, method, type) { if (maybeKey == undefined) { // eslint-disable-line eqeqeq log.error(constants_1.ERROR_NULL, [method, type]); return false; } if ((0, lang_1.isFiniteNumber)(maybeKey)) { log.warn(constants_1.WARN_CONVERTING, [method, type, maybeKey]); return (0, lang_1.toString)(maybeKey); } if ((0, lang_1.isString)(maybeKey)) { // It's a string, start by trimming the value. maybeKey = maybeKey.trim(); // It's aaaaaall good. if (maybeKey.length > 0 && maybeKey.length <= KEY_MAX_LENGTH) return maybeKey; if (maybeKey.length === 0) { log.error(constants_1.ERROR_EMPTY, [method, type]); } else if (maybeKey.length > KEY_MAX_LENGTH) { log.error(constants_1.ERROR_TOO_LONG, [method, type]); } } else { log.error(constants_1.ERROR_INVALID, [method, type]); } return false; } function validateKey(log, maybeKey, method) { if ((0, lang_1.isObject)(maybeKey)) { // Validate key object var matchingKey = validateKeyValue(log, maybeKey.matchingKey, method, 'matchingKey'); var bucketingKey = validateKeyValue(log, maybeKey.bucketingKey, method, 'bucketingKey'); if (matchingKey && bucketingKey) return { matchingKey: matchingKey, bucketingKey: bucketingKey }; log.error(constants_1.ERROR_INVALID_KEY_OBJECT, [method]); return false; } else { return validateKeyValue(log, maybeKey, method, 'key'); } } exports.validateKey = validateKey;