UNPKG

mysterium-vpn-js

Version:
40 lines (39 loc) 1.3 kB
"use strict"; /** * Copyright (c) 2020 BlockDev AG * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.validateArray = exports.validateMultiple = void 0; function getTypeString(value) { if (Array.isArray(value)) { return 'array'; } return typeof value; } function validate(typeName, obj, property) { const value = obj[property.name]; if (typeof value === 'undefined' && property.optional) { // valid return; } if (typeof value === 'undefined') { throw new TypeError(`${typeName}: ${property.name} is not provided`); } if (getTypeString(value) !== property.type) { throw new TypeError(`${typeName}: ${property.name} should be "${property.type}"`); } } exports.validate = validate; function validateMultiple(typeName, obj, properties) { properties.forEach((property) => validate(typeName, obj, property)); } exports.validateMultiple = validateMultiple; function validateArray(typeName, arr) { if (getTypeString(arr) !== 'array') { throw new TypeError(`${typeName}: should be "array"`); } } exports.validateArray = validateArray;