UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.validateDuplicateKeys = exports.validateMapElementNotNull = exports.validateArrayElementsNotNull = exports.validateNotNull = void 0;
19/**
20 * Validates that an object reference passed as a parameter to the calling
21 * method is not null.
22 *
23 * @param reference An object reference.
24 * @param errorMessage The exception message to use if the check fails.
25 * @returns An object reference.
26 */
27function validateNotNull(reference, errorMessage) {
28 if (reference === null || reference === undefined) {
29 throw new Error(`Missing mandatory ${errorMessage} parameter`);
30 }
31 return reference;
32}
33exports.validateNotNull = validateNotNull;
34/**
35 * Validates that an array passed as a parameter doesn't contain null element.
36 *
37 * @param list The argument list to check for null.
38 * @param errorMessage The exception message to use if the check fails.
39 */
40function validateArrayElementsNotNull(array, errorMessage) {
41 const areAllDefined = array.every(element => element !== null && typeof element !== 'undefined');
42 if (!areAllDefined) {
43 throw new Error(`${errorMessage} elements should not be a NULL`);
44 }
45}
46exports.validateArrayElementsNotNull = validateArrayElementsNotNull;
47/** Throws an error if any of the map elements is null. */
48function validateMapElementNotNull(map, errorMessage) {
49 for (const [key, value] of map.entries()) {
50 if (key == null || value == null) {
51 throw new Error(`${errorMessage} elements should not be a NULL`);
52 }
53 }
54}
55exports.validateMapElementNotNull = validateMapElementNotNull;
56/** Throws an error if any of the array element present in the map. */
57function validateDuplicateKeys(keys, constantLabels) {
58 const keysAndConstantKeys = new Set([...keys, ...constantLabels.keys()].map(k => k.key));
59 if (keysAndConstantKeys.size !== keys.length + constantLabels.size) {
60 throw new Error(`The keys from LabelKeys should not be present in constantLabels or LabelKeys should not contains duplicate keys`);
61 }
62}
63exports.validateDuplicateKeys = validateDuplicateKeys;
64//# sourceMappingURL=validations.js.map
\No newline at end of file